博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2+hibernate-jpa+Spring+maven 整合(2)
阅读量:6162 次
发布时间:2019-06-21

本文共 17222 字,大约阅读时间需要 57 分钟。

1.修改pom.xml

1. 添加  slf4j-api

org.slf4j
slf4j-api
1.7.0

2.添加 spring-orm

org.springframework
spring-orm
3.0.5.RELEASE

3.加入hibernate

org.aspectj
aspectjweaver
1.7.2
org.hibernate
hibernate-entitymanager
3.6.10.Final
slf4j-api
org.slf4j
org.hibernate
hibernate-core
3.6.10.Final
slf4j-api
org.slf4j
mysql
mysql-connector-java
5.1.21
com.alibaba.druid
druid-wrapper
0.2.9

最终pom文件如下:

4.0.0
cc.jorcen.msai
mport
0.0.1-SNAPSHOT
war
UTF-8
org.slf4j
slf4j-api
1.7.0
org.springframework
spring-orm
3.0.5.RELEASE
org.apache.struts
struts2-spring-plugin
2.3.16
org.aspectj
aspectjweaver
1.7.2
org.hibernate
hibernate-entitymanager
3.6.10.Final
slf4j-api
org.slf4j
org.hibernate
hibernate-core
3.6.10.Final
slf4j-api
org.slf4j
mysql
mysql-connector-java
5.1.21
com.alibaba.druid
druid-wrapper
0.2.9
junit
junit
4.11
javax.servlet
jstl
1.2
provided
javax.servlet.jsp
jsp-api
2.1
provided
org.glassfish
javax.annotation
3.0.1
org.glassfish
javax.ejb
3.0.1
org.glassfish
javax.servlet
3.0.1
maven-war-plugin
maven-compiler-plugin
1.6
1.6

添加 spring-dataSource.xml

classpath:dataSourceConfig.properties

添加 spring-jpa.xml 文件

配置dataSourceConfig.properties文件如下:

#hibernate.dialect=org.hibernate.dialect.OracleDialect#driverClassName=oracle.jdbc.driver.OracleDriver#validationQuery=SELECT 1 FROM DUAL#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl#jdbc_username=sypro#jdbc_password=syprohibernate.dialect=org.hibernate.dialect.MySQLDialectdriverClassName=com.mysql.jdbc.DrivervalidationQuery=SELECT 1jdbc_url=jdbc:mysql://localhost:3306/c_sai?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNulljdbc_username=rootjdbc_password=#hibernate.dialect=org.hibernate.dialect.SQLServerDialect#driverClassName=net.sourceforge.jtds.jdbc.Driver#validationQuery=SELECT 1#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy#jdbc_username=sa#jdbc_password=123456#hibernate.dialect=org.hibernate.dialect.DerbyDialect#driverClassName=org.apache.derby.jdbc.EmbeddedDriver#validationQuery=SELECT 1#jdbc_url=jdbc:derby:sy;create=true#jdbc_username=sypro#jdbc_password=sypro#jndiName=java:comp/env/dataSourceNamehibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=truesessionInfoName=sessionInfouploadFieldName=filedatauploadFileMaxSize=20971520uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,miduploadDirectory=attached

测试类如下:

package com.sai.test.dao.impl;import javax.annotation.Resource;import javax.persistence.EntityManagerFactory;import org.springframework.orm.jpa.JpaTemplate;import org.springframework.orm.jpa.support.JpaDaoSupport;import org.springframework.stereotype.Repository;import com.sai.test.po.TTest;@Repository("TTestDAOImpl")public class TTestDAOImpl extends JpaDaoSupport implements TTestDAO {    @Resource    public void setMyEntityManagerFactory(            EntityManagerFactory entityManagerFactory) {        JpaTemplate jpaTemplate = new JpaTemplate(entityManagerFactory);        super.setJpaTemplate(jpaTemplate);    }    @Override    public TTest findById(Integer id) {        return getJpaTemplate().find(TTest.class, id);    }    @Override    public TTest save(TTest user) {        getJpaTemplate().persist(user);        return null;    }}

service

package com.sai.test.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.sai.test.dao.impl.TTestDAO;import com.sai.test.po.TTest;import com.sun.tools.internal.ws.wsdl.document.jaxws.Exception;@Service("TTestServiceImpl")public class TTestServiceImpl implements TTestService {    @Resource    private TTestDAO dao;    @Override    public TTest findById(Integer id) {        TTest test = dao.findById(id);        return test;    }    @Override    public void save(TTest test) {        dao.save(test);    }    @Override    public void put(TTest test) {        dao.save(test);    }    @Override    public void save2(TTest test) throws Throwable {        dao.save(test);        if (1 == 1)            throw new Throwable("bollback now !");    }}

测试类如下:

package test.jpa.tx;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.sai.test.dao.impl.TTestDAO;import com.sai.test.po.TTest;import com.sai.test.service.impl.TTestService;public class JPA_TX_Test {    private static final String PATH = "spring-*.xml";    private static final String APPLICATION = "spring-application-jpa-customDataSource.xml";    private static final ApplicationContext ac = new ClassPathXmlApplicationContext(            PATH);    TTestService t = (TTestService) ac.getBean("TTestServiceImpl");    @Test    public void support() {        try {            TTestDAO t = (TTestDAO) ac.getBean("TTestDAOImpl");            Object obj = t.findById(1);            System.out.println("TTestDAOImpl.findById : "+obj);        } catch (Exception e) {            e.printStackTrace();        }    }    @Test    public void txSupport() {        try {            Object obj = t.findById(1);            TTest test = new TTest();            test.setUserName("put");            t.put(test);            System.out.println("TTestServiceImpl.put : "+obj);        } catch (Exception e) {            e.printStackTrace();        }    }    @Test    public void txRequired() {        try {            Object obj = t.findById(1);            TTest test = new TTest();            test.setUserName("save");            t.save(test);            System.out.println("TTestServiceImpl.save : "+obj);        } catch (Exception e) {            e.printStackTrace();        }    }    @Test    public void txRequired2() {        TTest test = new TTest();        try {            test.setUserName("save2");            t.save2(test);        } catch (Throwable e) {            System.out.println("TTestServiceImpl.save2 : "+test);            e.printStackTrace();        }    }}

结果如下:

Hibernate: select ttest0_.id as id0_0_, ttest0_.userName as userName0_0_ from c_sai.t_test ttest0_ where ttest0_.id=?TTestDAOImpl.findById : TTest [id=1, userName=2]Hibernate: insert into c_sai.t_test (userName) values (?)TTestServiceImpl.save2 : TTest [id=42, userName=save2]java.lang.Throwable: bollback now !    at com.sai.test.service.impl.TTestServiceImpl.save2(TTestServiceImpl.java:39)    at com.sai.test.service.impl.TTestServiceImpl$$FastClassByCGLIB$$ecd6b2c8.invoke(
) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621) at com.sai.test.service.impl.TTestServiceImpl$$EnhancerByCGLIB$$bd6bd63e.save2(
) at test.jpa.tx.JPA_TX_Test.txRequired2(JPA_TX_Test.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Hibernate: select ttest0_.id as id0_0_, ttest0_.userName as userName0_0_ from c_sai.t_test ttest0_ where ttest0_.id=?TTestServiceImpl.put : TTest [id=1, userName=2]Hibernate: select ttest0_.id as id0_0_, ttest0_.userName as userName0_0_ from c_sai.t_test ttest0_ where ttest0_.id=?Hibernate: insert into c_sai.t_test (userName) values (?)TTestServiceImpl.save : TTest [id=1, userName=2]

数据库记录如下:

Id

username

2
43  save

到此,在web.xml里面添加配置:

encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
Spring OpenEntityManagerInViewFilter
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
Spring OpenEntityManagerInViewFilter
/*

最终效果如下:

contextConfigLocation
classpath:/spring-*.xml
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
Spring OpenEntityManagerInViewFilter
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
Spring OpenEntityManagerInViewFilter
/*
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.cy
index.jsp

 

 

 

到此,整合完毕

 

 

转载地址:http://quafa.baihongyu.com/

你可能感兴趣的文章
关于java.net.URLEncoder.encode编码问题
查看>>
详解 javascript中offsetleft属性的用法(转)
查看>>
.a静态库构架合成
查看>>
常用bat文件
查看>>
理性思维的七条原则
查看>>
多项式幂函数(加强版)
查看>>
ios-UIButton-常用方法
查看>>
Spring+SpringMVC+Mybatis框架整合流程
查看>>
doc 宽带连接
查看>>
laravel进阶系列--通过事件和事件监听实现服务解耦
查看>>
【Android】人体图片、地图图片、热力图,如何实现点击不同的部位执行不同的操作?...
查看>>
BZOJ 2733 线段树的合并 并查集
查看>>
super()方法
查看>>
蓝桥杯 第十届 JAVAB组 E迷宫
查看>>
【BZOJ3676】 [Apio2014]回文串(SAM,manacher)
查看>>
awk的基础应用
查看>>
8.5 趣味游戏(2)
查看>>
IDEA配置Struts框架
查看>>
点击出现黑色背景的解决:-webkit-tap-highlight-color:rgba(0,0,0,0)
查看>>
system(“”start calc“”)
查看>>