》#千鋒逆戰(zhàn)#
如果數(shù)據(jù)庫(kù)中有表粗卜,我們可以逆向工程,如果數(shù)據(jù)庫(kù)中沒(méi)有表纳击,但是我們有實(shí)體類(lèi)续扔,我們可以使用正向工程
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.28</version>
</dependency>
<!--
添加spring-data-jpa的依賴(lài)
-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.0.RELEASE</version>
</dependency>
<!--
spring-data-jpa依賴(lài)于hibernate-entitymanager
-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
</dependencies>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
user=root
pass=123456
spring-jpa.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<context:component-scan base-package="com.qfedu.service"/>
<context:component-scan base-package="com.qfedu.dao"/>
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driver}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pass}"/>
</bean>
<!--
配置 HibernateJpaVendorAdapter,用來(lái)分別設(shè)置數(shù)據(jù)庫(kù)的方言和是否顯示sql語(yǔ)句
-->
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="adapter">
<!--<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQL57InnoDBDialect"/>
<property name="showSql" value="true"/>
</bean>
<!--
配置EntityManagerFactoryBean
-->
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
<property name="dataSource" ref="ds"/>
<property name="packagesToScan" value="com.qfedu.entity"/>
<property name="jpaVendorAdapter" ref="adapter"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">true</prop>
<!--
使用hibernate.hbm2ddl.auto屬性來(lái)根據(jù)需要?jiǎng)討B(tài)創(chuàng)建數(shù)據(jù)庫(kù)的表結(jié)構(gòu)
create:表示啟動(dòng)的時(shí)候先drop焕数,再create
create-drop: 也表示創(chuàng)建纱昧,只不過(guò)再系統(tǒng)關(guān)閉前執(zhí)行一下drop
update: 這個(gè)操作啟動(dòng)的時(shí)候會(huì)去檢查schema是否一致,如果不一致會(huì)做scheme更新
validate: 啟動(dòng)時(shí)驗(yàn)證現(xiàn)有schema與你配置的hibernate是否一致堡赔,如果不一致就拋出異常识脆,并不做更新
-->
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<!--
配置jpa的事務(wù)管理器
-->
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="jtx">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<!--配置事務(wù)驅(qū)動(dòng)-->
<tx:annotation-driven proxy-target-class="false" transaction-manager="jtx"/>
<jpa:repositories base-package="com.qfedu.dao" entity-manager-factory-ref="emf" transaction-manager-ref="jtx"/>
</beans>
entity
package com.qfedu.entity;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Table(name = "tbl_emp")
public class Emp {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "eid")
private int eid;
/**
* Column注解的各個(gè)屬性值說(shuō)明:
* name用來(lái)指定該屬性所對(duì)應(yīng)的列名,默認(rèn)與屬性名一致
* unique為true代表該屬性生成的字段唯一,默認(rèn)不唯一
* nullable為false不允許為空灼捂,默認(rèn)運(yùn)行為空
* length可以給字段指定長(zhǎng)度离例,默認(rèn)為255
*/
@Column(name = "first_name",nullable = false,unique = true,length = 20)
private String firstName;
@Column(name = "last_name")
private String lastName;
private double Salary;
}
dao
package com.qfedu.dao;
import com.qfedu.entity.Emp;
import org.hibernate.boot.model.source.spi.JpaCallbackSource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
@Repository
public interface IEmpDao extends JpaRepository<Emp, Serializable> {
}
service
package com.qfedu.service;
import com.qfedu.entity.Emp;
public interface IEmpService {
void saveEmp(Emp emp);
}
service.impl
package com.qfedu.service.impl;
import com.qfedu.dao.IEmpDao;
import com.qfedu.entity.Emp;
import com.qfedu.service.IEmpService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class EmpServiceImpl implements IEmpService {
@Resource
private IEmpDao empDao;
@Override
public void saveEmp(Emp emp) {
empDao.saveAndFlush(emp);
}
}
test
package com.qfedu.test;
import com.qfedu.entity.Emp;
import com.qfedu.service.IEmpService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jpa.xml")
public class TestEmp {
@Resource
private IEmpService empService;
@Test
public void saveEmp() {
Emp emp = new Emp();
emp.setFirstName("豬");
emp.setLastName("八戒");
emp.setSalary(200000);
empService.saveEmp(emp);
}
}