spring整合jpa
原创
©著作权归作者所有:来自51CTO博客作者morris131的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.1. Spring整合jpa
1.1.1. 新建工程
1.1.2. 引入jar包
1. 所使用hibernat版本:hibernate-release-4.3.7.Final,Lib\jpa目录和lib\required目录下所有的jar包,
2. mysql驱动包
3. spring版本spring-framework-4.1.3.RELEASE,libs下所有的jar包
4. tomcat jdbc连接池 tomcat-jdbc-7.0.53.jar和tomcat-juli-7.0.53.jar
5. 日志包commons-logging-1.1.3.jar
6. aopalliance-1.0.jar
7. 。。。。。。
1.1.3. 持久化配置文件persistence.xml
persistence.xml |
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!-- persistence-unit
name指定持久化单元的名字
transaction-type指定事务类型为本地事务
-->
<persistence-unit name="jpaDemo" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
|
1.1.4. 实体类Person.xml
Person.xml |
package com.morris.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person( String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
|
1.1.5. Dao接口
IpersonDao.Java |
package com.morris.dao.inter;
import com.morris.entity.Person;
public interface IPersonDao {
void save(Person person);
}
|
1.1.6. Dao接口具体实现类
PersonDaoImpl.java |
package com.morris.dao.impl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Repository;
import com.morris.dao.inter.IPersonDao;
import com.morris.entity.Person;
@Repository("personDao")
public class PersonDaoImpl implements IPersonDao {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public void save(Person person) {
entityManager.persist(person);
}
}
|
1.1.7. Service接口
IpersonService.java |
package com.morris.service.inter;
import com.morris.entity.Person;
public interface IPersonService {
void save(Person person);
}
|
1.1.8. Service接口具体实现类
PersonServiceImpl.java |
package com.morris.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.morris.dao.inter.IPersonDao;
import com.morris.entity.Person;
import com.morris.service.inter.IPersonService;
@Service("personService")
public class PersonServiceImpl implements IPersonService {
@Autowired
private IPersonDao personDao;
public void save(Person person) {
personDao.save(person);
}
}
|
1.1.9. Spring配置文件
Spring.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:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mysql" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="5" />
<property name="minIdle" value="5" />
<property name="maxIdle" value="30" />
<property name="maxActive" value="100" />
<property name="maxWait" value="1000" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="jpaDemo" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
</bean>
</property>
</bean>
<!-- 定义扫描根路径,不使用默认的扫描方式 -->
<context:component-scan base-package="com.morris"
use-default-filters="false">
<!-- 扫描符合@Service @Repository的类 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="com.morris.dao"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" />
</beans>
|
1.1.10. 测试文件
TestPerson.java |
package com.morris.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.morris.entity.Person;
import com.morris.service.inter.IPersonService;
public class TestPerson {
@Test
public void save() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
"personService");
personService.save(new Person("morris131",131));
}
}
|
1.1.11. 结果
后台打印sql语句 |
Hibernate: insert into Person (age, name) values (?, ?)
|