在Liferay 6.2中插入数据到数据库中,可以通过以下步骤实现:
portal-ext.properties
文件,添加以下配置信息:jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost:3306/your_database_name
jdbc.default.username=your_username
jdbc.default.password=your_password
请将your_database_name
替换为实际的数据库名称,your_username
和your_password
替换为实际的数据库用户名和密码。
import com.liferay.portal.kernel.model.BaseModel;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.model.impl.BaseModelImpl;
public class CustomEntity extends BaseModelImpl<CustomEntity> implements BaseModel<CustomEntity> {
private long entityId;
private String name;
public CustomEntity() {
}
public long getEntityId() {
return entityId;
}
public void setEntityId(long entityId) {
this.entityId = entityId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import com.liferay.portal.service.impl.BaseLocalServiceImpl;
public class CustomEntityLocalServiceImpl extends BaseLocalServiceImpl implements CustomEntityLocalService {
public CustomEntity addCustomEntity(String name) {
long entityId = counterLocalService.increment(CustomEntity.class.getName());
CustomEntity customEntity = customEntityPersistence.create(entityId);
customEntity.setName(name);
customEntityPersistence.update(customEntity);
return customEntity;
}
}
service.xml
文件,添加以下配置信息:<service-builder package-path="com.example">
<entity name="CustomEntity" local-service="true" remote-service="false">
<!-- 定义实体类字段 -->
<column name="entityId" type="long" primary="true" />
<column name="name" type="String" />
</entity>
</service-builder>
service.xml
文件生成相应的Java类和接口。运行以下命令:ant build-service
import com.liferay.util.bridges.mvc.MVCPortlet;
public class CustomPortlet extends MVCPortlet {
public void addData(ActionRequest request, ActionResponse response) {
String name = ParamUtil.getString(request, "name");
CustomEntityLocalServiceUtil.addCustomEntity(name);
}
}
以上步骤完成后,你就可以在Liferay 6.2中插入数据到数据库中了。请注意,以上示例中的代码仅供参考,实际应用中可能需要根据具体需求进行调整。
领取专属 10元无门槛券
手把手带您无忧上云