Recently the company to develop new projects, to use the struts2+mybatis+spring framework, so study under, to their blog published, Hope to bring help to everyone. Under
Side I put the source code of my MySchool development and database paste out.
Development Environment Myeclips+tomcate+sql Server
Development Technology Struts2+mybatis+spring Jsp+java
I. Create a Web project import the required jar files
two. Create and write configuration files, more profiles. May be a bit cumbersome.
1. Create and write 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" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <!--configuration Data Source Properties file--<bean id=" Propertyconfigurer "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> < Property name= ' Location ' > <value>/WEB-INF/configs/sqlServer.properties</value> </property> & Lt;/bean> <!--configuration data source-<bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerD
Atasource "> <property name=" driverclassname "> <value>${jdbc.driver}</value> </property> <property name= "url" > <value>${jdbc.url}</value> </property> <property name= "username "> <value>${jdbc.user}</value> </property&Gt <property name= "Password" > <value>${jdbc.pwd}</value> </property> </bean> <bean Id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "Configlocation" Val
Ue= "Classpath:com/test/sqlmapper/mybatis_config.xml"/> <property name= "DataSource" ref= "DataSource"/> </bean> <bean id= "Logindao" class= "Org.mybatis.spring.mapper.MapperFactoryBean" > <property n Ame= "Mapperinterface" value= "Com.test.dao.ILoginDao"/> <property name= "sqlsessionfactory" ref= " Sqlsessionfactory "/> </bean> <bean id=" loginaction "class=" com.test.action.LoginAction "> <propert Y name= "Logindao" ref= "Logindao" ></property> </bean> </beans>
2. Configure the data source of course there is a data source property file Sqlserver.properties
Jdbc.url=jdbc:sqlserver://localhost:1433;databasename=login
jdbc.driver= Com.microsoft.sqlserver.jdbc.SQLServerDriver
Jdbc.user=sa
jdbc.pwd=
My database password is empty pwd of course it will be empty.
Configuration file for 3.mybatis Mybatis_config.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<typealias alias= "userinfo" type= " Com.test.entity.UserInfo "/>
</typeAliases>
<mappers>
<mapper resource=" com/test/ Sqlmapper/loginmapper.xml "/>
</mappers>
</configuration>
4. There is a mapping file in Mybatis_config.xml Loginmapper.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "Com.test.dao.ILoginDao" >
<resultmap type= "userinfo" id= "UserMap" >
<id property= "id" column= "id"/>
<result property= "username" column= "username"/> <result property=
" Password "column=" password "/>
</resultMap>
<select id=" GetUser "parametertype=" String " resultmap= "UserMap" >
select * from UserInfo where username=#{username}
</select>
</mapper >
5. The next step is Web. xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.4" xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns : xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "HTTP://JAVA.SUN.COM/XML/NS/J2EE/http Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <context-param> <param-name>contextconfiglocation</ Param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param > <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-c
lass> </listener> <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>
Okay, all the configuration files are already configured.
Three. write Java file Interface, Entity helper class, and action
1. Write Interface Ilogindao.java
Package Com.test.dao;
Import java.util.List;
Public interface Ilogindao {public
List getUser (String userName);
2. Entity Auxiliary class Userinfo.java
Package com.test.entity;
public class UserInfo {
private int id;
Private String username;
private String password;
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 int getId () {
return ID;
}
public void setId (int id) {
this.id = ID;
}
}
3.action class Loginaction.java
package com.test.action;
Import java.util.List;
Import Com.test.dao.ILoginDao;
public class Loginaction {private Ilogindao Logindao;
Private String username;
private String password;
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 Ilogindao Getlogindao () {return logindao;
} public void Setlogindao (Ilogindao logindao) {This.logindao = Logindao;
Public String Execute () {string userName = GetUserName ();
String password = GetPassword ();
System.out.println ("UserName:" +username+ "\ n" + "Password:" +password);
List List = Logindao.getuser (userName);
if (List.size () >0) {return "success";
}else{return "error"; }
}
}
Four. The page has three of the following pages
1. login.jsp
<body>
<s:form action= "Login" method= "POST" >
<s:textfield name= "username" label= "user name:" > </s:textfield>
<s:textfield name= "password" label= "Password:" ></s:textfield>
<s:submit Value= "Submit" ></s:submit>
</s:form>
</body>
2.success.jsp a few words on the landing success
3.ERROR,JSP Login failed on several words
Five. Create a database login CREATE TABLE userinfo three field ID username password database I have uploaded this article inside, after downloading can use directly
Six. Well, start Tomcat and see if it works.
Struts_ibatis_spring_ Framework Integration database. RAR (7.8 MB) Downloaded: 1131 ibatis_spring consolidating documents. RAR (649.2 KB) Download number of times: 433