spring、mybatis 配置方式詳解(常用兩種方式)
在之前的文章中總結(jié)了三種方式,但是有兩種是注解sql的,這種方式比較混亂所以大家不怎么使用,下面總結(jié)一下常用的兩種總結(jié)方式:
一、 動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫(xiě)dao的實(shí)現(xiàn)類(lèi)
這種方式比較簡(jiǎn)單,不用實(shí)現(xiàn)dao層,只需要定義接口就可以了,這里只是為了記錄配置文件所以程序?qū)懙暮芎?jiǎn)單:
1、整體結(jié)構(gòu)圖:

2、三個(gè)配置文件以及一個(gè)映射文件
(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)聽(tīng),在web容器啟動(dòng)時(shí)自動(dòng)裝配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> <!-- 字符編碼過(guò)濾器 --> <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> <!-- 這個(gè)配置文件在容器啟動(dòng)的時(shí)候 就加載 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 攔截請(qǐng)求 --> <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)、掃描控制層、自動(dòng)注入以及視圖解析器的配置 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ū)動(dòng) --> <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 自動(dòng)掃描注入、spring代管mybatissqlsessionFactory 、dao層接口動(dòng)態(tài)代理以及事務(wù)的配置ApplicationContext.xml
這里會(huì)有多中配置文件
1)、單數(shù)據(jù)源,動(dòng)態(tài)代理實(shí)現(xiàn)dao層接口時(shí)不設(shè)置sqlSessionFactoryBeanName、或sqlSessionTemplateBeanName 兩個(gè)屬性的值
<?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>
<!-- 開(kāi)啟注解配置 即Autowried -->
<!-- <context:annotation-config/> -->
<!--其實(shí)component-scan 就有了annotation-config的功能即把需要的類(lèi)注冊(cè)到了spring容器中 -->
<context:component-scan base-package="com.website.service" />
<!-- 在使用mybatis時(shí) spring使用sqlsessionFactoryBean 來(lái)管理mybatis的sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件路徑 -->
<property name="configLocation" value="" />
<!-- 實(shí)體類(lèi)映射文件路徑,這里只有一個(gè)就寫(xiě)死了,多個(gè)可以使用mybatis/*.xml來(lái)替代 -->
<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> -->
<!--動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫(xiě)dao的實(shí)現(xiàn) -->
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 這里的basePackage 指定了dao層接口路勁,這里的dao接口不用自己實(shí)現(xiàn) -->
<property name="basePackage" value="com.website.dao" />
<!-- 如果只有一個(gè)數(shù)據(jù)源的話可以不用指定,但是如果有多個(gè)數(shù)據(jù)源的話必須要指定 -->
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
<!--直接指定了sqlsessionTemplate名稱(chēng),這個(gè)和上面的其實(shí)是一樣的 -->
<!-- <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 這個(gè)屬性值
<?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>
<!-- 開(kāi)啟注解配置 即Autowried -->
<!-- <context:annotation-config/> -->
<!--其實(shí)component-scan 就有了annotation-config的功能即把需要的類(lèi)注冊(cè)到了spring容器中 -->
<context:component-scan base-package="com.website.service" />
<!-- 在使用mybatis時(shí) spring使用sqlsessionFactoryBean 來(lái)管理mybatis的sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件路徑 -->
<property name="configLocation" value="" />
<!-- 實(shí)體類(lèi)映射文件路徑,這里只有一個(gè)就寫(xiě)死了,多個(gè)可以使用mybatis/*.xml來(lái)替代 -->
<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> -->
<!--動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫(xiě)dao的實(shí)現(xiàn) -->
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 這里的basePackage 指定了dao層接口路勁,這里的dao接口不用自己實(shí)現(xiàn) -->
<property name="basePackage" value="com.website.dao" />
<!-- 如果只有一個(gè)數(shù)據(jù)源的話可以不用指定,但是如果有多個(gè)數(shù)據(jù)源的話必須要指定 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--直接制定了sqlsessionTemplate名稱(chēng),這個(gè)和上面的其實(shí)是一樣的 -->
<!-- <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 這個(gè)屬性值
<?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>
<!-- 開(kāi)啟注解配置 即Autowried -->
<!-- <context:annotation-config/> -->
<!--其實(shí)component-scan 就有了annotation-config的功能即把需要的類(lèi)注冊(cè)到了spring容器中 -->
<context:component-scan base-package="com.website.service" />
<!-- 在使用mybatis時(shí) spring使用sqlsessionFactoryBean 來(lái)管理mybatis的sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件路徑 -->
<property name="configLocation" value="" />
<!-- 實(shí)體類(lèi)映射文件路徑,這里只有一個(gè)就寫(xiě)死了,多個(gè)可以使用mybatis/*.xml來(lái)替代 -->
<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>
<!--動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫(xiě)dao的實(shí)現(xiàn) -->
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 這里的basePackage 指定了dao層接口路勁,這里的dao接口不用自己實(shí)現(xiàn) -->
<property name="basePackage" value="com.website.dao" />
<!-- 如果只有一個(gè)數(shù)據(jù)源的話可以不用指定,但是如果有多個(gè)數(shù)據(jù)源的話必須要指定 -->
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
<!--直接制定了sqlsessionTemplate名稱(chēng),這個(gè)和上面的其實(shí)是一樣的 -->
<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 來(lái)指定具體的數(shù)據(jù)源,不知道在上面的配置中有沒(méi)有注意到,如果使用sqlSessionTemplateBeanName 的話要
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory" /> </constructor-arg> </bean>
來(lái)創(chuàng)建具體的實(shí)例并賦值給sqlSessionTemplateBeanName 這個(gè)屬性。
(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接口的完整路勁,就這個(gè)demo而言namespace 就是userDao.java的完整路勁 -->
<mapper namespace="com.website.dao.UserDao">
<!-- 這里的id就是接口中方法的名稱(chēng) -->
<insert id="saveUser" parameterType="java.util.Map">
insert into user(id,name) values(#{id},#{name})
</insert>
</mapper>
ok 到這里配置文件到搞定了下面來(lái)看看控制層,業(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 對(duì)象
@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接口實(shí)現(xiàn)類(lèi)實(shí)例
// @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 接口的完整路勁就是這個(gè)dao 接口對(duì)應(yīng)的那個(gè)映射文件的namespace 而方法名就是 id的值
ok到這里這種配置方式都完了,也有了一個(gè)完整的小demo,下面我們簡(jiǎn)單總結(jié)一下:
這種配置方式相比之前的配置方式(下面也會(huì)寫(xiě)出來(lái))特別之處就是他使用了dao層接口的動(dòng)態(tài)代理方式實(shí)現(xiàn)了,之前我們會(huì)在dao層自己手動(dòng)實(shí)現(xiàn)dao層然后自動(dòng)注入SqlSessionTemplate 實(shí)例來(lái)調(diào)用具體的方法 比如 insert("","") selectOne("","") 等方法 其中第一個(gè)參數(shù)就是映射文件的地址: namespace+id 而第二個(gè)參數(shù)就是傳遞的條件這樣mybatis 就會(huì)按照我們傳遞的這兩個(gè)參數(shù)找到具體的映射文件進(jìn)行解析查詢。而這里使用動(dòng)態(tài)代理就省去了我們實(shí)現(xiàn)dao接口的這一步驟,而是由spring提我們實(shí)現(xiàn)了,那有個(gè)問(wèn)題,查詢條件參數(shù)我們傳遞了,但映射文件的具體路徑即:namespce+id 沒(méi)有傳遞怎么辦,那就是你的映射文件的namespace 必須是接口的類(lèi)全名稱(chēng)而id 必須是接口中的方法名稱(chēng),這樣動(dòng)態(tài)代理就能找到路勁了也有了參數(shù)了。 這樣一來(lái)是不是覺(jué)得就一樣了啊哈哈哈!
二、手動(dòng)實(shí)現(xiàn)dao層接口
下面先來(lái)看看手動(dòng)實(shí)現(xiàn)dao層的配置以及代碼:
1、正題結(jié)構(gòu)圖

2、三個(gè)配置文件以及映射文件
(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> <!-- 字符編碼過(guò)濾器 --> <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> <!-- 這個(gè)配置文件在容器啟動(dòng)的時(shí)候 就加載 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 攔截請(qǐng)求 --> <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)、掃描控制層、自動(dòng)注入以及視圖解析器的配置 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ū)動(dòng) --> <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 自動(dòng)掃描注入、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>
<!-- 開(kāi)啟注解配置 即Autowried -->
<!--component-scan擁有 annotation-config的功能即注入需要的類(lèi)到spring容器中 -->
<!--<context:annotation-config/> -->
<!--使用自動(dòng)注入的時(shí)候要 添加他來(lái)掃描bean之后才能在使用的時(shí)候 -->
<context:component-scan base-package="com.website.service ,com.website.dao" />
<!-- 在使用mybatis時(shí) spring使用sqlsessionFactoryBean 來(lái)管理mybatis的sqlsessionFactory -->
<!-- 而像這種使用接口實(shí)現(xiàn)的方式 是使用sqlsessionTemplate來(lái)進(jìn)行操作的,他提供了一些方法 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件路徑 -->
<property name="configLocation" value="" />
<!-- 實(shí)體類(lèi)映射文件路徑,在開(kāi)發(fā)中映射文件肯定是多個(gè)所以使用mybatis/*.xml來(lái)替代 -->
<property name="mapperLocations" value="classpath:mybatis/UserMapping.xml" />
</bean>
<!--其實(shí)這里類(lèi)的實(shí)例就是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">
<!--這個(gè)namespace + 下面的id 就是一個(gè)完整的路徑,在dao層我們寫(xiě)了完整的路徑之后mybatis就是映射這個(gè)文件中的相關(guān)sql語(yǔ)句 -->
<mapper namespace="com.website.userMapper">
<!-- parameterType就是你接受的參數(shù)的類(lèi)型, -->
<!-- 添加用戶信息 -->
<insert id="insertUser" parameterType="java.util.Map">
insert into user(id,name,password) values(#{id},#{name},#{password})
</insert>
</mapper>
你可能看到了這里的映射文件的namespace +id 是自定義的而不是dao 層接口的全類(lèi)名+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 這個(gè)其實(shí)是mybatis中的SqlSession 對(duì)象,我們看到在ApplicationContext.xml 中配置了,所以在我們使用時(shí)spring會(huì)幫我們自動(dòng)注入,我們直接使用就可以了不用去自己創(chuàng)建,這也就是所謂的控制反轉(zhuǎn)。ok 到此兩種文件的配置方式就結(jié)束了。
總結(jié)
以上所述是小編給大家介紹的spring、mybatis 配置方式詳解(常用兩種方式),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能
這篇文章主要介紹了springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
mybatis-plus內(nèi)置雪花算法主鍵重復(fù)問(wèn)題解決
本文主要介紹了mybatis-plus內(nèi)置雪花算法主鍵重復(fù)問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
Java面試題-實(shí)現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享
這篇文章主要介紹了Java面試題-實(shí)現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享,小編覺(jué)得還是挺不錯(cuò)的,具有參考價(jià)值,需要的朋友可以了解下。2017-10-10
詳解Java如何實(shí)現(xiàn)基于Redis的分布式鎖
在不同進(jìn)程需要互斥地訪問(wèn)共享資源時(shí),分布式鎖是一種非常有用的技術(shù)手段。這篇文章運(yùn)用圖文和實(shí)例代碼介紹了Java如何實(shí)現(xiàn)基于Redis的分布式鎖,文章介紹的很詳細(xì),對(duì)Java和Redis剛興趣的朋友們可以參考借鑒,下面來(lái)一起看看。2016-08-08

