spring、mybatis 配置方式詳解(常用兩種方式)
在之前的文章中總結(jié)了三種方式,但是有兩種是注解sql的,這種方式比較混亂所以大家不怎么使用,下面總結(jié)一下常用的兩種總結(jié)方式:
一、 動態(tài)代理實現(xiàn) 不用寫dao的實現(xiàn)類
這種方式比較簡單,不用實現(xiàn)dao層,只需要定義接口就可以了,這里只是為了記錄配置文件所以程序?qū)懙暮芎唵危?/p>
1、整體結(jié)構(gòu)圖:
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> <!-- 設(shè)置監(jiān)聽,在web容器啟動時自動裝配ApplicationContext的配置信息--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 設(shè)置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"> <!-- 注解驅(qū)動 --> <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" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"> </property> <property name="suffix" value=".jsp"></property> </bean> </beans>
(3)、數(shù)據(jù)源、service 自動掃描注入、spring代管mybatissqlsessionFactory 、dao層接口動態(tài)代理以及事務(wù)的配置ApplicationContext.xml
這里會有多中配置文件
1)、單數(shù)據(jù)源,動態(tài)代理實現(xiàn)dao層接口時不設(shè)置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" /> <!-- 數(shù)據(jù)源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑 --> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,這里只有一個就寫死了,多個可以使用mybatis/*.xml來替代 --> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/> </constructor-arg> </bean> --> <!--動態(tài)代理實現(xiàn) 不用寫dao的實現(xiàn) --> <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 這里的basePackage 指定了dao層接口路勁,這里的dao接口不用自己實現(xiàn) --> <property name="basePackage" value="com.website.dao" /> <!-- 如果只有一個數(shù)據(jù)源的話可以不用指定,但是如果有多個數(shù)據(jù)源的話必須要指定 --> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> <!--直接指定了sqlsessionTemplate名稱,這個和上面的其實是一樣的 --> <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> --> </bean> <!--事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用全注釋事務(wù) --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
2)、單數(shù)據(jù)源配置 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" /> <!-- 數(shù)據(jù)源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑 --> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,這里只有一個就寫死了,多個可以使用mybatis/*.xml來替代 --> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/> </constructor-arg> </bean> --> <!--動態(tài)代理實現(xiàn) 不用寫dao的實現(xiàn) --> <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 這里的basePackage 指定了dao層接口路勁,這里的dao接口不用自己實現(xiàn) --> <property name="basePackage" value="com.website.dao" /> <!-- 如果只有一個數(shù)據(jù)源的話可以不用指定,但是如果有多個數(shù)據(jù)源的話必須要指定 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!--直接制定了sqlsessionTemplate名稱,這個和上面的其實是一樣的 --> <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> --> </bean> <!--事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用全注釋事務(wù) --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
3)、單數(shù)據(jù)源配置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" /> <!-- 數(shù)據(jù)源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑 --> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,這里只有一個就寫死了,多個可以使用mybatis/*.xml來替代 --> <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" /> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean> <!--動態(tài)代理實現(xiàn) 不用寫dao的實現(xiàn) --> <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 這里的basePackage 指定了dao層接口路勁,這里的dao接口不用自己實現(xiàn) --> <property name="basePackage" value="com.website.dao" /> <!-- 如果只有一個數(shù)據(jù)源的話可以不用指定,但是如果有多個數(shù)據(jù)源的話必須要指定 --> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> <!--直接制定了sqlsessionTemplate名稱,這個和上面的其實是一樣的 --> <property name="sqlSessionTemplateBeanName" value="sqlSession" /> </bean> <!--事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用全注釋事務(wù) --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
4)、多數(shù)據(jù)源
注意如果是多數(shù)據(jù)源則一定要使用sqlSessionFactoryBeanName 或sqlSessionTemplateBeanName 來指定具體的數(shù)據(jù)源,不知道在上面的配置中有沒有注意到,如果使用sqlSessionTemplateBeanName 的話要
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean>
來創(chuàng)建具體的實例并賦值給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 到這里配置文件到搞定了下面來看看控制層,業(yè)務(wù)邏輯層以及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接口實現(xiàn)類實例 // @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 接口對應(yīng)的那個映射文件的namespace 而方法名就是 id的值
ok到這里這種配置方式都完了,也有了一個完整的小demo,下面我們簡單總結(jié)一下:
這種配置方式相比之前的配置方式(下面也會寫出來)特別之處就是他使用了dao層接口的動態(tài)代理方式實現(xiàn)了,之前我們會在dao層自己手動實現(xiàn)dao層然后自動注入SqlSessionTemplate 實例來調(diào)用具體的方法 比如 insert("","") selectOne("","") 等方法 其中第一個參數(shù)就是映射文件的地址: namespace+id 而第二個參數(shù)就是傳遞的條件這樣mybatis 就會按照我們傳遞的這兩個參數(shù)找到具體的映射文件進行解析查詢。而這里使用動態(tài)代理就省去了我們實現(xiàn)dao接口的這一步驟,而是由spring提我們實現(xiàn)了,那有個問題,查詢條件參數(shù)我們傳遞了,但映射文件的具體路徑即:namespce+id 沒有傳遞怎么辦,那就是你的映射文件的namespace 必須是接口的類全名稱而id 必須是接口中的方法名稱,這樣動態(tài)代理就能找到路勁了也有了參數(shù)了。 這樣一來是不是覺得就一樣了啊哈哈哈!
二、手動實現(xiàn)dao層接口
下面先來看看手動實現(xiàn)dao層的配置以及代碼:
1、正題結(jié)構(gòu)圖
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> <!-- 設(shè)置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"> <!-- 注解驅(qū)動 --> <mvc:annotation-driven /> <!-- <context:annotation-config /> --> <!-- 掃描 --> <context:component-scan base-package="com.website.controller"></context:component-scan> <!-- 視圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"> </property> <property name="suffix" value=".jsp"></property> </bean> </beans>
(3)、數(shù)據(jù)源、service 自動掃描注入、spring代管mybatissqlsessionFactory 以及事務(wù)的配置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" /> <!-- 數(shù)據(jù)源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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 --> <!-- 而像這種使用接口實現(xiàn)的方式 是使用sqlsessionTemplate來進行操作的,他提供了一些方法 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件路徑 --> <property name="configLocation" value="" /> <!-- 實體類映射文件路徑,在開發(fā)中映射文件肯定是多個所以使用mybatis/*.xml來替代 --> <property name="mapperLocations" value="classpath:mybatis/UserMapping.xml" /> </bean> <!--其實這里類的實例就是mybatis中SQLSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--使用全注釋事務(wù) --> <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就是映射這個文件中的相關(guān)sql語句 --> <mapper namespace="com.website.userMapper"> <!-- parameterType就是你接受的參數(shù)的類型, --> <!-- 添加用戶信息 --> <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、業(yè)務(wù)邏輯層 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會幫我們自動注入,我們直接使用就可以了不用去自己創(chuàng)建,這也就是所謂的控制反轉(zhuǎn)。ok 到此兩種文件的配置方式就結(jié)束了。
總結(jié)
以上所述是小編給大家介紹的spring、mybatis 配置方式詳解(常用兩種方式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
springboot實現(xiàn)添加郵件發(fā)送及壓縮功能
這篇文章主要介紹了springboot實現(xiàn)添加郵件發(fā)送及壓縮功能 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07mybatis-plus內(nèi)置雪花算法主鍵重復問題解決
本文主要介紹了mybatis-plus內(nèi)置雪花算法主鍵重復問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-09-09