zoukankan      html  css  js  c++  java
  • SpringMVC + MyBatis整合

    网络上关于这个架构的搭建文章,实在是太多了,本文是对于本人初次搭建时的一些注意点的整理。

    主要是一些配置文件的内容和架构的目录。

    0. project 目录

    1. spring-resources.xml

    这个文件是用来完成spring和mybatis的整合的xml。注意properties文件的读入方式。

     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"
     4     xmlns:p="http://www.springframework.org/schema/p" 
     5     xmlns:context="http://www.springframework.org/schema/context" 
     6     xmlns:tx="http://www.springframework.org/schema/tx" 
     7     xmlns:aop="http://www.springframework.org/schema/aop"
     8     xsi:schemaLocation="  
     9     http://www.springframework.org/schema/beans   
    10     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    11     http://www.springframework.org/schema/tx   
    12     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
    13     http://www.springframework.org/schema/context   
    14     http://www.springframework.org/schema/context/spring-context-3.0.xsd
    15     http://www.springframework.org/schema/aop        
    16     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" 
    17     default-autowire="byName" default-lazy-init="false">
    18      
    19     <!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 --> 
    20     <context:component-scan base-package="org.com.mars">  
    21         <context:exclude-filter type="regex" 
    22             expression="org.com.mars.controller.*" />  
    23     </context:component-scan>
    24  
    25     <!-- 引入jdbc配置文件 --> 
    26     <context:property-placeholder location="classpath:prop/jdbc.properties" /> 
    27  
    28     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 
    29     <bean id="transactionManager" 
    30         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    31         <property name="dataSource" ref="dataSource" />  
    32     </bean> 
    33      
    34     <!-- 声明式事务管理 -->
    35     <aop:config>  
    36         <aop:advisor pointcut="execution(* org.com.mars.service..*.*(..))" 
    37             advice-ref="myAdvice" />  
    38     </aop:config>  
    39     <tx:advice id="myAdvice" transaction-manager="transactionManager">  
    40         <tx:attributes>  
    41             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />  
    42             <tx:method name="query*" propagation="SUPPORTS" read-only="true" />  
    43             <tx:method name="list*" propagation="SUPPORTS" read-only="true" />  
    44    
    45             <tx:method name="create*" propagation="REQUIRED" />  
    46             <tx:method name="save*" propagation="REQUIRED" />  
    47             <tx:method name="modify*" propagation="REQUIRED" />  
    48             <tx:method name="update*" propagation="REQUIRED" />  
    49             <tx:method name="delete*" propagation="REQUIRED" />  
    50    
    51             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
    52         </tx:attributes>  
    53     </tx:advice>  
    54      
    55     <!-- 创建SqlSessionFactory,同时指定数据源 --> 
    56     <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    57         <property name="dataSource" ref="dataSource" />  
    58         <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    59         <property name="mapperLocations" value="classpath*:maper/*.map.xml" />
    60     </bean> 
    61      
    62     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    63     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    64         <property name="basePackage" value="org.com.mars.dao" />
    65         <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" />
    66     </bean>
    67      
    68     <!-- 可通过注解控制事务 --> 
    69     <tx:annotation-driven />
    70      
    71     <!-- 数据库连接池 -->
    72     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    73         destroy-method="close">
    74         <!-- Connection Info -->
    75         <property name="driverClassName" value="${jdbc.driver}" />
    76         <property name="url" value="${jdbc.url}" />
    77         <property name="username" value="${jdbc.username}" />
    78         <property name="password" value="${jdbc.password}" />
    79  
    80         <!-- Connection Pooling Info -->
    81         <property name="maxActive" value="${dbcp.maxActive}" />
    82         <property name="maxIdle" value="${dbcp.maxIdle}" />
    83         <property name="defaultAutoCommit" value="false" />
    84         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    85         <property name="timeBetweenEvictionRunsMillis" value="3600000" />
    86         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    87         <property name="minEvictableIdleTimeMillis" value="3600000" />
    88     </bean>
    89 </beans>

    2. mapper.xml

    该xml 如何使用配置,请参看spring-resources.xml,注意mapper.xml中 namespace的配置。

    <?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">
    <mapper namespace="org.com.mars.dao.test.UserDao">
     
     <!-- mapping -->
     <resultMap id="User_t" type="org.com.mars.pojo.test.User">
     <result property="userId" column="user_id" jdbcType="VARCHAR" />
     <result property="userName" column="user_name" jdbcType="VARCHAR" />
     </resultMap>
     
     <sql id="columns">
     user_id,
     user_name
     </sql>
     
     
     <!-- 根据ID查询用户信息 -->
     <select id="queryUserById" resultMap="User_t">
     SELECT
     <include refid="columns" />
     FROM
     user_t
     WHERE
     user_id = #{userId}
     </select>
     
    </mapper>
  • 相关阅读:
    【OpenCv/EmguCv】指针式仪表读数(二)
    合天网安实验室CTF练习赛之RE300
    Codeforces Round #527 -A. Uniform String(思维)
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/wqsbk/p/5275330.html
Copyright © 2011-2022 走看看