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

 摘要

最近一直在折腾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 style="color: #0000ff;">import<span style="color: #000000;"> com.opensymphony.xwork2.ActionSupport;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.opensymphony.xwork2.ModelDriven;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.ssh.domain.User;

<span style="color: #008000;">/**<span style="color: #008000;">

  • user相关action
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class UserAction <span style="color: #0000ff;">extends ActionSupport <span style="color: #0000ff;">implements ModelDriven<span style="color: #000000;"> {

    <span style="color: #0000ff;">private User user = <span style="color: #0000ff;">new<span style="color: #000000;"> User();

    @Override
    <span style="color: #0000ff;">public<span style="color: #000000;"> User getModel() {
    <span style="color: #0000ff;">return<span style="color: #000000;"> user;
    }

    <span style="color: #0000ff;">public<span style="color: #000000;"> String login() {
    System.out.println(user);
    <span style="color: #0000ff;">return<span style="color: #000000;"> NONE;
    }
    }

配置struts.xml

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

登录

<span style="color: #0000ff;"><<span style="color: #800000;">form <span style="color: #ff0000;">action<span style="color: #0000ff;">="${pageContext.request.contextPath}/user_login.action"<span style="color: #0000ff;">><span style="color: #000000;">
用户名:<span style="color: #0000ff;"><<span style="color: #800000;">input <span style="color: #ff0000;">type<span style="color: #0000ff;">="text"<span style="color: #ff0000;"> name<span style="color: #0000ff;">="name"<span style="color: #0000ff;">><span style="color: #000000;">
密码:<span style="color: #800000;"><input <span style="color: #ff0000;">type<span style="color: #0000ff;">="password"<span style="color: #ff0000;"> name<span style="color: #0000ff;">="password"<span style="color: #ff0000;"> id<span style="color: #0000ff;">="password"<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">input <span style="color: #ff0000;">type<span style="color: #0000ff;">="submit"<span style="color: #ff0000;"> value<span style="color: #0000ff;">="登录"<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">form<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">body<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">html<span style="color: #0000ff;">>

启动

在启动时发现如下内容

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 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包引起的,那么找到该包,引入即可

5020b423ebf28b.png" alt="">

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

404 - There is no Action mapped for namespace [/] and action name [user_find] associated with context path [].

通配符调用方法时,内部会验证是否允许访问该方法,所以要加上

方法名1,方法名2…代码。

 

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

<span style="color: #0000ff;"><!<span style="color: #ff00ff;">DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"<span style="color: #0000ff;">>

<span style="color: #0000ff;"><<span style="color: #800000;">struts<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">package <span style="color: #ff0000;">name<span style="color: #0000ff;">="userAction"<span style="color: #ff0000;"> extends<span style="color: #0000ff;">="struts-default"<span style="color: #ff0000;"> namespace<span style="color: #0000ff;">="/"<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">action <span style="color: #ff0000;">name<span style="color: #0000ff;">="user_*"<span style="color: #ff0000;"> method<span style="color: #0000ff;">="{1}"<span style="color: #ff0000;"> class<span style="color: #0000ff;">="com.ssh.action.UserAction"<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">allowed-methods<span style="color: #0000ff;">>login<span style="color: #0000ff;"></<span style="color: #800000;">allowed-methods<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">action<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">package<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">struts<span style="color: #0000ff;">>

测试struts2

后台action能收到数据

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

整合spring

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

<span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;将action的创建交给spring ioc 注意action是多例的 一定要将scope设置为prototype</span><span style="color: #008000;"&gt;--></span>
<span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;bean </span><span style="color: #ff0000;"&gt;class</span><span style="color: #0000ff;"&gt;="com.ssh.action.UserAction"</span><span style="color: #ff0000;"&gt; id</span><span style="color: #0000ff;"&gt;="userAction"</span><span style="color: #ff0000;"&gt; scope</span><span style="color: #0000ff;"&gt;="prototype"</span><span style="color: #0000ff;"&gt;/></span>

<span style="color: #0000ff;"></<span style="color: #800000;">beans<span style="color: #0000ff;">>

我们希望在服务器启动的时候加载该配置文件,那么需要在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 sql"true sql"true 自动生成表结构 update org.hibernate.connection.C3P0ConnectionProvider 5 20 数据库连接的过期时间 秒 如果数据库连接池中的某个连接处于空闲时间超过了timeout,则从连接池中清除 120 3000

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

在applicationContext.xml中添加如下配置

<span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;加载sessionFactory配置</span><span style="color: #008000;"&gt;--></span>
<span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;bean </span><span style="color: #ff0000;"&gt;id</span><span style="color: #0000ff;"&gt;="sessionFactory"</span><span style="color: #ff0000;"&gt; class</span><span style="color: #0000ff;"&gt;="org.springframework.orm.hibernate3.LocalSession<a href="https://www.f2er.com/tag/factorybean/" target="_blank" class="keywords">factorybean</a>"</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;property </span><span style="color: #ff0000;"&gt;name</span><span style="color: #0000ff;"&gt;="configLocation"</span><span style="color: #ff0000;"&gt; value</span><span style="color: #0000ff;"&gt;="classpath:hibernate.cfg.xml"</span><span style="color: #0000ff;"&gt;></</span><span style="color: #800000;"&gt;property</span><span style="color: #0000ff;"&gt;></span>
<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;bean</span><span style="color: #0000ff;"&gt;></span>

<span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt; 注入事务管理器,提交事务和回滚事务 </span><span style="color: #008000;"&gt;--></span>
<span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;bean </span><span style="color: #ff0000;"&gt;id</span><span style="color: #0000ff;"&gt;="transactionManager"</span><span style="color: #ff0000;"&gt; class</span><span style="color: #0000ff;"&gt;="org.springframework.orm.hibernate3.HibernateTransactionManager"</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;property </span><span style="color: #ff0000;"&gt;name</span><span style="color: #0000ff;"&gt;="sessionFactory"</span><span style="color: #ff0000;"&gt; ref</span><span style="color: #0000ff;"&gt;="sessionFactory"</span><span style="color: #0000ff;"&gt;/></span>
<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;bean</span><span style="color: #0000ff;"&gt;></span>
<span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;开启事务注解方式</span><span style="color: #008000;"&gt;--></span>
<span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;tx:annotation-driven </span><span style="color: #ff0000;"&gt;transaction-manager</span><span style="color: #0000ff;"&gt;="transactionManager"</span><span style="color: #0000ff;"&gt;/></span>
<span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;配置userDao</span><span style="color: #008000;"&gt;--></span>
<span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;bean </span><span style="color: #ff0000;"&gt;class</span><span style="color: #0000ff;"&gt;="com.ssh.dao.UserDao"</span><span style="color: #ff0000;"&gt; id</span><span style="color: #0000ff;"&gt;="userDao"</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;注入sessionFactory</span><span style="color: #008000;"&gt;--></span>
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;property </span><span style="color: #ff0000;"&gt;name</span><span style="color: #0000ff;"&gt;="sessionFactory"</span><span style="color: #ff0000;"&gt; ref</span><span style="color: #0000ff;"&gt;="sessionFactory"</span><span style="color: #0000ff;"&gt;></</span><span style="color: #800000;"&gt;property</span><span style="color: #0000ff;"&gt;></span>
<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;bean</span><span style="color: #0000ff;"&gt;></span>
<span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;配置userService</span><span style="color: #008000;"&gt;--></span>
<span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;bean </span><span style="color: #ff0000;"&gt;id</span><span style="color: #0000ff;"&gt;="userService"</span><span style="color: #ff0000;"&gt; class</span><span style="color: #0000ff;"&gt;="com.ssh.service.UserService"</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;注入userDao</span><span style="color: #008000;"&gt;--></span>
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;property </span><span style="color: #ff0000;"&gt;name</span><span style="color: #0000ff;"&gt;="userDao"</span><span style="color: #ff0000;"&gt; ref</span><span style="color: #0000ff;"&gt;="userDao"</span><span style="color: #0000ff;"&gt;></</span><span style="color: #800000;"&gt;property</span><span style="color: #0000ff;"&gt;></span>
<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;bean</span><span style="color: #0000ff;"&gt;></span>
<span style="color: #008000;"&gt;<!--</span><span style="color: #008000;"&gt;将action的创建交给spring ioc 注意action是多例的 一定要将scope设置为prototype</span><span style="color: #008000;"&gt;--></span>
<span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;bean </span><span style="color: #ff0000;"&gt;class</span><span style="color: #0000ff;"&gt;="com.ssh.action.UserAction"</span><span style="color: #ff0000;"&gt; id</span><span style="color: #0000ff;"&gt;="userAction"</span><span style="color: #ff0000;"&gt; scope</span><span style="color: #0000ff;"&gt;="prototype"</span><span style="color: #0000ff;"&gt;/></span>

<span style="color: #0000ff;"></<span style="color: #800000;">beans<span style="color: #0000ff;">>

userDao

<span style="color: #0000ff;">import<span style="color: #000000;"> com.ssh.domain.User;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.orm.hibernate3.support.HibernateDaoSupport;

<span style="color: #0000ff;">public <span style="color: #0000ff;">class UserDao <span style="color: #0000ff;">extends<span style="color: #000000;"> HibernateDaoSupport {
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> save(User user) {
<span style="color: #0000ff;">this<span style="color: #000000;">.getHibernateTemplate().save(user);

}

}

userService

<span style="color: #0000ff;">import<span style="color: #000000;"> com.ssh.dao.UserDao;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.ssh.domain.User;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.transaction.annotation.Transactional;

@Transactional
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> UserService {

</span><span style="color: #0000ff;"&gt;private</span><span style="color: #000000;"&gt; UserDao userDao;
</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; setUserDao(UserDao userDao) {
    </span><span style="color: #0000ff;"&gt;this</span>.userDao =<span style="color: #000000;"&gt; userDao;
}
</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; save(User user) {
    userDao.save(user);
}

}

userServiceTest

<span style="color: #0000ff;">import<span style="color: #000000;"> com.ssh.domain.User;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.ssh.service.UserService;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.junit.Test;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.junit.runner.RunWith;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.test.context.ContextConfiguration;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

<span style="color: #0000ff;">import<span style="color: #000000;"> javax.annotation.Resource;

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

@Test
</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; saveTest() {
    User user </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; User();
    user.setId(</span>1<span style="color: #000000;"&gt;);
    user.setName(</span>"wolfy"<span style="color: #000000;"&gt;);
    user.setPassword(</span>"1234"<span style="color: #000000;"&gt;);
    userService.save(user);
}

}

测试

50279fd033943f07c28b7c11d3.png" alt="">

 

总结

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

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

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

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

相关文章

Spring Cloud为Spring Boot应用程序提供Netflix OSS集成。 提供的功能模块包括服务发现(Eureka),断路...
Spring Cloud 学习笔记;maven配置;入门学习;基于Spring Boot 实现;服务端配置,客户端配置;
可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的...
Spring中各种方式进行日期时间处理,有作用于单个实体的,也有作用于全局的,有作用于请求入参的,有作...
跨域资源共享(Cross-origin resource sharing)(CORS)是W3C的标准,大部分的浏览器都实现了这个标准...
Spring Boot使创建基于Spring的应用程序变得轻松,大部分的SpringBoot应用程序都只需要很少的Spring配置...