I summarized three methods in the previous article, but there are two methods that annotate SQL. This method is quite confusing, so everyone doesn't use it very much. Let's summarize the two commonly used methods:
1. Dynamic proxy implementation class without writing dao
This method is relatively simple. You don’t need to implement the dao layer, you just need to define the interface. Here it is just to record the configuration file, so the program is very simple:
1. Overall structure diagram:
2. Three configuration files and one mapping file
(1) The program entrance and front-end controller configuration web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>website1</display-name> <!-- Set up listening and automatically assemble the configuration information of ApplicationContext when the web container starts --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Set Spring container loading configuration file path --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/springmvc-servlet.xml, classpath:config/ApplicationContext.xml </param-value> </context-param> <!-- Character encoding filter--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <!-- Front-end controller--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springmvc-servlet.xml</param-value> </init-param> <!-- This configuration file is loaded when the container starts--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- Intercept request--> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
(2) Scan control layer, automatic injection and view parser configuration springmvc-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> <!-- Annotation driver --> <mvc:annotation-driven /> <!-- <context:annotation-config /> --> <!-- context:component-scan has the function of annotation-config --> <!-- Scan control layer --> <context:component-scan base-package="com.website.controller"></context:component-scan> <!-- View parser--> <bean id="viewResolver"> <property name="prefix" value="/WEB-INF/view/"> </property> <property name="suffix" value=".jsp"></property> </beans>
(3) Data source, service automatic scanning and injection, spring managed mybatissqlsessionFactory, dao layer interface dynamic proxy, and transaction configuration ApplicationContext.xml
There will be multiple configuration files here
1) Single data source, dynamic proxy does not set the values of the two properties of sqlSessionFactoryBeanName or sqlSessionTemplateBeanName when implementing the dao layer interface.
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Load configuration JDBC file --> <context:property-placeholder location="classpath:db.properties" /> <!-- Data source--> <bean id="dataSource"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- Turn on the annotation configuration, namely Autowried --> <!-- <context:annotation-config/> --> <!-- In fact, component-scan has the function of annotation-config, that is, register the required classes in the spring container --> <context:component-scan base-package="com.website.service" /> <!-- When using mybatis, spring uses sqlsessionFactoryBean to manage mybatis' sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis configuration file path --> <property name="configLocation" value="" /> <!-- Entity class map file path, only one of them is written to the end, and multiple ones can be replaced by mybatis/*.xml --> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <!-- <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/> </constructor-arg> </bean> --> <!-- Dynamic proxy implementation does not need to write dao--> <bean id="MapperScannerConfigurer"> <!-- Here basePackage The dao layer interface power is specified, and the dao interface here does not need to be implemented by itself --> <property name="basePackage" value="com.website.dao" /> <!-- If there is only one data source, you can not specify it, but if there are multiple data sources, you must specify it --> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> <!-- directly specify the sqlsessionTemplate name, which is actually the same as the above --> <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> --> </bean> <!-- Transaction Manager--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Using full annotation transaction--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>2) Configure the property value of sqlSessionFactoryBeanName for a single data source
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Load configuration JDBC file --> <context:property-placeholder location="classpath:db.properties" /> <!-- Data source--> <bean id="dataSource"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- Turn on the annotation configuration, namely Autowried --> <!-- <context:annotation-config/> --> <!-- In fact, component-scan has the function of annotation-config, that is, register the required classes in the spring container --> <context:component-scan base-package="com.website.service" /> <!-- When using mybatis, spring uses sqlsessionFactoryBean to manage mybatis' sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis configuration file path --> <property name="configLocation" value="" /> <!-- Entity class map file path, only one of them is written to the end, and multiple ones can be replaced by mybatis/*.xml --> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <!-- <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/> </constructor-arg> </bean> --> <!-- Dynamic proxy implementation does not need to write dao--> <bean id="MapperScannerConfigurer"> <!-- Here basePackage The dao layer interface power is specified, and the dao interface here does not need to be implemented by itself --> <property name="basePackage" value="com.website.dao" /> <!-- If there is only one data source, you can not specify it, but if there are multiple data sources, you must specify it --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- Directly formulated the sqlsessionTemplate name, which is actually the same as the above --> <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> --> </bean> <!-- Transaction Manager--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Using full annotation transaction--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>3) Configure the sqlSessionTemplateBeanName property value for a single data source
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Load configuration JDBC file --> <context:property-placeholder location="classpath:db.properties" /> <!-- Data source--> <bean id="dataSource"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- Turn on the annotation configuration, namely Autowried --> <!-- <context:annotation-config/> --> <!-- In fact, component-scan has the function of annotation-config, that is, register the required classes in the spring container --> <context:component-scan base-package="com.website.service" /> <!-- When using mybatis, spring uses sqlsessionFactoryBean to manage mybatis' sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis configuration file path --> <property name="configLocation" value="" /> <!-- Entity class map file path, only one of them is written to the end, and multiple ones can be replaced by mybatis/*.xml --> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean> <!-- Dynamic proxy implementation does not require writing dao --> <bean id="MapperScannerConfigurer"> <!-- BasePackage here The dao layer interface power is specified, and the dao interface here does not need to be implemented by itself --> <property name="basePackage" value="com.website.dao" /> <!-- If there is only one data source, you can not specify it, but if there are multiple data sources, you must specify it --> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> <!-- Directly formulate the sqlsessionTemplate name, which is actually the same as the above --> <property name="sqlSessionTemplateBeanName" value="sqlSession" /> </bean> <!--Transaction Manager--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Using full annotation transaction--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>4) Multiple data sources
Note that if it is a multi-data source, you must use sqlSessionFactoryBeanName or sqlSessionTemplateBeanName to specify the specific data source. I don’t know if you noticed it in the above configuration. If you use sqlSessionTemplateBeanName, you must
<bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean>
To create a specific instance and assign it to the sqlSessionTemplateBeanName property.
(4), mybatis SQL mapping file userMapper.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"> <!-- The value of namespace is the complete path of the dao interface. For this demo, namespace is the complete path of userDao.java--> <mapper namespace="com.website.dao.UserDao"> <!-- The id here is the name of the method in the interface--> <insert id="saveUser" parameterType="java.util.Map"> insert into user(id,name) values(#{id},#{name}) </insert> </mapper>OK Let’s go to the configuration file and get it done. Let’s take a look at the control layer, business logic layer and dao layer code.
3. Controller layer
package com.website.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.website.service.UserService; @Controller @RequestMapping(value = "/user") public class UserController { // Inject userService object @Autowired private UserService userService; @RequestMapping(value = "/save.do", method = RequestMethod.GET) public String saveUser(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); String name = request.getParameter("name"); Map<String, String> map = new HashMap<String, String>(); map.put("id", id); map.put("name", name); userService.saveUser(map); return "index"; } }4. Service layer
package com.website.service; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.website.dao.UserDao; @Service("userService") @Transactional public class UserService { // Injection dao interface implementation class instance// Both injection methods can be @Autowired private UserDao userDao; public void saveUser(Map<String, String> map) { int end = userDao.saveUser(map); System.out.println("end:" + end); } }5. Dao Layer Interface
package com.website.dao; import java.util.Map; //com.website.dao.UserDao public interface UserDao { int saveUser(Map<String, String> map); } The complete path of the dao interface is the namespace of the mapping file corresponding to this dao interface, and the method name is the value of the id
OK, this configuration method is over, and there is also a complete small demo. Let’s briefly summarize it below:
Compared with the previous configuration method (which will be written below) is that it uses the dynamic proxy method of the dao layer interface. Before, we will manually implement the dao layer in the dao layer and then automatically inject the SqlSessionTemplate instance to call specific methods such as insert("","") selectOne("","") and other methods. The first parameter is the address of the mapping file: namespace+id, and the second parameter is the passed conditions. In this way, mybatis will find the specific mapping file according to the two parameters we passed for parsing and querying. Using dynamic proxy here eliminates the step of implementing the dao interface, but we implemented it by spring. There is a problem. We passed the query condition parameters, but the specific path of the mapping file is: namespce+id. What should I do if it is not passed? That is, the namespace of your mapping file must be the full name of the interface class and id must be the method name in the interface, so that the dynamic proxy can find the path and also has parameters. Do you think it's the same? Hahaha!
2. Manually implement the dao layer interface
Let’s first take a look at the configuration and code for manually implementing the dao layer:
1. Structure diagram of topic
2. Three configuration files and mapping files
(1) Program entry, front-end controller configuration web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>website2</display-name> <!-- Load spring container configuration--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Set the Spring container loading configuration file path --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/springmvc-servlet.xml, classpath:config/ApplicationContext.xml </param-value> </context-param> <!-- Character encoding filter--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <!-- Front-end controller --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springmvc-servlet.xml</param-value> </init-param> <!-- This configuration file is loaded when the container starts --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- Intercept request--> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
(2) Scan control layer, automatic injection and view parser configuration springmvc-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> <!-- Annotation driver --> <mvc:annotation-driven /> <!-- <context:annotation-config /> --> <!-- Scan --> <context:component-scan base-package="com.website.controller"></context:component-scan> <!-- View parser--> <bean id="viewResolver" > <property name="prefix" value="/WEB-INF/view/"> </property> <property name="suffix" value=".jsp"></property> </bean> </beans>
(3) Data source, service automatic scanning and injection, spring managed mybatissqlsessionFactory and transaction configuration 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Load configuration JDBC file --> <context:property-placeholder location="classpath:db.properties" /> <!-- Data source--> <bean id="dataSource" > <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <!-- Turn on the annotation configuration, namely Autowried --> <!--component-scan has the function of annotation-config, that is, inject the required classes into the spring container --> <!--<context:annotation-config/> --> <!-- When using automatic injection, add it to scan the bean before it can be used --> <context:component-scan base-package="com.website.service ,com.website.dao" /> <!-- When using mybatis, spring uses sqlsessionFactoryBean to manage mybatis' sqlsessionFactory --> <!-- When using interfaces, the method of using sqlsessionTemplate is used to operate. It provides some methods --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis configuration file path --> <property name="configLocation" value="" /> <!-- Entity class map file path, there must be multiple map files in development, so mybatis/*.xml is used instead --> <property name="mapperLocations" value="classpath:mybatis/UserMapping.xml" /> </bean> <!--In fact, the example of this class is SQLSession in mybatis --> <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean> <bean id="transactionManager" > <property name="dataSource" ref="dataSource" /> </bean> <!--Use full annotation transaction--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>(4) Mybatis mapping file
<?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"> <!--This namespace + the id below is a complete path. After we write the complete path in the dao layer mybatis is the mapping of the relevant sql statements in this file --> <mapper namespace="com.website.userMapper"> <!-- parameterType is the type of parameter you accept, --> <!-- Add user information --> <insert id="insertUser" parameterType="java.util.Map"> insert into user(id,name,password) values(#{id},#{name},#{password}) </insert> </mapper>You may see that the namespace +id of the mapping file here is customized instead of the full class name +id of the dao layer interface.
3. Controller
package com.website.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.website.service.UserService; /** * @author WHD data June 5, 2016*/ @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/save.do") public String saveUser(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); String name = request.getParameter("name"); String password = request.getParameter("password"); Map<String, String> map = new HashMap<String, String>(); map.put("id", id); map.put("name", name); map.put("password", password); userService.saveUser(map); return "index"; } }4. Business logic layer service
package com.website.service; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.website.dao.UserDao; /** * @author WHD data June 5, 2016*/ @Service("userService") @Transactional public class UserService { @Autowired private UserDao userDao; public void saveUser(Map<String, String> map) { userDao.saveUser(map); } }5. Dao Layer
package com.website.dao; import java.util.Map; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * @author WHD data June 5, 2016*/ @Repository("userDao") public class UserDao { @Autowired private SqlSessionTemplate sqlSession; public void saveUser(Map<String, String> map) { int end = sqlSession.insert("com.website.userMapper.insertUser", map); System.out.println("end" + end); } }We look at the SqlSessionTemplate of the inverted dao layer. This is actually the SqlSession object in mybatis. We see that it is configured in ApplicationContext.xml, so when we use it, spring will automatically inject it. We can use it directly without creating it ourselves. This is the so-called control inversion. OK The configuration methods of these two files are over.
Summarize
The above is a detailed explanation of the configuration methods of spring and mybatis introduced to you (two commonly used methods). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!