前言
本章讲解Spring整合SpringData JPA的相关知识
方法
1.概念
SpringData JPA是SpringData项目下的一个子模块,提供了一套基于JPA标准的操作数据库的简化方案,其底层采用Hibernate JPA来实现,但是操作起来要比Hibernate JPA还要简单!
2.Spring整合SpringData JPA
1)修改pom文件,引入相关jar的坐标
<!-- springData JPA 相关jar -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.10.RELEASE</version>
</dependency>
2)修改applicationContext.xml文件
加入jpa命名空间:
<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: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/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/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">
配置dao接口扫描:
<!-- 配置SpringData JPA组件扫描 -->
<jpa:repositories base-package="cn.edu.ccut.dao"></jpa:repositories>
3)去掉dao接口实现类,修改dao接口
package cn.edu.ccut.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import cn.edu.ccut.bo.Users;
/**
* 继承JpaRepository<T, ID>接口
* T传入当前操作的实体类,ID传入该类的主键类型
* @author jwang
*
*/
public interface UserDAO extends JpaRepository<Users, Integer>{
}
可能有人会问了,你不写接口方法的吗?实际上基本的操作都已经封装到了JpaRepository接口了,我们可以很轻松的使用:
4)编写测试类测试
package cn.edu.ccut.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.edu.ccut.bo.Users;
import cn.edu.ccut.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class UserTest {
@Autowired
private UserService userService;
@Test
public void testFindUser() {
Integer id = 1;
Users user = userService.getUserById(id);
System.out.println(user);
}
@Test
public void testDeleteUser() {
Integer id = 8;
userService.deleteUser(id);
}
@Test
public void testAddUser() {
Users user = new Users();
user.setUsername("jjj");
user.setPassword("www");
userService.addUser(user);
}
@Test
public void testUpdateUser() {
Users user = new Users();
user.setId(8);
user.setUsername("蓝月");
user.setPassword("www");
userService.updateUser(user);
}
}
这里说个坑:
之前我们配置事务管理器的时候,我这里的bean的id写的是tx
那么我运行测试类的时候会报这个错:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' available: No matching PlatformTransactionManager bean found for qualifier 'transactionManager' - neither qualifier match nor bean name match!
这个错搞了我半天,其实我们重点关注标红的部分。
大致的意思是找不到名称为transactionManager的bean,无法进行事务处理!
然后我发现我的JPA事务管理器设置成了tx,我们将其修改为transactionManager即可恢复正常啦!