好久没写了,今天更一篇。在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.
*/
@Service
public 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.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dafangzi
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
spring.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
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
运行后,访问SystemConfigController中的请求,即可自动生成数据库表。
详细代码可参看https://github.com/xukexin/dafangzi
更多有趣,好玩的信息请关注我的微信公众号!