[Java web]Spring+Struts2+Hibernate整合过程

[Java web]Spring+Struts2+Hibernate整合过程,第1张

概述 摘要最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh。环境工具IDE :Idea 2018  数据库:Mysql关于如何安装tomcat、mysql、idea,请自行查找。1、使用idea创建spring项目2、创建项目SSHDemo,项目结构如下3、添加三层的包。这里采用三层的架构进行项目的设计action存放struts2的action类dao 数据库 *** 作service业务层test单元测试domain javabean4、配置struts2在web.xml中你会看到下面的红字提示,引起这个的原因是因为struts2 版本>=2.5 需要使用另外的方式进行配置<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>修改后的配置如下<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class></filter>其他内容不变在action包 添加一个UserActionpackage com.ssh.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.ssh.domain.User;/*** user相关action*/public class UserAction extends ActionSupport implements ModelDriven<User> {private User user = new User();@Overridepublic User getModel() {return user;}public String login() {System.out.println(user);return NONE;}}配置struts.xml<struts><package name="user" extends="struts-default" namespace="/"><action name="user_*" class="com.ssh.action.UserAction" method="{1}"/></package></struts>添加一个登录页面login.jsp测试<%--Created by IntelliJ IDEA.User: sunwyDate: 2018/6/6Time: 10:23To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>登录</title></head><body><form action="${pageContext.request.contextPath}/user_login.action">用户名:<input type="text" name="name">密码:<input type="password" name="password" id="password"><input type="submit" value="登录"></form></body></html>启动在启动时发现如下内容严重: Context [] startup failed due to previous errors[2018-06-06 10:25:36,908] Artifact SSHDemo:war exploded: Error during artifact deployment. See server log for details.打开project structure修复出现的问题重启Tomcat发现如下错误六月 06, 2018 10:57:12 上午 org.apache.catalina.startup.TldConfig execute信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...ERROR Dispatcher Dispatcher initialization failedjava.lang.RuntimeException: java.lang.reflect.InvocationTargetExceptionat com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:289)at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:422)at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:231)at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:106)at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:584)at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:581)at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:560)at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:581)at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createBootstrapContainer(DefaultConfiguration.java:287)at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:162)at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:957)at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:463)at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:496)at org.apache.struts2.dispatcher.InitOperations.initDispatcher(InitOperations.java:73)at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:61)at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)at  摘要

最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh。

环境

工具IDE :IDea 2018  

数据库:MysqL

关于如何安装tomcat、MysqL、IDea,请自行查找。

1、使用IDea创建spring项目

2、创建项目SSHDemo,项目结构如下

3、添加三层的包。

这里采用三层的架构进行项目的设计

action存放struts2的action类

dao 数据库 *** 作

service业务层

test单元测试

domain javabean

4、配置struts2

在web.xml中你会看到下面的红字提示,引起这个的原因是因为struts2 版本>=2.5 需要使用另外的方式进行配置

struts2 org.apache.struts2.dispatcher. struts2 /*

修改后的配置如下

struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

其他内容不变

在action包 添加一个UserAction

<span >import<span > com.opensymphony.xwork2.ActionSupport;
<span >import
<span > com.opensymphony.xwork2.ModelDriven;
<span >import
<span > com.ssh.domain.User;

<span >/**<span >

user相关action
<span >*/
<span >public <span >class UserAction <span >extends ActionSupport <span >implements ModelDriven<span > {

<span >private User user = <span >new<span > User();

@OverrIDe
<span >public<span > User getModel() {
<span >return<span > user;
}

<span >public<span > String login() {
System.out.println(user);
<span >return<span > NONE;
}
}

配置struts.xml

添加一个登录页面login.Jsp测试

登录

<span ><<span >form <span >action<span >="${pageContext.request.contextpath}/user_login.action"<span >><span >
用户名:<span ><<span >input <span >type<span >="text"<span > name<span >="name"<span >><span >
密码:<span ><input <span >type<span >="password"<span > name<span >="password"<span > ID<span >="password"<span >>
<span ><<span >input <span >type<span >="submit"<span > value<span >="登录"<span >>
<span ></<span >form<span >>
<span ></<span >body<span >>
<span ></<span >HTML<span >>

启动

在启动时发现如下内容

打开project structure修复出现的问题

重启Tomcat发现如下错误

六月 06,2018 10:57:12 上午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable deBUG logging for this logger for a complete List of JARs that were scanned but no TLDs were found in them. SkipPing unneeded JARs during scanning can improve startup time and JsP compilation time.
ERROR StatusLogger Log4j2 Could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR dispatcher dispatcher initialization Failed
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:289)
at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:422)
at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:231)
at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)
at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:106)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:584)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:581)
at com.opensymphony.xwork2.inject.ContainerImpl.callinContext(ContainerImpl.java:560)
at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:581)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createbootstrapContainer(DefaultConfiguration.java:287)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:162)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.dispatcher.getContainer(dispatcher.java:957)
at org.apache.struts2.dispatcher.dispatcher.init_PreloadConfiguration(dispatcher.java:463)
at org.apache.struts2.dispatcher.dispatcher.init(dispatcher.java:496)
at org.apache.struts2.dispatcher.Initoperations.initdispatcher(Initoperations.java:73)
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:61)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5699)
at org.apache.catalina.util.lifecycleBase.start(lifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1017)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:993)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1900)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.Mbeanfactory.createStandardContext(Mbeanfactory.java:618)
at org.apache.catalina.mbeans.Mbeanfactory.createStandardContext(Mbeanfactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:287)
... 64 more
Caused by: java.lang.ExceptionInInitializerError
at com.opensymphony.xwork2.ognl.OgnlValueStackFactory.setContainer(OgnlValueStackFactory.java:88)
... 69 more
Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
at ognl.OgnlRuntime.(OgnlRuntime.java:169)
... 70 more
Caused by: java.lang.classNotFoundException: javassist.Classpool
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1928)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1771)
at java.lang.class.forname0(Native Method)
at java.lang.class.forname(Class.java:264)
at ognl.OgnlRuntime.(OgnlRuntime.java:166)
... 70 more
六月 06,2018 10:57:12 上午 org.apache.catalina.core.StandardContext startInternal
严重: One or more Filters Failed to start. Full details will be found in the appropriate container log file
六月 06,2018 10:57:12 上午 org.apache.catalina.core.StandardContext startInternal
严重: Context [] startup Failed due to prevIoUs errors

好吧,解决问题吧

Caused by: java.lang.classNotFoundException: javassist.Classpool

这个问题,是由于没有引入javassist-3.11.0.GA.jar包引起的,那么找到该包,引入即可

重新发布,输入用户名和密码

代码。

 

最后struts.xml文件内容,这种配置也是一种安全设置,通配符,如果该action有其他不想被别人访问到的方法,如果通过通配符的方式,直接暴漏出来了,通过这种配置,可以配置允许访问哪些方法,对其它隐私的方法进行保护。

<span ><!<span >DOCTYPE struts PUBliC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"<span >>

<span ><<span >struts<span >>
<span ><<span >package <span >name<span >="userAction"<span > extends<span >="struts-default"<span > namespace<span >="/"<span >>
<span ><<span >action <span >name<span >="user_*"<span > method<span >="{1}"<span > class<span >="com.ssh.action.UserAction"<span >>
<span ><<span >allowed-methods<span >>login<span ></<span >allowed-methods<span >>
<span ></<span >action<span >>
<span ></<span >package<span >>
<span ></<span >struts<span >>

测试struts2

后台action能收到数据

好了,到这里,struts2 算是配置成功了,发现还是不容易的。

整合spring

在src目录下添加applicationContext.xml配置文件

<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;将action的<a href="https://m.jb51.cc/tag/chuangjian/" target="_blank" >创建</a>交给spring ioc 注意action是多例的 一定要将s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e<a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>为prototype</span><span &gt;--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="com.ssh.action.UserAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="userAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="prototype"</span><span &gt;/></span>

<span ></<span >beans<span >>

我们希望在服务器启动的时候加载该配置文件,那么需要在web.xml添加如下内容

org.springframework. contextConfigLocation classpath:applicationContext.xml

这里需要添加spring-web-3.2.0.RELEASE.jar包

成功加载

整合Hibernate

 添加jar包

在src目录下添加hibernate配置文件hibernate.cfg.xml

com.MysqL.jdbc.Driver jdbc:MysqL:///sshdemo root abcd org.hibernate.dialect.MysqLDialect true true update org.hibernate.connection.C3P0ConnectionProvIDer 5 20 120 3000

在domain包下添加映射配置User.hbm.xml

在applicationContext.xml中添加如下配置

<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;加载sessionFactory配置</span><span &gt;--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="sessionFactory"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="org.springframework.orm.hibernate3.LocalSession<a href="https://www.jb51.cc/tag/factorybean/" target="_blank" >factorybean</a>"</span><span &gt;></span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="con<a href="https://www.jb51.cc/tag/fig/" target="_blank" >fig</a>Location"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; value</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="cla<a href="https://www.jb51.cc/tag/ssp/" target="_blank" >ssp</a>ath:hibernate.cfg.xml"</span><span &gt;></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;property</span><span &gt;></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;</</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean</span><span &gt;></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 注入事务<a href="https://m.jb51.cc/tag/guanli/" target="_blank" >管理</a>器,提交事务和回滚事务 </span><span &gt;--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="transactionManager"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="org.springframework.orm.hibernate3.HibernateTransactionManager"</span><span &gt;></span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="sessionFactory"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; ref</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="sessionFactory"</span><span &gt;/></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;</</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean</span><span &gt;></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;开启事务注解方式</span><span &gt;--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;tx:annotation-driven </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;transaction-manager</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="transactionManager"</span><span &gt;/></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;配置userDao</span><span &gt;--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="com.ssh.dao.UserDao"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="userDao"</span><span &gt;></span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;注入sessionFactory</span><span &gt;--></span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="sessionFactory"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; ref</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="sessionFactory"</span><span &gt;></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;property</span><span &gt;></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;</</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean</span><span &gt;></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;配置userService</span><span &gt;--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="userService"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="com.ssh.service.UserService"</span><span &gt;></span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;注入userDao</span><span &gt;--></span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="userDao"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; ref</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="userDao"</span><span &gt;></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;property</span><span &gt;></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;</</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean</span><span &gt;></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;将action的<a href="https://m.jb51.cc/tag/chuangjian/" target="_blank" >创建</a>交给spring ioc 注意action是多例的 一定要将s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e<a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>为prototype</span><span &gt;--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;<</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="com.ssh.action.UserAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="userAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"&gt; s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;="prototype"</span><span &gt;/></span>

<span ></<span >beans<span >>

userDao

<span >import<span > com.ssh.domain.User;
<span >import
<span > org.springframework.orm.hibernate3.support.HibernateDaoSupport;

<span >public <span >class UserDao <span >extends<span > HibernateDaoSupport {
<span >public <span >voID<span > save(User user) {
<span >this<span >.getHibernateTemplate().save(user);

}

}

userService

<span >import<span > com.ssh.dao.UserDao;
<span >import
<span > com.ssh.domain.User;
<span >import
<span > org.springframework.transaction.annotation.Transactional;

@Transactional
<span >public <span >class<span > UserService {

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;private</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; UserDao userDao;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; setUserDao(UserDao userDao) {    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;this</span>.userDao =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; userDao;}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; save(User user) {    userDao.save(user);}

}

userServiceTest

<span >import<span > com.ssh.domain.User;
<span >import
<span > com.ssh.service.UserService;
<span >import
<span > org.junit.Test;
<span >import
<span > org.junit.runner.RunWith;
<span >import
<span > org.springframework.test.context.ContextConfiguration;
<span >import
<span > org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

<span >import<span > javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.<span >class<span >)
@ContextConfiguration("classpath:applicationContext.xml"<span >)
<span >public <span >class<span > UserServiceTest {
@Resource(name = "userService"<span >)
<span >private<span > UserService userService;

@Test</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; save<a href="https://www.jb51.cc/tag/test/" target="_blank" >test()</a> {    User user </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; User();    user.set<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>(</span>1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    user.set<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>(</span>"w<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>fy"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    user.setPassword(</span>"1234"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);    userService.save(user);}

}

测试

 

总结

1、配置过程很麻烦,很可能会缺包。

2、在保存数据的时候发现,执行了,但数据库中没有数据,原因是因为没有配置事务管理器,还要注意开启注解的方式管理事务。

 3、当然仍然可以进一步的整合,比如去掉hibernate.cfg.xml文件,在applicationContext.xml进行配置

4、注意如果使用hibernate4需要下载hibernate4.2+的jar包

总结

以上是内存溢出为你收集整理的[Java web]Spring+Struts2+Hibernate整合过程全部内容,希望文章能够帮你解决[Java web]Spring+Struts2+Hibernate整合过程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270016.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存