three method of config mybatis

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

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

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

1、整体结构图:


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

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

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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
 print ? 在CODE上查看代码片 派生到我的代码片
  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.         <!-- 注解驱动 -->  
  18.         <mvc:annotation-driven />  
  19.         <!-- <context:annotation-config /> -->  
  20.         <!-- context:component-scan 具有annotation-config 的功能 -->  
  21.         <!-- 扫描 控制层 -->  
  22.         <context:component-scan base-package="com.website.controller"></context:component-scan>  
  23.         <!-- 视图解析器 -->  
  24.         <bean id="viewResolver" 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 、dao层接口动态代理以及事务的配置ApplicationContext.xml

这里会有多中配置文件

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

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

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

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

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

4)、多数据源

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

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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
 print ? 在CODE上查看代码片 派生到我的代码片
  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.   
  5. <!-- namespace的值就是dao接口的完整路劲,就这个demo而言namespace 就是userDao.java的完整路劲 -->  
  6. <mapper namespace="com.website.dao.UserDao">  
  7.         <!-- 这里的id就是接口中方法的名称 -->  
  8.         <insert id="saveUser" parameterType="java.util.Map">  
  9.                 insert into user(id,name) values(#{id},#{name})  
  10.         </insert>  
  11. </mapper>    

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

3、controller层 

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

4、service层

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

5、dao 层 接口

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.website.dao;  
  2.   
  3. import java.util.Map;  
  4.   
  5. //com.website.dao.UserDao  
  6. public interface UserDao {  
  7.     int saveUser(Map<String, String> map);  
  8. }  
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
 print ? 在CODE上查看代码片 派生到我的代码片
  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
 print ? 在CODE上查看代码片 派生到我的代码片
  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.   
  19.     <!-- 注解驱动 -->  
  20.     <mvc:annotation-driven />  
  21.     <!-- <context:annotation-config /> -->  
  22.     <!-- 扫描 -->  
  23.     <context:component-scan base-package="com.website.controller"></context:component-scan>  
  24.     <!-- 视图解析器 -->  
  25.     <bean id="viewResolver"  
  26.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  27.         <property name="prefix" value="/WEB-INF/view/">  
  28.         </property>  
  29.         <property name="suffix" value=".jsp"></property>  
  30.     </bean>  
  31. </beans>   


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

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

(4)、mybatis 映射文件

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.website.controller;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.springframework.beans.factory.annotation.Autowired;  
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12.   
  13. import com.website.service.UserService;  
  14.   
  15. /** 
  16.  * @author WHD data 2016年6月5日 
  17.  */  
  18. @Controller  
  19. @RequestMapping(value = "/user")  
  20. public class UserController {  
  21.     @Autowired  
  22.     private UserService userService;  
  23.   
  24.     @RequestMapping(value = "/save.do")  
  25.     public String saveUser(HttpServletRequest request,  
  26.             HttpServletResponse response) {  
  27.         String id = request.getParameter("id");  
  28.         String name = request.getParameter("name");  
  29.         String password = request.getParameter("password");  
  30.         Map<String, String> map = new HashMap<String, String>();  
  31.         map.put("id", id);  
  32.         map.put("name", name);  
  33.         map.put("password", password);  
  34.         userService.saveUser(map);  
  35.         return "index";  
  36.     }  
  37.   
  38. }  

4、业务逻辑层 service

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.website.service;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Service;  
  7. import org.springframework.transaction.annotation.Transactional;  
  8.   
  9. import com.website.dao.UserDao;  
  10.   
  11. /** 
  12.  * @author WHD data 2016年6月5日 
  13.  */  
  14. @Service("userService")  
  15. @Transactional  
  16. public class UserService {  
  17.     @Autowired  
  18.     private UserDao userDao;  
  19.   
  20.     public void saveUser(Map<String, String> map) {  
  21.         userDao.saveUser(map);  
  22.     }  
  23. }  

5、dao层

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值