Spring之路

Spring和SpringDataJpa整合详解

2019-07-24  本文已影响1人  逍遥天扬

Spring和SpringDataJpa整合详解

一、概述

SpringBoot操作数据库有多种方式,如

当然还有其他中间件,主流使用的就是Mybatis和Spring-data-jpa。

首发地址:

二、环境配置

本文假设你已经引入Spring必备的一切了,已经是个Spring项目了,如果不会搭建,可以打开这篇文章看一看《Spring和Spring Mvc 5整合详解》

2.1 maven依赖

使用Spring-data-jpa需要引入spring-data-jpa,因为是非Springboot项目,我们不能通过starter引入,需要引入spring-data-jpa、javax.transaction-api、hibernate-core。

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.pomit</groupId>
        <artifactId>SpringWork</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>SpringDataJpa</artifactId>
    <packaging>jar</packaging>
    <name>SpringDataJpa</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.0.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.17.Final</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>jboss-transaction-api_1.2_spec</artifactId>
                    <groupId>org.jboss.spec.javax.transaction</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <finalName>SpringDataJpa</finalName>
    </build>
</project>

父模块可以在https://www.pomit.cn/spring/SpringWork/pom.xml获取。

2.2 Spring配置

需要配置数据源、jdbcTemplate、entityManagerFactory、transactionManager和jpa:repositories。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop 
                    http://www.springframework.org/schema/aop/spring-aop.xsd
                    http://www.springframework.org/schema/context      
                    http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/data/jpa
                    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:annotation-config />
    <context:component-scan base-package="cn.pomit.springwork.springdatajpa">
    </context:component-scan>

    <bean id="annotationPropertyConfigurerJpaLock"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${db.dirverClass}"></property>
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />

        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxTotal" value="20" />

        <property name="validationQuery" value="SELECT 1" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
    </bean>

    <!-- jdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="cn.pomit.springwork.springdatajpa.domain"></property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect">
            </property>
            </bean>
        </property>
    </bean>
    <jpa:repositories base-package="cn.pomit.springwork.springdatajpa.dao" />
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

这里面,需要注意的是:

db.properties中存放数据库的地址端口等连接信息。

db.properties:

db.url=jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
db.username=cff
db.password=123456
db.dirverClass=com.mysql.cj.jdbc.Driver

三、数据访问Dao层

我们直接使用Spring-data-jpa, 一切都会变的特别简单。Spring-data-jpa支持快速查询,也支持@Query自定义查询,只需要新建接口继承JpaRepository或者CrudRepository等接口即可。

UserInfoDao :

package cn.pomit.springwork.springdatajpa.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import cn.pomit.springwork.springdatajpa.domain.UserInfo;

@Repository
public interface UserInfoDao extends JpaRepository<UserInfo, String> {
    UserInfo findByUserName(String userName);
}

注意加上@Repository注解。实体要加上@Entity和@Table注解。

四、测试业务逻辑

我们定义一个service和web接口来做测试。

UserInfoService :

package cn.pomit.springwork.springdatajpa.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.pomit.springwork.springdatajpa.dao.UserInfoDao;
import cn.pomit.springwork.springdatajpa.domain.UserInfo;

@Service
public class UserInfoService {
    @Autowired
    UserInfoDao userInfoDao;
    public UserInfo getUserInfoByUserName(String userName){
        return userInfoDao.findByUserName(userName);
    }
}

SpringDataJpaRest:

package cn.pomit.springwork.springdatajpa.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.pomit.springwork.springdatajpa.domain.UserInfo;
import cn.pomit.springwork.springdatajpa.service.UserInfoService;

@RestController
@RequestMapping("/springdatajpa")
public class SpringDataJpaRest {

    @Autowired
    UserInfoService userInfoService;
    
    @RequestMapping(value = "/test/{name}", method = { RequestMethod.GET })
    public UserInfo testMybatis(@PathVariable("name") String name) {
        return userInfoService.getUserInfoByUserName(name);
    }
}

五、过程中用到的实体

UserInfo:


详细完整的实体,可以访问品茗IT-博客《Spring和SpringDataJpa整合详解》进行查看

全部代码可以在Spring组件化构建https://www.pomit.cn/java/spring/spring.html中的SpringDataJpa组件中查看,并下载。

快速构建项目

Spring组件化构建

SpringBoot组件化构建

SpringCloud服务化构建

喜欢这篇文章么,喜欢就加入我们一起讨论Spring技术吧!


品茗IT交流群
上一篇下一篇

猜你喜欢

热点阅读