使用 Spring Data JPA 进行持久层开发需要的四个步骤:
第四步:创建service层 并创建UserService
1.配置 Spring 整合 JPA
第一步:导入所需的jar包
第二步:创建一个Spring的applicationContext.xm文件来整合
在 Spring 配置文件中配置 Spring Data,让 Spring 为声明的接口创建代理对象。配置了 <jpa:repositories> 后,Spring 初始化容器时将会扫描 base-package 指定的包目录及其子目录,为继承 Repository 或其子接口的接口创建代理对象,并将代理对象注册为 Spring Bean,业务层便可以通过 Spring 自动封装的特性来直接使用该对象。
1)创建一个db.properties文件来防数据库的连接信息
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis
db.username=root
db.password=root
2)创建一个applicationContext.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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--pezhi-->
<context:component-scan base-package="org.peter.service"/>
<context:property-placeholder location="classpath:db.properties"/>
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!--配置JPA,,替代之前JPA中的persistence.xml文件-->
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
<!--配置JPA的实现类的适配器-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<!--配置所有Model的位置-->
<property name="packagesToScan" value="org.peter.model"></property>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!--配置Spring Data,base-package表示要扫描的包-->
<jpa:repositories base-package="org.peter.dao" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
第三步:创建一个model层 user
package org.peter.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* Created by Lenovo on 2017/7/28.
*/
@Entity(name = "t_user")
public class User {
private Long id;
private String username;
private String password;
private String address;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", address='" + address + '\'' +
'}';
}
}
这里创建的model其实就是JPA里创建model是一样的
第四步:创建dao层,并且创建UserDao
声明持久层的接口,该接口继承 Repository,Repository 是一个标记型接口,它不包含任何方法,如必要,Spring Data 可实现 Repository 其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法。
package org.peter.dao;
import org.peter.model.User;
import org.springframework.data.repository.Repository;
/**
* Created by Lenovo on 2017/7/28.
*/
public interface UserDao extends Repository<User,Long>{
User findById(Long id);
}
第四步:创建service层 并创建UserService
package org.peter.service;
import org.peter.dao.UserDao;
import org.peter.model.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* Created by Lenovo on 2017/7/28.
*/
@Service
@Transactional
public class UserService {
@Resource
UserDao userDao;
public User findById(Long id){
User user = userDao.findById(id);
return user;
}
}
第五步:创建一个main类做测试
package org.peter;
import org.junit.Test;
import org.peter.model.User;
import org.peter.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Lenovo on 2017/7/28.
*/
public class main {
private ClassPathXmlApplicationContext ctx;
private UserService userService;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
userService = ctx.getBean(UserService.class);
}
@Test
public void findById(){
User user = userService.findById(1l);
System.out.println(user);
}
}
代码资料:http://download.csdn.net/detail/strive_peter/9913801