转载自http://blog.csdn.net/qh_java/article/details/51601139

在之前的文章中总结了三种方式,但是有两种是注解sql的,这种方式比较混乱所以大家不怎么使用,下面总结一下常用的两种总结方式:

一、 动态代理实现 不用写dao的实现类

这种方式比较简单,不用实现dao层,只需要定义接口就可以了,这里只是为了记录配置文件所以程序写的很简单:

1、整体结构图:

2、三个配置文件以及一个映射文件

(1)、程序入口以及前端控制器配置 web.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
  4. version="3.0">
  5. <display-name>website1</display-name>
  6. <!-- 设置监听,在web容器启动时自动装配ApplicationContext的配置信息-->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
  10. <!-- 设置Spring容器加载配置文件路径 -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>
  14. classpath:config/springmvc-servlet.xml,
  15. classpath:config/ApplicationContext.xml
  16. </param-value>
  17. </context-param>
  18. <!-- 字符编码过滤器 -->
  19. <filter>
  20. <filter-name>encodingFilter</filter-name>
  21. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  22. <init-param>
  23. <param-name>encoding</param-name>
  24. <param-value>utf-8</param-value>
  25. </init-param>
  26. <init-param>
  27. <param-name>forceEncoding</param-name>
  28. <param-value>true</param-value>
  29. </init-param>
  30. </filter>
  31. <filter-mapping>
  32. <filter-name>encodingFilter</filter-name>
  33. <url-pattern>*.do</url-pattern>
  34. </filter-mapping>
  35. <!-- 前端控制器 -->
  36. <servlet>
  37. <servlet-name>springmvc</servlet-name>
  38. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  39. <init-param>
  40. <param-name>contextConfigLocation</param-name>
  41. <param-value>classpath:config/springmvc-servlet.xml</param-value>
  42. </init-param>
  43. <!-- 这个配置文件在容器启动的时候 就加载 -->
  44. <load-on-startup>1</load-on-startup>
  45. </servlet>
  46. <servlet-mapping>
  47. <servlet-name>springmvc</servlet-name>
  48. <!-- 拦截请求 -->
  49. <url-pattern>*.do</url-pattern>
  50. </servlet-mapping>
  51. <welcome-file-list>
  52. <welcome-file>index.html</welcome-file>
  53. <welcome-file>index.htm</welcome-file>
  54. <welcome-file>index.jsp</welcome-file>
  55. <welcome-file>default.html</welcome-file>
  56. <welcome-file>default.htm</welcome-file>
  57. <welcome-file>default.jsp</welcome-file>
  58. </welcome-file-list>
  59. </web-app>

(2)、扫描控制层、自动注入以及视图解析器的配置 springmvc-servlet.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  6. xmlns:cache="http://www.springframework.org/schema/cache"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  11. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  12. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
  13. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  14. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
  15. http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
  16. <!-- 注解驱动 -->
  17. <mvc:annotation-driven />
  18. <!-- <context:annotation-config /> -->
  19. <!-- context:component-scan 具有annotation-config 的功能 -->
  20. <!-- 扫描 控制层 -->
  21. <context:component-scan base-package="com.website.controller"></context:component-scan>
  22. <!-- 视图解析器 -->
  23. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  24. <property name="prefix" value="/WEB-INF/view/">
  25. </property>
  26. <property name="suffix" value=".jsp"></property>
  27. </bean>
  28. </beans>

(3)、数据源、service 自动扫描注入、spring代管mybatissqlsessionFactory 、dao层接口动态代理以及事务的配置ApplicationContext.xml

这里会有多中配置文件

1)、单数据源,动态代理实现dao层接口时不设置sqlSessionFactoryBeanName、或sqlSessionTemplateBeanName 两个属性的值

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  5. http://www.springframework.org/schema/context
  6. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  9. <!-- 加载配置JDBC文件 -->
  10. <context:property-placeholder location="classpath:db.properties" />
  11. <!-- 数据源 -->
  12. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  13. <property name="driverClassName">
  14. <value>${jdbc.driverClassName}</value>
  15. </property>
  16. <property name="url">
  17. <value>${jdbc.url}</value>
  18. </property>
  19. <property name="username">
  20. <value>${jdbc.username}</value>
  21. </property>
  22. <property name="password">
  23. <value>${jdbc.password}</value>
  24. </property>
  25. </bean>
  26. <!-- 开启注解配置 即Autowried -->
  27. <!-- <context:annotation-config/> -->
  28. <!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 -->
  29. <context:component-scan base-package="com.website.service" />
  30. <!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory -->
  31. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource" />
  33. <!-- mybatis配置文件路径 -->
  34. <property name="configLocation" value="" />
  35. <!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis/*.xml来替代 -->
  36. <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" />
  37. </bean>
  38. <!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/>
  39. </constructor-arg> </bean> -->
  40. <!--动态代理实现 不用写dao的实现 -->
  41. <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  42. <!-- 这里的basePackage 指定了dao层接口路劲,这里的dao接口不用自己实现 -->
  43. <property name="basePackage" value="com.website.dao" />
  44. <!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 -->
  45. <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
  46. <!--直接指定了sqlsessionTemplate名称,这个和上面的其实是一样的 -->
  47. <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> -->
  48. </bean>
  49. <!--事务管理器 -->
  50. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  51. <property name="dataSource" ref="dataSource" />
  52. </bean>
  53. <!-- 使用全注释事务 -->
  54. <tx:annotation-driven transaction-manager="transactionManager" />
  55. </beans>

2)、单数据源配置 sqlSessionFactoryBeanName 这个属性值

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  5. http://www.springframework.org/schema/context
  6. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  9. <!-- 加载配置JDBC文件 -->
  10. <context:property-placeholder location="classpath:db.properties" />
  11. <!-- 数据源 -->
  12. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  13. <property name="driverClassName">
  14. <value>${jdbc.driverClassName}</value>
  15. </property>
  16. <property name="url">
  17. <value>${jdbc.url}</value>
  18. </property>
  19. <property name="username">
  20. <value>${jdbc.username}</value>
  21. </property>
  22. <property name="password">
  23. <value>${jdbc.password}</value>
  24. </property>
  25. </bean>
  26. <!-- 开启注解配置 即Autowried -->
  27. <!-- <context:annotation-config/> -->
  28. <!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 -->
  29. <context:component-scan base-package="com.website.service" />
  30. <!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory -->
  31. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource" />
  33. <!-- mybatis配置文件路径 -->
  34. <property name="configLocation" value="" />
  35. <!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis/*.xml来替代 -->
  36. <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" />
  37. </bean>
  38. <!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"> <ref bean="sqlSessionFactory"/>
  39. </constructor-arg> </bean> -->
  40. <!--动态代理实现 不用写dao的实现 -->
  41. <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  42. <!-- 这里的basePackage 指定了dao层接口路劲,这里的dao接口不用自己实现 -->
  43. <property name="basePackage" value="com.website.dao" />
  44. <!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 -->
  45. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  46. <!--直接制定了sqlsessionTemplate名称,这个和上面的其实是一样的 -->
  47. <!-- <property name="sqlSessionTemplateBeanName" value="sqlSession" /> -->
  48. </bean>
  49. <!--事务管理器 -->
  50. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  51. <property name="dataSource" ref="dataSource" />
  52. </bean>
  53. <!-- 使用全注释事务 -->
  54. <tx:annotation-driven transaction-manager="transactionManager" />
  55. </beans>

3)、单数据源配置sqlSessionTemplateBeanName 这个属性值

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  5. http://www.springframework.org/schema/context
  6. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  9. <!-- 加载配置JDBC文件 -->
  10. <context:property-placeholder location="classpath:db.properties" />
  11. <!-- 数据源 -->
  12. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  13. <property name="driverClassName">
  14. <value>${jdbc.driverClassName}</value>
  15. </property>
  16. <property name="url">
  17. <value>${jdbc.url}</value>
  18. </property>
  19. <property name="username">
  20. <value>${jdbc.username}</value>
  21. </property>
  22. <property name="password">
  23. <value>${jdbc.password}</value>
  24. </property>
  25. </bean>
  26. <!-- 开启注解配置 即Autowried -->
  27. <!-- <context:annotation-config/> -->
  28. <!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 -->
  29. <context:component-scan base-package="com.website.service" />
  30. <!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory -->
  31. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource" />
  33. <!-- mybatis配置文件路径 -->
  34. <property name="configLocation" value="" />
  35. <!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis/*.xml来替代 -->
  36. <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" />
  37. </bean>
  38. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  39. <constructor-arg index="0">
  40. <ref bean="sqlSessionFactory" />
  41. </constructor-arg>
  42. </bean>
  43. <!--动态代理实现 不用写dao的实现 -->
  44. <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  45. <!-- 这里的basePackage 指定了dao层接口路劲,这里的dao接口不用自己实现 -->
  46. <property name="basePackage" value="com.website.dao" />
  47. <!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 -->
  48. <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
  49. <!--直接制定了sqlsessionTemplate名称,这个和上面的其实是一样的 -->
  50. <property name="sqlSessionTemplateBeanName" value="sqlSession" />
  51. </bean>
  52. <!--事务管理器 -->
  53. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  54. <property name="dataSource" ref="dataSource" />
  55. </bean>
  56. <!-- 使用全注释事务 -->
  57. <tx:annotation-driven transaction-manager="transactionManager" />
  58. </beans>

4)、多数据源

注意如果是多数据源则一定要使用sqlSessionFactoryBeanName 或sqlSessionTemplateBeanName 来指定具体的数据源,不知道在上面的配置中有没有注意到,如果使用sqlSessionTemplateBeanName 的话要

[html] view plain copy
  1. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  2. <constructor-arg index="0">
  3. <ref bean="sqlSessionFactory" />
  4. </constructor-arg>
  5. </bean>

来创建具体的实例并赋值给sqlSessionTemplateBeanName 这个属性。

(4)、mybatis SQL映射文件 userMapper.xml:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <!-- namespace的值就是dao接口的完整路劲,就这个demo而言namespace 就是userDao.java的完整路劲 -->
  5. <mapper namespace="com.website.dao.UserDao">
  6. <!-- 这里的id就是接口中方法的名称 -->
  7. <insert id="saveUser" parameterType="java.util.Map">
  8. insert into user(id,name) values(#{id},#{name})
  9. </insert>
  10. </mapper>

ok  到这里配置文件到搞定了下面来看看控制层,业务逻辑层以及dao层的代码。

3、controller层

[java] view plain copy
  1. package com.website.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import com.website.service.UserService;
  11. @Controller
  12. @RequestMapping(value = "/user")
  13. public class UserController {
  14. // 注入userService 对象
  15. @Autowired
  16. private UserService userService;
  17. @RequestMapping(value = "/save.do", method = RequestMethod.GET)
  18. public String saveUser(HttpServletRequest request,
  19. HttpServletResponse response) {
  20. String id = request.getParameter("id");
  21. String name = request.getParameter("name");
  22. Map<String, String> map = new HashMap<String, String>();
  23. map.put("id", id);
  24. map.put("name", name);
  25. userService.saveUser(map);
  26. return "index";
  27. }
  28. }

4、service层

[java] view plain copy
  1. package com.website.service;
  2. import java.util.Map;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import com.website.dao.UserDao;
  7. @Service("userService")
  8. @Transactional
  9. public class UserService {
  10. // 注入dao接口实现类实例
  11. // @Resource、@Autowired两种注入方式都可以
  12. @Autowired
  13. private UserDao userDao;
  14. public void saveUser(Map<String, String> map) {
  15. int end = userDao.saveUser(map);
  16. System.out.println("end:" + end);
  17. }
  18. }

5、dao 层 接口

[java] view plain copy
  1. package com.website.dao;
  2. import java.util.Map;
  3. //com.website.dao.UserDao
  4. public interface UserDao {
  5. int saveUser(Map<String, String> map);
  6. }

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

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="WebApp_ID" version="3.0">
  6. <display-name>website2</display-name>
  7. <!-- 加载spring容器配置 -->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. <!-- 设置Spring容器加载配置文件路径 -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>
  15. classpath:config/springmvc-servlet.xml,
  16. classpath:config/ApplicationContext.xml
  17. </param-value>
  18. </context-param>
  19. <!-- 字符编码过滤器 -->
  20. <filter>
  21. <filter-name>encodingFilter</filter-name>
  22. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  23. <init-param>
  24. <param-name>encoding</param-name>
  25. <param-value>utf-8</param-value>
  26. </init-param>
  27. <init-param>
  28. <param-name>forceEncoding</param-name>
  29. <param-value>true</param-value>
  30. </init-param>
  31. </filter>
  32. <filter-mapping>
  33. <filter-name>encodingFilter</filter-name>
  34. <url-pattern>*.do</url-pattern>
  35. </filter-mapping>
  36. <!-- 前端控制器 -->
  37. <servlet>
  38. <servlet-name>springmvc</servlet-name>
  39. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  40. <init-param>
  41. <param-name>contextConfigLocation</param-name>
  42. <param-value>classpath:config/springmvc-servlet.xml</param-value>
  43. </init-param>
  44. <!-- 这个配置文件在容器启动的时候 就加载 -->
  45. <load-on-startup>1</load-on-startup>
  46. </servlet>
  47. <servlet-mapping>
  48. <servlet-name>springmvc</servlet-name>
  49. <!-- 拦截请求 -->
  50. <url-pattern>*.do</url-pattern>
  51. </servlet-mapping>
  52. <welcome-file-list>
  53. <welcome-file>index.html</welcome-file>
  54. <welcome-file>index.htm</welcome-file>
  55. <welcome-file>index.jsp</welcome-file>
  56. <welcome-file>default.html</welcome-file>
  57. <welcome-file>default.htm</welcome-file>
  58. <welcome-file>default.jsp</welcome-file>
  59. </welcome-file-list>
  60. </web-app>

(2)、扫描控制层、自动注入以及视图解析器的配置 springmvc-servlet.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  6. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  7. xmlns:cache="http://www.springframework.org/schema/cache"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  12. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  13. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
  14. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  15. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
  16. http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
  17. <!-- 注解驱动 -->
  18. <mvc:annotation-driven />
  19. <!-- <context:annotation-config /> -->
  20. <!-- 扫描 -->
  21. <context:component-scan base-package="com.website.controller"></context:component-scan>
  22. <!-- 视图解析器 -->
  23. <bean id="viewResolver"
  24. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  25. <property name="prefix" value="/WEB-INF/view/">
  26. </property>
  27. <property name="suffix" value=".jsp"></property>
  28. </bean>
  29. </beans>

(3)、数据源、service 自动扫描注入、spring代管mybatissqlsessionFactory 以及事务的配置ApplicationContext.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  10. <!-- 加载配置JDBC文件 -->
  11. <context:property-placeholder location="classpath:db.properties" />
  12. <!-- 数据源 -->
  13. <bean id="dataSource"
  14. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  15. <property name="driverClassName">
  16. <value>${jdbc.driverClassName}</value>
  17. </property>
  18. <property name="url">
  19. <value>${jdbc.url}</value>
  20. </property>
  21. <property name="username">
  22. <value>${jdbc.username}</value>
  23. </property>
  24. <property name="password">
  25. <value>${jdbc.password}</value>
  26. </property>
  27. </bean>
  28. <!-- 开启注解配置 即Autowried -->
  29. <!--component-scan拥有 annotation-config的功能即注入需要的类到spring容器中 -->
  30. <!--<context:annotation-config/> -->
  31. <!--使用自动注入的时候要 添加他来扫描bean之后才能在使用的时候 -->
  32. <context:component-scan base-package="com.website.service ,com.website.dao" />
  33. <!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory -->
  34. <!-- 而像这种使用接口实现的方式 是使用sqlsessionTemplate来进行操作的,他提供了一些方法 -->
  35. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  36. <property name="dataSource" ref="dataSource" />
  37. <!-- mybatis配置文件路径 -->
  38. <property name="configLocation" value="" />
  39. <!-- 实体类映射文件路径,在开发中映射文件肯定是多个所以使用mybatis/*.xml来替代 -->
  40. <property name="mapperLocations" value="classpath:mybatis/UserMapping.xml" />
  41. </bean>
  42. <!--其实这里类的实例就是mybatis中SQLSession -->
  43. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  44. <constructor-arg index="0">
  45. <ref bean="sqlSessionFactory" />
  46. </constructor-arg>
  47. </bean>
  48. <bean id="transactionManager"
  49. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  50. <property name="dataSource" ref="dataSource" />
  51. </bean>
  52. <!--使用全注释事务 -->
  53. <tx:annotation-driven transaction-manager="transactionManager" />
  54. </beans>

(4)、mybatis 映射文件

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <!--这个namespace + 下面的id 就是一个完整的路径,在dao层我们写了完整的路径之后mybatis就是映射这个文件中的相关sql语句 -->
  5. <mapper namespace="com.website.userMapper">
  6. <!-- parameterType就是你接受的参数的类型,  -->
  7. <!-- 添加用户信息 -->
  8. <insert id="insertUser"  parameterType="java.util.Map">
  9. insert  into  user(id,name,password)  values(#{id},#{name},#{password})
  10. </insert>
  11. </mapper>

你可能看到了这里的映射文件的namespace +id  是自定义的而不是dao 层接口的全类名+id

3、控制层Controller

[java] view plain copy
  1. package com.website.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import com.website.service.UserService;
  10. /**
  11. * @author WHD data 2016年6月5日
  12. */
  13. @Controller
  14. @RequestMapping(value = "/user")
  15. public class UserController {
  16. @Autowired
  17. private UserService userService;
  18. @RequestMapping(value = "/save.do")
  19. public String saveUser(HttpServletRequest request,
  20. HttpServletResponse response) {
  21. String id = request.getParameter("id");
  22. String name = request.getParameter("name");
  23. String password = request.getParameter("password");
  24. Map<String, String> map = new HashMap<String, String>();
  25. map.put("id", id);
  26. map.put("name", name);
  27. map.put("password", password);
  28. userService.saveUser(map);
  29. return "index";
  30. }
  31. }

4、业务逻辑层 service

[java] view plain copy
  1. package com.website.service;
  2. import java.util.Map;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import com.website.dao.UserDao;
  7. /**
  8. * @author WHD data 2016年6月5日
  9. */
  10. @Service("userService")
  11. @Transactional
  12. public class UserService {
  13. @Autowired
  14. private UserDao userDao;
  15. public void saveUser(Map<String, String> map) {
  16. userDao.saveUser(map);
  17. }
  18. }

5、dao层

[java] view plain copy
  1. package com.website.dao;
  2. import java.util.Map;
  3. import org.mybatis.spring.SqlSessionTemplate;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Repository;
  6. /**
  7. * @author WHD data 2016年6月5日
  8. */
  9. @Repository("userDao")
  10. public class UserDao {
  11. @Autowired
  12. private SqlSessionTemplate sqlSession;
  13. public void saveUser(Map<String, String> map) {
  14. int end = sqlSession.insert("com.website.userMapper.insertUser", map);
  15. System.out.println("end" + end);
  16. }
  17. }

我们看倒dao层的  SqlSessionTemplate  这个其实是mybatis中的SqlSession 对象,我们看到在ApplicationContext.xml 中配置了,所以在我们使用时spring会帮我们自动注入,我们直接使用就可以了不用去自己创建,这也就是所谓的控制反转。ok 到此两种文件的配置方式就结束了。

这里我们没有看到有讲解属性,只是简单了一个配置小demo 在写这篇文章的时候看到了一片文章对这两种配置有详细的讲解所以下一片我会转载这篇文章,对这两种配置有不明白的可以看下面一片文章!

常用的两种spring、mybatis 配置方式相关推荐

  1. TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable

    TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable 目录 输出结果 代码设计 输出结果 代码设计 # tensorflow中的两种定义scope(命 ...

  2. 动态网页常用的两种数据加载方式ajax和js动态请求

    欢迎关注"生信修炼手册"! 对于静态网页,我们只需要访问对应的URL就可以获得全部的数据了,动态网页则没有这么简单.比如以下网站 http://q.10jqka.com.cn/zj ...

  3. java操作excel常用的两种方式

    转载自:https://www.cnblogs.com/wangyang108/p/6030420.html Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进 ...

  4. 【REACT NATIVE 系列教程之十三】利用LISTVIEW与TEXTINPUT制作聊天/对话框获取组件实例常用的两种方式...

    本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/react-native/2346.html ...

  5. Xml解析常用的两种方式

    Xml解析常用的两种方式 Xml解析常用的两种方式dom. DOM4J 下面我们先看一下这两种方式的优缺点: 1.DOM解析的特点是将整个xml文档以树形结构放入到内存中,是官方推荐标准,优点是在内存 ...

  6. 两种获取connectionString的方式

    两种获取connectionString的方式 1. public static string connectionString = ConfigurationManager.ConnectionSt ...

  7. Hibernate中两种获取Session的方式

    转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...

  8. 在计算机系统中有两种不同的图像编码方式,第二章计算机系统与计算原理.ppt...

    第二章计算机系统与计算原理 大学计算机基础 * 信息表示与处理 ----西文字符 ASCII 码是美国信息交换标准代码(American Standard Code for Information I ...

  9. 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)

    From: http://www.cnblogs.com/lanxuezaipiao/p/3703988.html 导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列 ...

最新文章

  1. 新方案-eclipse配置tomcat中文乱码另一种解决方案
  2. 深入探索C++对象模型学习笔记
  3. 安妮宝贝的50句经典语句
  4. PyQt5基础——3
  5. [转]IIS 允许/禁止 目录浏览
  6. sublime text3:提示 There are no packages available installation 解决方案
  7. [html] const nums1 = [1, 2, 2, 1], nums2 = [2] 交集是什么?
  8. 互斥锁、死锁、递归锁、信号量、Event
  9. BlogEngine.Net架构与源代码分析系列part12:页面共同的基类——BlogBasePage
  10. 18. Make interfaces esay to use correctly and hard to use incorrectly
  11. Windows API一日一练(17-18)DialogBox DialogBoxParam EndDialog函数
  12. 我的创作纪念日 | 软件测试成长之路
  13. 嵌入式入门必去的网站 —— 介绍的非常详细
  14. Pycharm+Django之Django学习(1)(初学者)
  15. 必修的十堂电影课(男人篇)
  16. Oracle VirtualBox 6.1.18 安装扩展包
  17. python怎么换行输入print_python中print换行的方法
  18. SRC漏洞提交平台和应急响应中心
  19. 从I到R:人工智能语言简史
  20. hiredis的代码示例

热门文章

  1. C++虚继承(十) --- 谈谈陈皓遗留的问题
  2. C++中基于Crt的内存泄漏检测
  3. OpenCV 中的绘制功能
  4. WebRTC 的版本号与代码分支
  5. python新式类和经典类的区别?
  6. 手写带注册中心的rpc框架(Netty版和Socket版)
  7. 好文推荐 | 缓存与数据库一致性问题深度剖析 (修订)
  8. 你知道 Spring Batch 吗?
  9. 用 Go 解析复杂 JSON 的思路
  10. SRS的2021,盐碱地里种西瓜的王婆