1.Demo整体架构
2.pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringDataJpa</groupId>
<artifactId>SpringDataJpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name />
<description />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.0</slf4j.version>
<junit.version>4.10</junit.version>
<spring.version>3.2.5.RELEASE</spring.version>
<hibernate.version>4.2.0.Final</hibernate.version>
</properties>
<!-- 指定Maven仓库 -->
<repositories>
<!-- 官方Maven仓库 -->
<repository>
<id>maven</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 数据库依赖 -->
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- oracle11g数据库驱动 <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>${oracle.version}</version> </dependency> -->
<!-- <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> -->
<dependency>
<groupId>com.kenai.nbpwr</groupId>
<artifactId>org-apache-commons-dbcp</artifactId>
<version>1.2.2-201002241055</version>
</dependency>
<!-- spring3 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springdatajpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.0</version>
</dependency>
<!-- hibernate4 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
/**3.Repository
*
* Copyright: Copyright (c) 2014 Asiainfo-Linkage
*
* bean@date:2014-3-25
* @ClassName: User.java
* @Description: 用户表映射类
*
* @version: v1.0.0上午9:45:25eleven
* @author: elevenSpringDataJpa
*
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 新建
*/
package com.xr.bean;
import java.io.Serializable;
@Entity
@Table(name = "_user")
public class User implements Serializable {
private static final long serialVersionUID = 4284572555216108008L;
@Id
private String id;
private String name;
private String pwd;
private int age;
public User() {
}
public User(String id, String name, String pwd, int age) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", age=" + age + "]";
}
}
/**
*
* Copyright: Copyright (c) 2014 Asiainfo-Linkage
*
* repository@date:2014-3-25
* @ClassName: UserRepository.java
* @Description: 用户Repository
*
* @version: v1.0.0上午9:51:30eleven
* @author: elevenSpringDataJpa
*
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 新建
*/
package com.xr.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, String>{
}
4.Service
/**
*
* Copyright: Copyright (c) 2014 Asiainfo-Linkage
*
* service@date:2014-3-25
* @ClassName: UserService.java
* @Description: 用户服务接口
*
* @version: v1.0.0上午9:54:56eleven
* @author: elevenSpringDataJpa
*
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 新建
*/
package com.xr.service;
import com.xr.bean.User;
public interface UserService {
/**
*
* @Title: addUser
* @Description: TODO(添加用户)
* @return: void
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
void addUser(User user);
}
/** * * Copyright: Copyright (c) 2014 Asiainfo-Linkage * * service.impl@date:2014-3-25 * @ClassName: UserServiceImpl.java * @Description: 用户服务接口实现类 * * @version: v1.0.0上午9:56:48eleven * @author: elevenSpringDataJpa * * Modification History: * Date Author Version Description * ---------------------------------------------------------* * 2014-3-25 eleven v1.0.0 新建 */package com.xr.service.impl;import javax.annotation.Resource;@Service(value = "userService")public class UserServiceImpl implements UserService { @Resource private UserRepository userRepository; public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } /* (非 Javadoc) * <p>Title: addUser</p> * <p>Description: </p> * @see service.UserService#addUser() */ @Override public void addUser(User user) { userRepository.save(user); }}
5.Test
/**
*
* Copyright: Copyright (c) 2014 Asiainfo-Linkage
*
* com.xr.service@date:2014-3-25
* @ClassName: UserServiceTest.java
* @Description: 用户服务接口测试类
*
* @version: v1.0.0上午10:12:41eleven
* @author: elevenSpringDataJpa
*
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 新建
*/
package com.xr.service;
import java.util.UUID;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserServiceTest {
@Resource
private UserService userService;
@Test
public void test() {
User user = new User();
user.setId(UUID.randomUUID().toString());
user.setName("springdatajpa");
user.setPwd("123456");
userService.addUser(user);
}
}
6.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:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 注解组件扫描 -->
<context:component-scan base-package="com.xr">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 开启注解事务只对当前配置文件有效 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<jpa:repositories base-package="com.xr" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"></jpa:repositories>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.xr.bean" />
<property name="persistenceProvider">
<bean class="org.hibernate.ejb.HibernatePersistence" />
</property>
<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.MySQL5InnoDBDialect" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaPropertyMap">
<map>
<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" />
</map>
</property>
</bean>
<!--事务管理器配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/springdatajpa?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
</beans>