If you are not clear about these three frameworks, it is recommended that you understand each frame first and then see that the pressure is smaller.
The first thing you need to know is how each framework plays a role in integration and how they relate to each other.
1, spring is generally used to manage the data source, inject MyBatis, struts2 the properties of the configured JavaBean
2, MyBatis is responsible for providing the operation of the database, his database configuration connection operation will be given to the spring configuration file
3, STRUTS2 is responsible for the front-end response, (if you want to manipulate the database in action) by encapsulating an instance of the class that operates the database (managed by MyBatis), which can be injected into the configuration file by spring, and render the data results to the page
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 "> <welcome-file-list> <welcome-file>index.jsp</ Welcome-file> </welcome-file-list> <!--indicate where spring configuration files are--<context-param> <PARAM-NAME&G T;contextconfiglocation</param-name> <param-value>/web-inf/lib/applicationcontext.xml</ Param-value> </context-param> <!--load Spring configuration file Applicationcontext.xml--<listener> <liste Ner-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> < !--Configuring STRUTS2 Files--<filter> <filter-name>struts2</filter-name> <filter-class>org.apach e.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <u Rl-pattern>/*</url-pattern> </filter-mapping> </web-app>
struts.xml file (I use the version is 2.3.16.1, different versions to change the corresponding content)
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/ Dtds/struts-2.3.dtd "> <struts> <package name=" Default "namespace=" "extends=" Struts-default "> <a ction name= "testaction" class= "com.icreate.service.testAction" > <result name= "Success" >/index.jsp</ result> </action> </package> <package name= "Ajaxdemo" namespace= "extends=" Json-default "> < !--return JSON data--<action name= "Tajax" class= "Com.icreate.service.tAjax" method= "LoadData" > <result name= "Success" type= "JSON" > <param name= "root" >map</param> </result> </action> <action Name= "GetPos" class= "Com.icreate.service.tAjax" method= "loadposition" > <result name= "position" type= "JSON" > <param name= "root" >posList</param> </result> </action> </package>
</struts>
The data query mapping file in the MyBatis configuration file (mapper), where the MyBatis version is 3.2.0
User.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.icreate.dao.UserDao" > <resultmap type= "com.icreate.entity.User" id= "Resultlist" > <id column= "id" property= "id"/> <result column= "username" property= "username"/> <result column= "Passwo
Rd "property=" password "/> </resultMap> <!--The total number of records in the query table--<select id=" Countall "resulttype=" int "> Select COUNT (*) c from Tb_user </select> <select id= "SelectAll" resultmap= "resultlist" > select * from TB _user ORDER BY username ASC </select> <insert id= "Insert" parametertype= "Com.icreate.entity.User" > Inser T into Tb_user (Username,password) VALUES (#{username},#{password}) </insert> <update id= "Update" Parametertyp E= "Com.icreate.entity.User" > Update tb_user set Username=#{username},password=#{password} where UsernamE=#{username} </update> <delete id= "delete" parametertype= "String" > Delete form tb_user where username=# {username} </delete> <select id= "Findbyusername" parametertype= "String" resulttype= " Com.icreate.entity.User "> select * from Tb_user where username=#{username} </select> </mapper>
config file for MyBatis
Mybatis-configuration.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>
<mappers>
<mapper resource= "Com/icreate/dao/userdao.xml"/>
</mappers>
</configuration>
Spring's configuration file, this is the key to the combination, it manages the connection of the MyBatis with the database, and the injection of various attributes
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-3.1.xsd "> <!--configuration data source-<bean id=" DataSource " class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "driverclassname" value= " Com.mysql.jdbc.Driver "></property> <property name=" url "value=" Jdbc:mysql://localhost:3306/myshop? Characterencoding=utf8 "></property> <property name=" username "value=" root "></property> < Property name= "Password" value= "5817513" ></property> </bean> <!--configuration transaction Manager--<bean id= " TransactionManager "class=" org.springframework. Jdbc.datasource.DataSourceTransactionManager "> <property Name= "DataSource" ref= "DataSource" ></property> </bean> <!--generate a session factory (mainly for MyBatis to generate instance services), specificallyUnderstanding can be viewed first mybatis tutorial--<bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > < Property Name= "Configlocation" value= "/web-inf/classes/mybatis-configuration.xml" ></property> < Property Name= "DataSource" ref= "DataSource" ></property> </bean> <!--generate a specific instance based on the mapping relationship--< Bean id= "Userdao" class= "Org.mybatis.spring.mapper.MapperFactoryBean" > <property name= "mapperinterface" value = "Com.icreate.dao.UserDao" ></property> <property name= "sqlsessionfactory" ref= "Sqlsessionfactory" > </property> </bean> <!--inject a property in MyBatis--<bean id= "UserService" class= " Com.icreate.service.UserServiceImpl "> <property name=" Userdao "ref=" Userdao "></property> </bean > <!--inject the properties of an action in Struts2, UserService---<bean id= "testaction" class= "com.icreate.service.testAction "> <property name=" userservice "ref=" UserService "> </property> </bean> </beanS>
in action injection, you must require that the name of the variable defined by the action be consistent with the ID name of the injected bean, and no injection fails ~ ~ ~