标签:create 增删改查 classname end 2.x params man generated strong
前言:
基于spring framework 4.x或spring boot 1.x开发环境
务必注意以下版本问题:
Spring framework4.x(Spring boot1.x)对应spring-data1.x
Spring framework5.x(Spring boot2.x)对应spring-data2.x
一、依赖
需要jpa 1.x,hibernate 5.x,spring-data-commons,spring-data-jpa
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>
-
二、环境配置
注意两个扫描器(一个是po实体类扫描,还有一个是dao层接口扫描)
-
<?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"/>
-
-
三、实体类和Repository接口
(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;
-
-
-
四、总结
1、添加依赖(添加spring-data及spring-data-jpa依赖包)
2、配置jpa环境(配置dao扫描路径和实体类扫描路径)
3、编写实体类和dao层接口(如果是简单的单表增删改查操作,直接继承CrudRepository接口即可,基本不需要写代码)
jpa单表操作基本无可挑剔,涉及多表操作需要手写hql语句或jpa
实体类是用工具生成的,所以实际上只需要写一个dao接口即可
spring-data详解之spring-data-jpa:简单三步快速上手spring-data-jpa开发
标签:create 增删改查 classname end 2.x params man generated strong
原文地址:https://www.cnblogs.com/eguid/p/9667152.html