在之前的文章中總結了三種方式,但是有兩種是註解sql的,這種方式比較混亂所以大家不怎麼使用,下面總結一下常用的兩種總結方式:
一、 動態代理實現不用寫dao的實現類
這種方式比較簡單,不用實現dao層,只需要定義接口就可以了,這裡只是為了記錄配置文件所以程序寫的很簡單:
1、整體結構圖:
2、三個配置文件以及一個映射文件
(1)、程序入口以及前端控制器配置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/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>website1</display-name> <!-- 設置監聽,在web容器啟動時自動裝配ApplicationContext的配置信息--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 設置Spring容器加載配置文件路徑--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/springmvc-servlet.xml, classpath:config/ApplicationContext.xml </param-value> </context-param> <!-- 字符編碼過濾器--> <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> <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> <!-- 前端控制器--> <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> <!-- 這個配置文件在容器啟動的時候就加載--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 攔截請求--> <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)、掃描控制層、自動注入以及視圖解析器的配置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/context http://www.springframework.org/schema/context/spring-context-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/spring-cache-3.1.xsd"> <!-- 註解驅動--> <mvc:annotation-driven /> <!-- <context:annotation-config /> --> <!-- context:component-scan 具有annotation-config 的功能--> <!-- 掃描控制層--> <context:component-scan base-package="com.website.controller"></context:component-scan> <!-- 視圖解析器--> <bean id="viewResolver"> <property name="prefix" value="/WEB-INF/view/"> </property> <property name="suffix" value=".jsp"></property> </bean> </beans>
(3)、數據源、service 自動掃描注入、spring代管mybatissqlsessionFactory 、dao層接口動態代理以及事務的配置ApplicationContext.xml
這裡會有多中配置文件
1)、單數據源,動態代理實現dao層接口時不設置sqlSessionFactoryBeanName、或sqlSessionTemplateBeanName 兩個屬性的值
<?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"> <!-- 加載配置JDBC文件--> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據源--> <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> <!-- 開啟註解配置即Autowried --> <!-- <context:annotation-config/> --> <!--其實component-scan 就有了annotation-config的功能即把需要的類註冊到了spring容器中--> <context:component-scan base-package="com.website.service" /> <!-- 在使用mybatis時spring使用sqlsessionFactoryBean 來管理mybatis的sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑--> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,這裡只有一個就寫死了,多個可以使用mybatis/*.xml來替代--> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <!-- <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/> </constructor-arg> </bean> --> <!--動態代理實現不用寫dao的實現--> <bean id="MapperScannerConfigurer"> <!-- 這裡的basePackage 指定了dao層接口路勁,這裡的dao接口不用自己實現--> <property name="basePackage" value="com.website.dao" /> <!-- 如果只有一個數據源的話可以不用指定,但是如果有多個數據源的話必須要指定--> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> <!--直接指定了sqlsessionTemplate名稱,這個和上面的其實是一樣的--> <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> --> </bean> <!--事務管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用全註釋事務--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>2)、單數據源配置sqlSessionFactoryBeanName 這個屬性值
<?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"> <!-- 加載配置JDBC文件--> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據源--> <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> <!-- 開啟註解配置即Autowried --> <!-- <context:annotation-config/> --> <!--其實component-scan 就有了annotation-config的功能即把需要的類註冊到了spring容器中--> <context:component-scan base-package="com.website.service" /> <!-- 在使用mybatis時spring使用sqlsessionFactoryBean 來管理mybatis的sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑--> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,這裡只有一個就寫死了,多個可以使用mybatis/*.xml來替代--> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <!-- <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/> </constructor-arg> </bean> --> <!--動態代理實現不用寫dao的實現--> <bean id="MapperScannerConfigurer"> <!-- 這裡的basePackage 指定了dao層接口路勁,這裡的dao接口不用自己實現--> <property name="basePackage" value="com.website.dao" /> <!-- 如果只有一個數據源的話可以不用指定,但是如果有多個數據源的話必須要指定--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!--直接製定了sqlsessionTemplate名稱,這個和上面的其實是一樣的--> <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> --> </bean> <!--事務管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用全註釋事務--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>3)、單數據源配置sqlSessionTemplateBeanName 這個屬性值
<?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"> <!-- 加載配置JDBC文件--> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據源--> <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> <!-- 開啟註解配置即Autowried --> <!-- <context:annotation-config/> --> <!--其實component-scan 就有了annotation-config的功能即把需要的類註冊到了spring容器中--> <context:component-scan base-package="com.website.service" /> <!-- 在使用mybatis時spring使用sqlsessionFactoryBean 來管理mybatis的sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑--> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,這裡只有一個就寫死了,多個可以使用mybatis/*.xml來替代--> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean> <!--動態代理實現不用寫dao的實現--> <bean id="MapperScannerConfigurer"> <!-- 這裡的basePackage 指定了dao層接口路勁,這裡的dao接口不用自己實現--> <property name="basePackage" value="com.website.dao" /> <!-- 如果只有一個數據源的話可以不用指定,但是如果有多個數據源的話必須要指定--> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> <!--直接製定了sqlsessionTemplate名稱,這個和上面的其實是一樣的--> <property name="sqlSessionTemplateBeanName" value="sqlSession" /> </bean> <!--事務管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用全註釋事務--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>4)、多數據源
注意如果是多數據源則一定要使用sqlSessionFactoryBeanName 或sqlSessionTemplateBeanName 來指定具體的數據源,不知道在上面的配置中有沒有註意到,如果使用sqlSessionTemplateBeanName 的話要
<bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean>
來創建具體的實例並賦值給sqlSessionTemplateBeanName 這個屬性。
(4)、mybatis SQL映射文件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"> <!-- namespace的值就是dao接口的完整路勁,就這個demo而言namespace 就是userDao.java的完整路勁--> <mapper namespace="com.website.dao.UserDao"> <!-- 這裡的id就是接口中方法的名稱--> <insert id="saveUser" parameterType="java.util.Map"> insert into user(id,name) values(#{id},#{name}) </insert> </mapper>ok 到這裡配置文件到搞定了下面來看看控制層,業務邏輯層以及dao層的代碼。
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 org.springframework.web.bind.annotation.RequestMethod; import com.website.service.UserService; @Controller @RequestMapping(value = "/user") public class UserController { // 注入userService 對象@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層
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 { // 注入dao接口實現類實例// @Resource、@Autowired兩種注入方式都可以@Autowired private UserDao userDao; public void saveUser(Map<String, String> map) { int end = userDao.saveUser(map); System.out.println("end:" + end); } }5、dao 層接口
package com.website.dao; import java.util.Map; //com.website.dao.UserDao public interface UserDao { int saveUser(Map<String, String> map); } dao 接口的完整路勁就是這個dao 接口對應的那個映射文件的namespace 而方法名就是id的值
ok到這裡這種配置方式都完了,也有了一個完整的小demo,下面我們簡單總結一下:
這種配置方式相比之前的配置方式(下面也會寫出來)特別之處就是他使用了dao層接口的動態代理方式實現了,之前我們會在dao層自己手動實現dao層然後自動注入SqlSessionTemplate 實例來調用具體的方法比如insert("","") selectOne("","") 等方法其中第一個參數就是映射文件的地址: namespace+id 而第二個參數就是傳遞的條件這樣mybatis 就會按照我們傳遞的這兩個參數找到具體的映射文件進行解析查詢。而這裡使用動態代理就省去了我們實現dao接口的這一步驟,而是由spring提我們實現了,那有個問題,查詢條件參數我們傳遞了,但映射文件的具體路徑即:namespce+id 沒有傳遞怎麼辦,那就是你的映射文件的namespace 必須是接口的類全名稱而id 必須是接口中的方法名稱,這樣動態代理就能找到路勁了也有了參數了。 這樣一來是不是覺得就一樣了啊哈哈哈!
二、手動實現dao層接口
下面先來看看手動實現dao層的配置以及代碼:
1、正題結構圖
2、三個配置文件以及映射文件
(1)、程序入口,前端控制器配置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/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>website2</display-name> <!-- 加載spring容器配置--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 設置Spring容器加載配置文件路徑--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/springmvc-servlet.xml, classpath:config/ApplicationContext.xml </param-value> </context-param> <!-- 字符編碼過濾器--> <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> <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> <!-- 前端控制器--> <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> <!-- 這個配置文件在容器啟動的時候就加載--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 攔截請求--> <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)、掃描控制層、自動注入以及視圖解析器的配置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/context http://www.springframework.org/schema/context/spring-context-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/spring-cache-3.1.xsd"> <!-- 註解驅動--> <mvc:annotation-driven /> <!-- <context:annotation-config /> --> <!-- 掃描--> <context:component-scan base-package="com.website.controller"></context:component-scan> <!-- 視圖解析器--> <bean id="viewResolver" > <property name="prefix" value="/WEB-INF/view/"> </property> <property name="suffix" value=".jsp"></property> </bean> </beans>
(3)、數據源、service 自動掃描注入、spring代管mybatissqlsessionFactory 以及事務的配置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"> <!-- 加載配置JDBC文件--> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據源--> <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> <!-- 開啟註解配置即Autowried --> <!--component-scan擁有annotation-config的功能即註入需要的類到spring容器中--> <!--<context:annotation-config/> --> <!--使用自動注入的時候要添加他來掃描bean之後才能在使用的時候--> <context:component-scan base-package="com.website.service ,com.website.dao" /> <!-- 在使用mybatis時spring使用sqlsessionFactoryBean 來管理mybatis的sqlsessionFactory --> <!-- 而像這種使用接口實現的方式是使用sqlsessionTemplate來進行操作的,他提供了一些方法--> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑--> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,在開發中映射文件肯定是多個所以使用mybatis/*.xml來替代--> <property name="mapperLocations" value="classpath:mybatis/UserMapping.xml" /> </bean> <!--其實這裡類的實例就是mybatis中SQLSession --> <bean id="sqlSession"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean> <bean id="transactionManager" > <property name="dataSource" ref="dataSource" /> </bean> <!--使用全註釋事務--> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>(4)、mybatis 映射文件
<?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"> <!--這個namespace + 下面的id 就是一個完整的路徑,在dao層我們寫了完整的路徑之後mybatis就是映射這個文件中的相關sql語句--> <mapper namespace="com.website.userMapper"> <!-- parameterType就是你接受的參數的類型, --> <!-- 添加用戶信息--> <insert id="insertUser" parameterType="java.util.Map"> insert into user(id,name,password) values(#{id},#{name},#{password}) </insert> </mapper>你可能看到了這裡的映射文件的namespace +id 是自定義的而不是dao 層接口的全類名+id
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 2016年6月5日*/ @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、業務邏輯層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 2016年6月5日*/ @Service("userService") @Transactional public class UserService { @Autowired private UserDao userDao; public void saveUser(Map<String, String> map) { userDao.saveUser(map); } }5、dao層
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 2016年6月5日*/ @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); } }我們看倒dao層的SqlSessionTemplate 這個其實是mybatis中的SqlSession 對象,我們看到在ApplicationContext.xml 中配置了,所以在我們使用時spring會幫我們自動注入,我們直接使用就可以了不用去自己創建,這也就是所謂的控制反轉。 ok 到此兩種文件的配置方式就結束了。
總結
以上所述是小編給大家介紹的spring、mybatis 配置方式詳解(常用兩種方式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!