Because the work of several projects are based on S2SM (Strut2+spring+mybatis) or s2si framework, so take advantage of the two-day break, they also try to build a simple SSI framework in these days. Nonsense not much to say, we go directly to the point:
Preparatory work: (1) Development tools + database (myeclipse 8.5 +tomcat 6.0+mysql 5.5);
(2) The jar package required by the framework (here's a list of the jar package names)
The first step, after the preparation is complete, is to create the Web project project, and after you create the project, you can import the default Spring 3.0 jar package ("note" through the MyEclipse) by using the default imported MyEclipse jar. And you can produce the default Applicationcontext.xml file. As long as we have this file here, put the applicationcontext.xml under the Project/webroot/web-inf folder), and of course you can also get strut.xml by importing the default Struts2 jar package. Then delete your own Jar pack and introduce our own jar package (including Ibatis and database-connected jar packs).
The second step, after the work is done, we want to create the project and the database connection (data source configuration), where we are configured in Applicationcontext.xml, including the Ibatis sqlmapclient, as well as things management configuration; see Code for details:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= " Http://www.springframework.org/schema/aop "xmlns:tx=" Http://www.springframework.org/schema/tx "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 "> <!--set MYSQL data source--> <bean id=" DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "> <property name=" driverclassname "value=" Com.mysql.jdbc.Driver " > <property name= "url" value= "Jdbc:mysql://localhost:3306/zengmeizhan?characterencoding=utf8"/> <
Property name= "username" value= "root"/><property name= "Password" value= "782306"/> </bean> <!--things note (database connection management)--> <bean "Id=" Nager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <property name=" datasource "ref=" DataSource "/> <property name=" Globalrollbackonparticipationfailure "value=" false "/> </bean> & lt;!
--Transaction annotations support--> <tx:annotation-driven transaction-manager= "TransactionManager"/> <!--spring configuration Ibatis--> <bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" > <property name= "
Configlocations "> <value>classpath*:sqlMapConfig.xml</value> </property> <!--introducing data sources--> <property name= "DataSource" ref= "DataSource"/> <property name= "Usetransactionawaredatasource" > <val ue> false </value> </property> </bean> </beans>
And the configuration in the Sqlmapconfig.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE sqlmapconfig public
"-//ibatis.com//dtd SQL Map Config 2.0//en"
"http://ibatis.apache.org/dtd/ Sql-map-config-2.dtd ">
<sqlMapConfig>
<settings cachemodelsenabled =" false "
enhancementenabled = "false"
lazyloadingenabled = "false"
usestatementnamespaces = "true"
maxrequests = " 128 "
maxSessions =" "
maxtransactions="
/>
</sqlMapConfig>
"Note": cachemodelsenabled: Enables or disables all cached cachemodelsenabled model for sqlmapclient globally. used when debugging a program.
Enhancementenabled: Enable or disable Run-time bytecode enhancements globally to optimize performance of accessing Java Bean properties while optimizing the performance of deferred loading.
Lazyloadingenabled: Enables or disables all deferred loading of sqlmapclient globally. Used when lazyloadingenabled the debugger.
Usestatementnamespaces: If you enable this property, you must use the fully qualified name to refer to mapped usestatementnamespaces statement.
The fully qualified name of the mapped statement is synthesized by the name of the Sql-map and the name of the mapped-statement.
Maxrequests: The maximum number of threads executing the SQL statement at the same time.
MaxSessions: The maximum number of sessions that are active at the same time.
Maxtransactions: The maximum number of thread maxtransactions to enter Sqlmapclient.starttransaction () at the same time.
In the second step, to configure the Web.xml file,
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <!--configuration 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> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <FILTER&G
T <!--struts Fiter--> <filter-name>struts2</filter-name> <filter-class> Org.apache.strut S2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <parAm-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,struts.xml</ param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter -name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
Step fourth, to create the package we need (based on the MVC framework), has created an entity Userbean (where the bean code is not posted), and the corresponding mapped Sqlmapper.xml file. "Note" because it was previously specified in Sqlmapconfig.xml usestatementnamespaces =true, so when we create sqlmapper.xml we want to assign a corresponding namespace to this, And to bring the good sqlmap.xml into the sqlmapconfig.xml.
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE sqlmap Public "-//ibatis.com//dtd SQL Map 2.0//en" "Http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlmap namespace= "User" > <!--Typealias allows us to use the Contact entity class below without the need to write a package name--> <typealias alias = "Alias_user" type = " Com.zengMeizan.charge.domain.UserEntity "/> <resultmap class=" alias_user "id=" Userresultmapper "> < Result property= "id" column= "id" jdbctype= "VARCHAR"/> <result property= "userName" column= "user_name" jdbctype= " VARCHAR "/> <result property= usercode" column= "User_code" jdbctype= "VARCHAR"/> <result "property=" Password "column=" password "jdbctype=" VARCHAR "/> <result property=" active "column=" active "jdbctype=" VARCHAR "/ > <result property= "createtime" column= "Create_time" jdbctype= "TIMESTAMP"/> "<result property=" ModifyTime "Column=" Modify_time "jdbctype=" TIMESTAMP "/> <result property=" CreateUser "column=" Create_user_code "JdbcType = "VARCHAR"/> <result property= "ModifyUser" column= "Modify_user_code" jdbctype= "VARCHAR"/> </resultMap> <!--
Insert SQL--> <insert id= "Insertoneuser" parameterclass= "Alias_user" > INSERT into User (ID, user_name,
User_code, PASSWORD, ACTIVE, Create_time, Modify_time, Create_user_code, Modify_user_code) VALUES (#id #, #userName #, #userCode #, #password #, #active #, #createTime #, #modifyTime #, #createUser #, #modifyUse
r#); </insert> </sqlMap>
In Sqlmapconfig.xml:
<!--introduced mapper file-->
<sqlmap resource= "/com/zengmeizan/charge/domain/sqlmap/usermapper.xml"/>
Step fifth, create a persistent data-tier DAO interface, and its implementation class, create a Business layer service interface, and his implementation class. The implementation class of the DAO interface also inherits the Sqlmapclientdaosupport abstract class, and the implementation class remembers to specify the namespace. Because of the integration of the management framework through spring, the DAO layer is dependent on the Applicationcontext.xml injection Service (the action is also due to the injection to the service layer)
Userdaoimpl class
/**
* Userdao Implementation class
* @author Leo * * */Public
class Userdao extends Sqlmapclientdaosupport Implements Iuserdao {public
static final String NAMESPACE = "user.";
/**
* NEW data * *
@Override public
int AddUser (userentity userentity) {
date now =new date ();
Userentity.setid (Uuidutils.getuuid ());
Userentity.setactive ("Y");
Userentity.setcreatetime (now);
Userentity.setmodifytime (now);
Userentity result= (userentity) this.getsqlmapclienttemplate (). Insert (namespace+ "Insertoneuser", UserEntity);
return result!=null?1:0
}
}
Userserviceimpl class:
/**
* *
@author Leo * */Public
class UserService implements Iuserservice {
/**
* userdao< c7/>*/
private Iuserdao Userdao;
public void Setuserdao (Iuserdao userdao) {
This.userdao = Userdao;
}
/**
* New User
*
/@Transactional @Override public
int AddUser (userentity userentity) {
if (Null ==userentity | | Stringutils.isblank (Userentity.getusercode ())) {
throw new Userexception ("parameter is Empty");
}
Userentity.setactive ("Y");
Userentity.setcreateuser ("USER");
Userentity.setcreatetime (New Date ());
Return Userdao.adduser (userentity);
}
In Applicationcontext.xml
<!--spring injection relies on-->
<!--DAO-->
<bean id= "Userdao" class= "Com.zengMeizan.charge.dao.impl.UserDao" >
<property name= "sqlmapclient" ref= "Sqlmapclient"/>
</bean>
<!--service-->
<bean id= "UserService" class= "Com.zengMeizan.charge.service.impl.UserService" >
<property name= "Userdao" ref= "Userdao"/>
</bean>
<!--action-->
<bean id= "useraction" class= "Com.zengMeizan.charge.action.UserAction" >
<property name= "UserService" ref= "UserService"/>
</bean>
Step sixth, create Useraction, and configure Strut2.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en"
"http://struts.apache.org/ Dtds/struts-2.1.dtd ">
<struts>
<include file=" struts-user.xml/> <include
file= Struts-main.xml "/>
<!--struts2 Development mode--> <constant name=" Struts-devmodel "value=
" true "/>
<!--Use JSON must inherit Josn-default-->
<package name= "Charge-default" extends= "Json-default" >
<!--define a global result set-->
<global-results>
<result>/WEB-INF/jsp/fail.jsp</result>
</global-results>
</package>
</struts>
In Struts-user.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en"
"http://struts.apache.org/ Dtds/struts-2.1.dtd ">
<struts>
<!--user login, add, remove Modify user's struts-->
<package name=" charge _login "namespace="/login "extends=" Charge-default "> <action name=" user_adduser "class=" UserAction "
method= "AddUser" >
<result name= "Success" >/WEB-INF/jsp/success.jsp</result>
</action >
</package>
In the Useraction class
/**
* *
@author Leo * */Public
class Useraction extends Baseaction {
/**
*
* * Private static final long serialversionuid = -5421622271053463421l;
/**
* Service
/private Iuserservice userservice;
/**
* Vo the body of the interaction between front and back
/private Userentityvo vo;
Public Userentityvo Getvo () {return
vo;
}
public void Setvo (Userentityvo vo) {
this.vo = vo;
}
public void Setuserservice (Iuserservice userservice) {
this.userservice = userservice;
}
/**
* New User
* @return
/Public String AddUser () {
try {
//get user
Userentity userentity = vo.getuserentity ();
Userservice.adduser (userentity);
Return "Success";
} catch (Userexception e) {return
"error";}}}
Finally, in the Webroot under the index.jsp Create a form simulation add user, start Tomcat, add a new entity test, if the record in the database to explain the framework to build success.