设为首页 收藏本站
查看: 656|回复: 0

[经验分享] spring+openJPA+DB2环境搭建,程序开发及测试

[复制链接]

尚未签到

发表于 2016-11-17 02:35:17 | 显示全部楼层 |阅读模式
  1.导包(这里省略)
  2.环境搭建
  方法一:数据源配置在persistence.xml中
  persistence.xml(位于META-INF下)

<?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="openJPAUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.iteye.domain.User</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:db2://localhost:50000/USER" />
<property name="openjpa.ConnectionDriverName" value="com.ibm.db2.jcc.DB2Driver" />
<property name="openjpa.Log" value="SQL=TRACE" />
<property name="openjpa.ConnectionUserName" value="username" />
<property name="openjpa.ConnectionPassword" value="password" />
<property name="openjpa.jdbc.TransactionIsolation" value="read-committed" />
<property name="openjpa.jdbc.Schema" value="USER" />
</properties>
</persistence-unit>
</persistence>
  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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.iteye.*" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="openJPAUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.iteye.services.*.*(..))" id="allService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allService" />
</aop:config>
</beans>
  方法二:数据源配置在applicationContext.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="openJPAUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.iteye.User</class>
<properties>
<property name="openjpa.Log" value="SQL=TRACE" />
<property name="openjpa.jdbc.TransactionIsolation" value="read-committed" />
<property name="openjpa.jdbc.Schema" value="USER" />
</properties>
</persistence-unit>
</persistence>
  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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.iteye.*" />
<context:property-placeholder location="classpath:userDataSource.properties" />
<!-- 这里使用的是DBCP数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</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="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.iteye.services.*.*(..))" id="allService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allService" />
</aop:config>
</beans>
  3.获取entityManager
  方法一:使用注解@PersistenceUnit

public class UserDao {
//注意这里的unitName要和配置文件里的unitName一致
@PersistenceUnit(unitName = "openJPAUnit")
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
public EntityManagerFactory getEntityManagerFactory() {
return this.entityManagerFactory;
}
public EntityManager getEntityManager() {
if (entityManager == null) {
entityManager = entityManagerFactory.createEntityManager();
}
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
  方法二:直接获取entityManager

    public EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("openJPAUnit");
EntityManager entityManager = emf.createEntityManager();
//将EntityManager强转成OpenJPAEntityManager
//        OpenJPAEntityManager openJPAEntityManager = OpenJPAPersistence.cast(entityManager);
return entityManager;
}
  4.使用entityManager来操作数据库

public User saveUser(User user) {
if (user == null) {
throw new IllegalArgumentException("user can not be empty.");
}
getEntityManager().getTransaction().begin();
getEntityManager().persist(user);
getEntityManager().getTransaction().commit();
return user;
}
  5.这里省略service的调用。直接编写Junit测试

//这里使用spring的测试类AbstractTransactionalSpringContextTests
class UserTest extends AbstractTransactionalSpringContextTests{
@Resource(name = "userService")
private UserService userService;
@Override
protected String[] getConfigLocations() {
return new String[] {"applicationContext.xml" };
}
@Test
public void testSaveUser() {
User user = new User();
user.setName("AAA");
userService.saveUser(user);
}
}
  PS:Mock测试参考链接http://zhizizhishou0104.iyunv.com/blog/1980182

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-301224-1-1.html 上篇帖子: DB2开发常用-------日期函数(常用日期函数) 下篇帖子: (SQL报错)DB2 SQL error: SQLCODE: -418, SQLSTATE: 42610, SQLERRMC: null
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表