(五)spring-boot中使用spring-data-jpa(hibernate实现)
来源:互联网 发布:亚马逊销售数据分析 编辑:程序博客网 时间:2024/06/06 19:53
好久没写了,今天更一篇。在spring-boot中使用spring-data-jpa作为持久层框架,hibernate作为实现。在多项目构建的基础上,在dafangzi-core,dafangzi-system中引入对spring-boot-starter-data-jpa的依赖。
首先,增加service层公共接口,
package com.xkx.dafangzi.core.foundation.service;import com.xkx.dafangzi.core.foundation.entity.BaseEntity;/** * Created by Administrator on 2017/8/3. */public interface IBaseService<T extends BaseEntity, PK> { public T saveOrUpdate(T t); public int deleteById(PK id); public int deleteByIds(String ids); public T getEntityById(PK id);}
然后创建接口ISystemConfigService继承自IBaseService。创建SystemConfigServiceImpl类实现ISystemConfigService,在实现类中引用ISystemConfigDao,该类继承子spring中的JpaRepository,在实现中就可以使用spring提供的已有方法了。
package com.xkx.dafangzi.system.service.impl;import com.xkx.dafangzi.core.foundation.service.impl.BaseServiceImpl;import com.xkx.dafangzi.system.dao.ISystemConfigDao;import com.xkx.dafangzi.system.entity.SystemConfigEntity;import com.xkx.dafangzi.system.service.ISystemConfigService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * Created by pc on 2017/8/13. */@Servicepublic class SystemConfigServiceImpl implements ISystemConfigService { @Autowired private ISystemConfigDao systemConfigDao; @Override public SystemConfigEntity saveOrUpdate(SystemConfigEntity systemConfigEntity) { return systemConfigDao.saveAndFlush(systemConfigEntity); } @Override public int deleteById(Long id) { return 0; } @Override public int deleteByIds(String ids) { return 0; } @Override public SystemConfigEntity getEntityById(Long id) { return systemConfigDao.findOne(id); }}
在fangzi-system中添加实体SystemConfigEntity,
package com.xkx.dafangzi.system.entity;import com.xkx.dafangzi.core.foundation.entity.BaseEntity;import javax.persistence.*;import java.io.Serializable;/** * Created by pc on 2017/8/9. */@Entity@Table(name="dafangzi_system_config")public class SystemConfigEntity extends BaseEntity { private String systemId; private String systemName; public String getSystemId() { return systemId; } public void setSystemId(String systemId) { this.systemId = systemId; } public String getSystemName() { return systemName; } public void setSystemName(String systemName) { this.systemName = systemName; }}
最后,添加配置文件,
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/dafangzispring.datasource.username=rootspring.datasource.password=rootspring.jpa.database=mysqlspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategyspring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
并且增加Application作为启动的入口,
package com.xkx.dafangzi.web.test.controller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.ComponentScan;/** * Created by pc on 2017/7/26. *///@SpringBootApplication@ComponentScan(basePackages = "com.xkx.dafangzi")@EnableConfigurationProperties@EnableAutoConfigurationpublic class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}
运行后,访问SystemConfigController中的请求,即可自动生成数据库表。
详细代码可参看https://github.com/xukexin/dafangzi
阅读全文
0 0
- (五)spring-boot中使用spring-data-jpa(hibernate实现)
- Spring Boot中使用Spring-data-jpa
- Spring Boot中使用Spring-data-jpa
- spring boot中使用spring-data-jpa
- Spring Boot中使用Spring-data-jpa
- Spring Boot中使用Spring-data-jpa
- spring boot(五):spring data jpa的使用
- spring boot(五):spring data jpa的使用
- spring boot(五):spring data jpa的使用
- Spring Boot系列(五):spring data jpa的使用
- spring boot(五):spring data jpa的使用
- spring boot(五):spring data jpa的使用
- spring boot(五):spring data jpa的使用
- 转载:Spring Boot (五):spring data jpa的使用
- Spring boot 中Spring data JPA的应用(一)
- spring data jpa demo(hibernate实现的jpa)
- Spring-boot中使用Spring-data-jpa操作DB
- spring data Jpa hibernate实现
- Pagodas HDU
- Java并发编程:volatile关键字解析
- python多线程下载图片
- 【51Nod1354】选数字
- 使用GitHub上传项目
- (五)spring-boot中使用spring-data-jpa(hibernate实现)
- statsvn查看代码情况
- Linux主分区、扩展分区和逻辑分区
- java接口的继承问题
- 使用GraphFrame 的shortestPaths API 求最短路径
- XCode调试填写命令行参数
- POJ 3252 Round numbers
- JAVA基础——深入了解JVM。
- jsp动作标识