前言:php
基於spring framework 4.x或spring boot 1.x開發環境java
務必注意如下版本問題:
Spring framework4.x(Spring boot1.x)對應spring-data1.x
spring
Spring framework5.x(Spring boot2.x)對應spring-data2.xapi
1、依賴maven
須要jpa 1.x,hibernate 5.x,spring-data-commons,spring-data-jpaide
maven方式:工具
-
-
<groupId>org.hibernate.javax.persistence</groupId>
-
<artifactId>hibernate-jpa-2.1-api</artifactId>
-
<version>1.0.2.Final</version>
-
-
-
<groupId>org.hibernate</groupId>
-
<artifactId>hibernate-core</artifactId>
-
<version>5.2.16.Final</version>
-
-
-
<groupId>org.hibernate</groupId>
-
<artifactId>hibernate-entitymanager</artifactId>
-
<version>5.2.16.Final</version>
-
-
-
<groupId>org.springframework.data</groupId>
-
<artifactId>spring-data-jpa</artifactId>
-
<version>1.11.11.RELEASE</version>
-
2、環境配置post
注意兩個掃描器(一個是po實體類掃描,還有一個是dao層接口掃描)fetch
-
<?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">
-
-
-
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
-
<property name="driverClassName" value="${jdbc.driverClassName}" />
-
<property name="url" value="${jdbc.url}" />
-
<property name="username" value="${jdbc.username}" />
-
<property name="password" value="${jdbc.password}" />
-
<property name="maxActive" value="${jdbc.maxActive}" />
-
<property name="initialSize" value="${jdbc.initialSize}" />
-
<property name="maxWait" value="${jdbc.maxWait}" />
-
<property name="maxIdle" value="${jdbc.maxIdle}" />
-
<property name="minIdle" value="${jdbc.minIdle}" />
-
<property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
-
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
-
<property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
-
<property name="validationQuery" value="${jdbc.validationQuery}"/>
-
<property name="validationQueryTimeout" value="${jdbc.validationQueryTimeout}" />
-
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
-
<property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}" />
-
-
-
<property name="poolPreparedStatements" value="false" />
-
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
-
-
-
<property name="filters" value="stat,wall"/>
-
<property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />
-
-
-
-
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
-
<property name="dataSource" ref="dataSource" />
-
-
<property name="packagesToScan" value="cc.eguid.xxx.pojo.po" />
-
<property name="persistenceProvider">
-
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
-
-
<property name="jpaVendorAdapter">
-
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
-
<property name="generateDdl" value="false" />
-
<property name="database" value="MYSQL" />
-
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
-
<property name="showSql" value="true" />
-
-
-
<property name="jpaDialect">
-
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
-
-
<property name="jpaPropertyMap">
-
-
<entry key="hibernate.query.substitutions" value="true 1, false 0" />
-
<entry key="hibernate.default_batch_fetch_size" value="16" />
-
<entry key="hibernate.max_fetch_depth" value="2" />
-
<entry key="hibernate.generate_statistics" value="true" />
-
<entry key="hibernate.bytecode.use_reflection_optimizer" value="true" />
-
<entry key="hibernate.cache.use_second_level_cache" value="false" />
-
<entry key="hibernate.cache.use_query_cache" value="false" />
-
-
-
-
-
-
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
-
-
-
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
-
<property name="entityManagerFactory" ref="entityManagerFactory"/>
-
-
-
-
<jpa:repositories base-package="cc.eguid.xxx.dao" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
-
-
3、實體類和Repository接口ui
(1)編寫dao層接口(不需實現類,spring-data-jpa會自動生成實現類)
-
import org.springframework.data.repository.CrudRepository;
-
-
-
-
-
-
-
public interface UserRepository extends CrudRepository<GameUserinfo, Integer>{
-
-
GameUserinfo findByUsername(String username);
-
-
(2)自動生成的ORM映射Entity(用JPA生成工具生成的)
-
-
-
-
-
-
-
@NamedQuery(name="Userinfo.findAll", query="SELECT g FROM Userinfo g")
-
public class Userinfo extends BaseEntity {
-
private static final long serialVersionUID = 1L;
-
-
-
@GeneratedValue(strategy=GenerationType.AUTO)
-
@Column(unique=true, nullable=false)
-
-
-
private Timestamp createtime;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@JoinColumn(name="userid", nullable=false)
-
-
-
@JoinColumn(name="roleid", nullable=false)
-
-
-
private List<roleinfo> roleinfos;
-
-
-
-
-
public Integer getUserid() {
-
-
-
-
public void setUserid(Integer userid) {
-
-
-
-
public Timestamp getCreatetime() {
-
-
-
-
public void setCreatetime(Timestamp createtime) {
-
this.createtime = createtime;
-
-
-
public String getNickname() {
-
-
-
-
public void setNickname(String nickname) {
-
this.nickname = nickname;
-
-
-
public String getPassword() {
-
-
-
-
public void setPassword(String password) {
-
this.password = password;
-
-
-
-
-
-
-
public void setType(int type) {
-
-
-
-
public String getUsername() {
-
-
-
-
public void setUsername(String username) {
-
this.username = username;
-
-
-
public List<Roleinfo> getRoleinfos() {
-
-
-
-
public void setRoleinfos(List<Roleinfo> roleinfos) {
-
this.roleinfos = roleinfos;
-
-
-
4、總結
一、添加依賴(添加spring-data及spring-data-jpa依賴包)
二、配置jpa環境(配置dao掃描路徑和實體類掃描路徑)
三、編寫實體類和dao層接口(若是是簡單的單表增刪改查操做,直接繼承CrudRepository接口便可,基本不須要寫代碼)
jpa單表操做基本無可挑剔,涉及多表操做須要手寫hql語句或jpa
實體類是用工具生成的,因此實際上只須要寫一個dao接口便可