星空网 > 软件开发 > ASP.net

SpringMVC + MyBatis整合

环境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3

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

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

0. project 目录

SpringMVC + MyBatis整合

1. spring-resources.

这个文件是用来完成spring和mybatis的整合的?

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



2. mapper.

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<? 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>



springmvc + mybatis整合详细,及遇到的问题请参看以下资料:

项目源码下载




原标题:SpringMVC + MyBatis整合

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊前台规则又变化!卖家如何找到出路?:https://www.kjdsnews.com/a/1511238.html
虎牙斗鱼游戏直播求生:https://www.kjdsnews.com/a/1511239.html
2023年元宇宙趋势:https://www.kjdsnews.com/a/1511240.html
货之家:境外没营业执照能做进口跨境电商?进口货物一定要进保税仓吗:https://www.kjdsnews.com/a/1511241.html
赛狐ERP | 亚马逊自动广告高曝光,低点击,低转化,如何优化?:https://www.kjdsnews.com/a/1511242.html
让价值延续!赛狐ERP2023亚马逊华东千人峰会!:https://www.kjdsnews.com/a/1511243.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
三亚有哪些酒店值得入住?:https://www.vstour.cn/a/366173.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流