首页 > 代码库 > spring整合jpa
spring整合jpa
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 |
<?xmlversion="1.0"encoding="UTF-8"?> <persistencexmlns="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-unitname="jpaDemo"transaction-type="RESOURCE_LOCAL"> <properties> <propertyname="hibernate.dialect"value="org.hibernate.dialect.MySQL5Dialect"/> <propertyname="hibernate.connection.driver_class"value="com.mysql.jdbc.Driver"/> <propertyname="hibernate.connection.username"value="root"/> <propertyname="hibernate.connection.password"value="root"/> <propertyname="hibernate.connection.url"value="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8"/> <propertyname="hibernate.hbm2ddl.auto"value="update"/> <propertyname="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 |
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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">
<!-- 数据源 --> <beanid="dataSource"class="org.apache.tomcat.jdbc.pool.DataSource"> <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/> <propertyname="url"value="jdbc:mysql://localhost:3306/mysql"/> <propertyname="username"value="root"/> <propertyname="password"value="root"/> <propertyname="initialSize"value="5"/> <propertyname="minIdle"value="5"/> <propertyname="maxIdle"value="30"/> <propertyname="maxActive"value="100"/> <propertyname="maxWait"value="1000"/> </bean>
<beanid="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <propertyname="dataSource"ref="dataSource"/> <propertyname="persistenceXmlLocation"value="classpath:META-INF/persistence.xml"/> <propertyname="persistenceUnitName"value="jpaDemo"/> <propertyname="jpaVendorAdapter"> <beanclass="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <propertyname="showSql"value="true"/> <propertyname="generateDdl"value="false"/> </bean> </property> </bean>
<!-- 定义扫描根路径,不使用默认的扫描方式 --> <context:component-scanbase-package="com.morris" use-default-filters="false"> <!-- 扫描符合@Service @Repository的类 --> <context:include-filtertype="annotation" expression="org.springframework.stereotype.Service"/> <context:include-filtertype="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan>
<tx:annotation-driventransaction-manager="transactionManager"/>
<beanid="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager"> <propertyname="entityManagerFactory"ref="entityManagerFactory"/> </bean>
<jpa:repositoriesbase-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");
IPersonService personService = (IPersonService) applicationContext.getBean("personService");
personService.save(new Person("morris131",131));
}
}
|
1.1.11. 结果
后台打印sql语句 |
Hibernate: insert into Person (age, name) values (?, ?) |
spring整合jpa