前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >常用的两种spring、mybatis 配置方式

常用的两种spring、mybatis 配置方式

作者头像
allsmallpig
发布2021-02-25 10:40:41
2190
发布2021-02-25 10:40:41
举报
文章被收录于专栏:allsmallpi博客

转载自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>website1display-name>
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  8. listener>
  9. <context-param>
  10. <param-name>contextConfigLocationparam-name>
  11. <param-value>
  12.          classpath:config/springmvc-servlet.xml,      
  13.          classpath:config/ApplicationContext.xml      
  14. param-value>
  15. context-param>
  16. <filter>
  17. <filter-name>encodingFilterfilter-name>
  18. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
  19. <init-param>
  20. <param-name>encodingparam-name>
  21. <param-value>utf-8param-value>
  22. init-param>
  23. <init-param>
  24. <param-name>forceEncodingparam-name>
  25. <param-value>trueparam-value>
  26. init-param>
  27. filter>
  28. <filter-mapping>
  29. <filter-name>encodingFilterfilter-name>
  30. <url-pattern>*.dourl-pattern>
  31. filter-mapping>
  32. <servlet>
  33. <servlet-name>springmvcservlet-name>
  34. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  35. <init-param>
  36. <param-name>contextConfigLocationparam-name>
  37. <param-value>classpath:config/springmvc-servlet.xmlparam-value>
  38. init-param>
  39. <load-on-startup>1load-on-startup>
  40. servlet>
  41. <servlet-mapping>
  42. <servlet-name>springmvcservlet-name>
  43. <url-pattern>*.dourl-pattern>
  44. servlet-mapping>
  45. <welcome-file-list>
  46. <welcome-file>index.htmlwelcome-file>
  47. <welcome-file>index.htmwelcome-file>
  48. <welcome-file>index.jspwelcome-file>
  49. <welcome-file>default.htmlwelcome-file>
  50. <welcome-file>default.htmwelcome-file>
  51. <welcome-file>default.jspwelcome-file>
  52. welcome-file-list>
  53. 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. <mvc:annotation-driven />
  17. <context:component-scan base-package="com.website.controller">context:component-scan>
  18. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  19. <property name="prefix" value="/WEB-INF/view/">
  20. property>
  21. <property name="suffix" value=".jsp">property>
  22. bean>
  23. 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. <context:property-placeholder location="classpath:db.properties" />
  10. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  11. <property name="driverClassName">
  12. <value>${jdbc.driverClassName}value>
  13. property>
  14. <property name="url">
  15. <value>${jdbc.url}value>
  16. property>
  17. <property name="username">
  18. <value>${jdbc.username}value>
  19. property>
  20. <property name="password">
  21. <value>${jdbc.password}value>
  22. property>
  23. bean>
  24. <context:component-scan base-package="com.website.service" />
  25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <property name="dataSource" ref="dataSource" />
  27. <property name="configLocation" value="" />
  28. <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" />
  29. bean>
  30. <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  31. <property name="basePackage" value="com.website.dao" />
  32. bean>
  33. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  34. <property name="dataSource" ref="dataSource" />
  35. bean>
  36. <tx:annotation-driven transaction-manager="transactionManager" />
  37. 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. <context:property-placeholder location="classpath:db.properties" />
  10. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  11. <property name="driverClassName">
  12. <value>${jdbc.driverClassName}value>
  13. property>
  14. <property name="url">
  15. <value>${jdbc.url}value>
  16. property>
  17. <property name="username">
  18. <value>${jdbc.username}value>
  19. property>
  20. <property name="password">
  21. <value>${jdbc.password}value>
  22. property>
  23. bean>
  24. <context:component-scan base-package="com.website.service" />
  25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <property name="dataSource" ref="dataSource" />
  27. <property name="configLocation" value="" />
  28. <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" />
  29. bean>
  30. <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  31. <property name="basePackage" value="com.website.dao" />
  32. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  33. bean>
  34. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  35. <property name="dataSource" ref="dataSource" />
  36. bean>
  37. <tx:annotation-driven transaction-manager="transactionManager" />
  38. 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. <context:property-placeholder location="classpath:db.properties" />
  10. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  11. <property name="driverClassName">
  12. <value>${jdbc.driverClassName}value>
  13. property>
  14. <property name="url">
  15. <value>${jdbc.url}value>
  16. property>
  17. <property name="username">
  18. <value>${jdbc.username}value>
  19. property>
  20. <property name="password">
  21. <value>${jdbc.password}value>
  22. property>
  23. bean>
  24. <context:component-scan base-package="com.website.service" />
  25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <property name="dataSource" ref="dataSource" />
  27. <property name="configLocation" value="" />
  28. <property name="mapperLocations" value="classpath:mybatis/userMapper.xml" />
  29. bean>
  30. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  31. <constructor-arg index="0">
  32. <ref bean="sqlSessionFactory" />
  33. constructor-arg>
  34. bean>
  35. <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  36. <property name="basePackage" value="com.website.dao" />
  37. <property name="sqlSessionTemplateBeanName" value="sqlSession" />
  38. bean>
  39. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  40. <property name="dataSource" ref="dataSource" />
  41. bean>
  42. <tx:annotation-driven transaction-manager="transactionManager" />
  43. 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.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.website.dao.UserDao">
  4. <insert id="saveUser" parameterType="java.util.Map">
  5.                 insert into user(id,name) values(#{id},#{name})  
  6. insert>
  7. 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 map = new HashMap();  
  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 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 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>website2display-name>
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  9. listener>
  10. <context-param>
  11. <param-name>contextConfigLocationparam-name>
  12. <param-value>
  13.          classpath:config/springmvc-servlet.xml,    
  14.          classpath:config/ApplicationContext.xml    
  15. param-value>
  16. context-param>
  17. <filter>
  18. <filter-name>encodingFilterfilter-name>
  19. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
  20. <init-param>
  21. <param-name>encodingparam-name>
  22. <param-value>utf-8param-value>
  23. init-param>
  24. <init-param>
  25. <param-name>forceEncodingparam-name>
  26. <param-value>trueparam-value>
  27. init-param>
  28. filter>
  29. <filter-mapping>
  30. <filter-name>encodingFilterfilter-name>
  31. <url-pattern>*.dourl-pattern>
  32. filter-mapping>
  33. <servlet>
  34. <servlet-name>springmvcservlet-name>
  35. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  36. <init-param>
  37. <param-name>contextConfigLocationparam-name>
  38. <param-value>classpath:config/springmvc-servlet.xmlparam-value>
  39. init-param>
  40. <load-on-startup>1load-on-startup>
  41. servlet>
  42. <servlet-mapping>
  43. <servlet-name>springmvcservlet-name>
  44. <url-pattern>*.dourl-pattern>
  45. servlet-mapping>
  46. <welcome-file-list>
  47. <welcome-file>index.htmlwelcome-file>
  48. <welcome-file>index.htmwelcome-file>
  49. <welcome-file>index.jspwelcome-file>
  50. <welcome-file>default.htmlwelcome-file>
  51. <welcome-file>default.htmwelcome-file>
  52. <welcome-file>default.jspwelcome-file>
  53. welcome-file-list>
  54. 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. <mvc:annotation-driven />
  18. <context:component-scan base-package="com.website.controller">context:component-scan>
  19. <bean id="viewResolver"
  20. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  21. <property name="prefix" value="/WEB-INF/view/">
  22. property>
  23. <property name="suffix" value=".jsp">property>
  24. bean>
  25. 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. <context:property-placeholder location="classpath:db.properties" />
  11. <bean id="dataSource"
  12. 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. <context:component-scan base-package="com.website.service ,com.website.dao" />
  27. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  28. <property name="dataSource" ref="dataSource" />
  29. <property name="configLocation" value="" />
  30. <property name="mapperLocations" value="classpath:mybatis/UserMapping.xml" />
  31. bean>
  32. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  33. <constructor-arg index="0">
  34. <ref bean="sqlSessionFactory" />
  35. constructor-arg>
  36. bean>
  37. <bean id="transactionManager"
  38. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  39. <property name="dataSource" ref="dataSource" />
  40. bean>
  41. <tx:annotation-driven transaction-manager="transactionManager" />
  42. beans>

(4)、mybatis 映射文件

[html] view plain copy

  1. xml version="1.0" encoding="UTF-8" ?>
  2.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.website.userMapper">
  4. <insert id="insertUser" parameterType="java.util.Map">
  5.  insert  into  user(id,name,password)  values(#{id},#{name},#{password})  
  6. insert>
  7. 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 map = new HashMap();  
  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 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 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 在写这篇文章的时候看到了一片文章对这两种配置有详细的讲解所以下一片我会转载这篇文章,对这两种配置有不明白的可以看下面一片文章!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/02/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档