好久沒寫了,今天更一篇。在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
更多有趣,好玩的信息請關注我的微信公衆號!