Go on, I'm going to create another project, Sshtest, and move the Shdemo code in (Shdemo to do something else)
1. Introducing Spring's required jar package
Not only does this need spring's jar package
You also need two data source packages in Hibernate
There is also a Aspectjweaver.jar package, this package in the AOP configuration of the time required, now spring package is not provided, to download themselves to the Internet.
2. Modify the Web. xml File
Add spring Listener and configuration file locations to the Web. xml file and Hibernate session filter
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "id=" webapp_id "version=" 3.1 "> <display-name>sshtest</ Display-name> <!--spring--<context-param> <param-name>contextconfiglocation</param-name > <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <listener > <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </ Listener> <!--struts2--<filter> <filter-name>struts2</filter-name> <FILTER-CLA Ss>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter > <filter-mapping> <filter-name>struts2</filter-name> <url-patteRn>/*</url-pattern> </filter-mapping> <!--hibernate--<filter> <filter-name>o Pensessioninviewfilter</filter-name> <filter-class> Org.springframework.orm.hibernate5.support.opensessioninviewfilter</filter-class> <init-param> <p
Aram-name>singlesession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> < url-pattern>*.do,*.action</url-pattern> </filter-mapping> <welcome-file-list> <welcome-fi Le>/web-inf/jsp/login.jsp</welcome-file> </welcome-file-list> </web-app>
3. Create a spring configuration file under the Web-inf directory Applicationcontext.xml
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:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "
Http://www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!--define data source information--<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "close" > <property nam E= "Driverclass" > <value>com.mysql.jdbc.Driver</value> </property> <pro
Perty name= "Jdbcurl" > <value>jdbc:mysql://localhost:3306/tzy?useunicode=true&characterencoding=utf8&usessl=true</
value> </property> <property name= "user" > <value>tzy</value> </property> <property name= "password" > <value>11111111</value> </ property> <property name= "maxpoolsize" > <value>80</value> </PROPERTY&G
T <property name= "Minpoolsize" > <value>1</value> </property> <propert Y name= "initialpoolsize" > <value>1</value> </property> <property name= "MaxIdleTime" > <value>20</value> </property> </bean> <!--definition Hibe Rnate Sessionfactory-<!--sessionfactory uses the data source for the above data source-<!--specifies hibernate's mapping file and configuration information--& Lt;bean id= "SessionfactoRy "class=" Org.springframework.orm.hibernate5.LocalSessionFactoryBean "> <property name=" DataSource "&
Gt <ref local= "DataSource"/> </property> <property name= "Mappingresources" > &L t;list> <value>com/tzy/bean/User.hbm.xml</value> </list> </pro perty> <property name= "hibernateproperties" > <props> <prop key= "Hibe Rnate.dialect ">org.hibernate.dialect.MySQLDialect</prop> <prop key=" Show_sql ">true</prop > <prop key= "hibernate.jdbc.batch_size" >20</prop> </props> </p roperty> </bean> <bean id= "TransactionManager" class= "Org.springframework.orm.hibernate5.Hibe
Rnatetransactionmanager "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> <Tx:advice id= "Transactionadvice" transaction-manager= "TransactionManager" > <tx:attributes> <tx:met Hod name= "get*" propagation= "REQUIRED"/> <tx:method name= "*" propagation= "REQUIRED"/> </tx:attr Ibutes> </tx:advice> <!--AOP proxy settings, the default is inject interface to add proxy-target-class= "true" to provide transaction enhancement through AOP configuration, Let DAO package all the methods of all beans owned transaction--<aop:config proxy-target-class= "true" > <aop:pointcut id= "Daopointcut" expression= "Execution (* com.tzy.dao.*.* (..))"
/> <aop:advisor advice-ref= "Transactionadvice" pointcut-ref= "Daopointcut"/> </aop:config> <bean id= "user" class= "com.tzy.bean.User" ></bean> <!--user registration data Access Class--<bean id= "Logindao" class= "Com.tzy.daoImpl.LoginDaoImpl" > <property name= "sessionfactory" > <ref bean= "sessionf
Actory "/> </property> <property name=" user "> <ref bean=" User "/> </propeRty> </bean> <!--user Registration Business logic class--<bean id= "Loginservice" class= "Com.tzy.serviceImpl.LoginServ Iceimpl "> <property name=" Logindao "> <ref bean=" Logindao "/> </property> &l t;/bean> <!--user registered action--<bean id= "loginaction" class= "Com.tzy.action.LoginAction" > & Lt;property name= "Loginservice" > <ref bean= "loginservice"/> </property> </bean&
Gt <!--more bean definitions Go-----</beans>
The following points are important to note:
1. Constraint Introduction
2. Dependency Injection
3. Facet-oriented configuration
3. Delete Hibernate's configuration file
Its work has been given to spring to do, so Hibernate.cfg.xml no longer need, xiemoshalv.
4. Modify the appropriate class
Since the object registered by Applicationcontext.xml must be dependent on injection (DI), subsequent generation of the object will be taken over by spring, so there should be no new object in the code, and the following objects will be modified to add setter methods to dependent objects without arguments, and the modified constructs Function:
Com.tzy.daoImpl.LoginDaoImpl.java
Package Com.tzy.daoImpl;
Import java.util.List;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;
Import Org.springframework.orm.hibernate5.support.HibernateDaoSupport;
Import Com.tzy.bean.User;
Import Com.tzy.dao.LoginDao;
public class Logindaoimpl extends Hibernatedaosupport implements Logindao {private user user;
public void SetUser (user user) {this.user = user; } @SuppressWarnings ("Unchecked") @Override public User GetUser (String username) {@SuppressWarnings ("resource") A Pplicationcontext context = new Filesystemxmlapplicationcontext ("e:/data/workspace/sshtest/webcontent/web-inf/
Applicationcontext.xml ");
user= (user) Context.getbean ("User");
String hql = "from User";
List<user> list= (list<user>) this.gethibernatetemplate (). Find (HQL);
for (User user2:list) {if (Username.equals (User2.getusername ())) {User.setid (User2.getid ()); User.setusername(User2.getusername ());
User.setpassword (User2.getpassword ());
}} return user;
}
}
Note:
There are three ways to generate 1.ApplicationContext: Filesystemxmlapplicationcontext, Classpathxmlapplicationcontext, Webxmlapplicationcontext, all line, recommended to use Classpathxmlapplicationcontext, I was to save trouble;
2.getHibernateTemplate () Here or use the stupid method, do not know how, this time with the condition query or will error, adding and removing the method can be successful operation, there should be a good study of the new version of the API changes.
Com.tzy.serviceImpl.LoginServiceImpl.java
Package Com.tzy.serviceImpl;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;
Import Com.tzy.bean.User;
Import Com.tzy.daoImpl.LoginDaoImpl;
Import Com.tzy.service.LoginService;
public class Loginserviceimpl implements Loginservice {
Logindaoimpl Logindao;
User user;
public void Setlogindao (Logindaoimpl logindao) {
This.logindao = Logindao;
}
@Override public
string CheckUser (string username, string password) {
@SuppressWarnings ("resource")
ApplicationContext context = new Filesystemxmlapplicationcontext ("e:/data/workspace/sshtest/webcontent/web-inf/ Applicationcontext.xml ");
Logindao = (Logindaoimpl) context.getbean ("Logindao");
user = Logindao.getuser (username);
if (User.getusername ()!=null) {
if (password.equals (User.getpassword ())) {
return "success";
} else{
return "error";
}
} else{
return "register";}}
Com.tzy.action.LoginAction.java
Package com.tzy.action;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;
Import Com.opensymphony.xwork2.ActionSupport;
Import Com.tzy.serviceImpl.LoginServiceImpl;
public class Loginaction extends actionsupport{/** * */private static final long serialversionuid = 1L;
Private String username;
private String password;
Loginserviceimpl Loginservice;
Public String GetUserName () {return username;
} public void Setusername (String username) {this.username = username;
} public String GetPassword () {return password;
} public void SetPassword (String password) {this.password = password;
} public void Setloginservice (Loginserviceimpl loginservice) {this.loginservice = Loginservice; } @Override Public String execute () throws Exception {@SuppressWarnings ("resource") ApplicationContext context = n EW Filesystemxmlapplicationcontext ("e:/data/workspace/sshtest/webcontent/web-inf/applicationcontext.xml ");
Loginservice = (Loginserviceimpl) context.getbean ("Loginservice");
Return Loginservice.checkuser (username, password);
}
}
The final file structure:
Run the project and the operational database will succeed.
Finally, summed up a sentence, if the use of the framework of the problem, the best solution is to read the official documents, the official will certainly not be wrong, especially when using the old method of error, first ruled out other factors, and then according to the official example to do first, the whole has been adjusted after the Back again to optimize and study the new changes in the API, this step I first lazy, back if it will be used to fill up.