jpa学习2---jpa和spring结合使用半注解方式
2017-03-05 19:19
676 查看
jpa学习2---jpa和spring结合使用半注解方式
1.工程结构图:
2.新建java工程-----导入jar包
建立实体类:使用注解
1.工程结构图:
2.新建java工程-----导入jar包
建立实体类:使用注解
package com.zl.sjpa.spring.entities; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Table(name="JPA_PERSONS") @Entity public class Person { private Integer id; private String lastName; private String email; private int age; @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="LAST_NAME") public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }3.service层
package com.zl.sjpa.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.zl.sjpa.dao.PersonDao; import com.zl.sjpa.spring.entities.Person; @Service public class PersonService { @Autowired private PersonDao personDao; @Transactional public void savePersons(Person p1, Person p2){ personDao.save(p1); // int i = 10 / 0; personDao.save(p2); } }4.dao层:
package com.zl.sjpa.dao; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import com.zl.sjpa.spring.entities.Person; @Repository public class PersonDao { //如何获取到和当前事务关联的 EntityManager 对象呢 ? //通过 @PersistenceContext 注解来标记成员变量! @PersistenceContext private EntityManager entityManager; public void save(Person person){ entityManager.persist(person); } }5.db.properties数据库参数
jdbc.user=root jdbc.password=tiger jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc\:mysql\:///spring6.spring中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: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-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.zl.sjpa"></context:component-scan> <!-- 配置 C3P0 数据源 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <!-- 配置其他属性 --> </bean> <!-- 配置 EntityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置 JPA 提供商的适配器. 可以通过内部 bean 的方式来配置 --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <!-- 配置实体类所在的包 --> <property name="packagesToScan" value="com.zl.sjpa.spring.entities"></property> <!-- 配置 JPA 的基本属性. 例如 JPA 实现产品的属性 --> <property name="jpaProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> < b9dc prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 配置 JPA 使用的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!-- 配置支持基于注解是事务配置 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
相关文章推荐
- Spring学习笔记(17)----使用Spring注解方式管理事务
- jpa学习5--spring data jpa 中的JpaRepository 使用 @Query 注解
- Spring学习历程---使用注解方式入门AOP
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- 学习《Spring实战(第3版)》扎记1:Spring使用注解配置Bean的方式
- 一篇总结的很好的Spring data jpa 文章,里面包含多种查询方式,可以结合api使用
- Spring学习笔记(15)----使用Spring注解方式管理事务
- Spring学习(九)使用ioc注解方式配置bean
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- Spring学习-13:Spring的XML和注解的结合使用
- Spring学习(九)使用ioc注解方式配置bean
- spring框架的学习(五)——Spring使用AspectJ进行AOP的开发:注解方式
- Spring视频学习(九)使用Spring注解方式管理事务与传播行为详解
- Spring学习第三天——Spring MVC,使用注解方式,intellij的代码说明,ajax,文件的上传下载
- Spring集成Hibernate,使用JPA注解方式,新增数据无法提交
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- spring学习笔记(12)——使用注解方式配置bean
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 使用Spring的注解方式实现AOP