SpringMVC + MyBatis整合

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

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

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

0. project 目录

1. spring-resources.xml

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="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.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath*:maper/*.map.xml" />
    </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.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>

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

参考资料:

http://www.itnose.net/detail/6074493.html

http://my.oschina.net/wangbiglei/blog/489583

感谢热心的前辈!!本文如有不正,请指出。

以上。

时间: 2024-08-08 09:39:23

SpringMVC + MyBatis整合的相关文章

之前SpringMVC+Mybatis整合,现在加上AOP

之前SpringMvc和mybatis整合的例子:http://www.cnblogs.com/acehalo/p/3901809.html Controller类增加方法,以便测试: @RequestMapping(value = "/aopTest") @ResponseBody public String aopTest(HttpServletRequest request) { for (int i = 0; i < 3; i++) { aopServiceTest.do

SpringMVC + MyBatis整合 【转】

环境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3 网络上关于这个架构的搭建文章,实在是太多了,本文是对于本人初次搭建时的一些注意点的整理. 主要是一些配置文件的内容和架构的目录. 0. project 目录 1. spring-resources.xml 这个文件是用来完成spring和mybatis的整合的xml.注意properties文件的读入方式. <?xml version="1.0" encoding="UTF-

[手把手教程][JavaWeb]优雅的SpringMvc+Mybatis整合之路

来源于:http://www.jianshu.com/p/5124eef40bf0 [手把手教程][JavaWeb]优雅的SpringMvc+Mybatis整合之路 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis 前面网友说我为啥很久不更新博客了,我告诉他们我准备潜修.其实是我的博客被人批评是在记流水账(~一脸尴尬~). 再次安利一波,博客地址:acheng1314.cn 本文中的图片用了个人服务器存储,网速较慢,各位老司机耐心等待. 工具 IDE为id

Spring+SpringMVC +MyBatis整合配置文件案例66666

Spring+SpringMVC +MyBatis整合配置文件案例 标签: springspringmvcmybatismvcjava 2017-04-13 19:12 228人阅读 评论(1) 收藏 举报 分类: java_javaSE(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Spring+SpringMVC +MyBatis整合配置文件案例 针对spring/SpringMVC/MyBatis三个框架的整合有很多的方式,经过最近的学习我来总结一下其配置文

IDEA下使用maven构建web项目(SpringMVC+Mybatis整合)

需求背景:由于最近总是接到一些需求,需要配合前端团队快速建设移动端UI应用或web应用及后台业务逻辑支撑的需求,若每次都复用之前复杂业务应用的项目代码,总会携带很多暂时不会用到的功能或组件,这样的初始工程就存在冗余代码. 在本文中,我们将使用Java语言开发集成环境IntelliJ IDEA(其倡言是智能编码?),应用maven构建SpringMVC整合Mybatis+MySQL5.7(流行框架)的web项目:目的在于快速构建一个简洁纯净版的web应用工程,将其作为一个基础web-demo,以便

ssm之spring+springmvc+mybatis整合初探

1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/

Spring4+SpringMVC+MyBatis整合思路

本文主要简单讲解框架整合的思路. 1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件,那么spring容器搭建完成.(当然org.springframework的核心jar包需要引入) 当然为了更加易用支持J2EE应用,一般我们还会加上如下: Spring监听HTTP请求事件:org.springframework.web.context.requ

springmvc mybatis 整合 框架源码 bootstrap html5 mysql oracle spring

获取[下载地址]   QQ: 313596790   [免费支持更新]A 代码生成器(开发利器);全部是源码     增删改查的处理类,service层,mybatis的xml,SQL( mysql   和oracle)脚本,   jsp页面 都生成   就不用写搬砖的代码了,生成的放到项目里,可以直接运行B 阿里巴巴数据库连接池druid;  数据库连接池  阿里巴巴的 druid.Druid在监控.可扩展性.稳定性和性能方面都有明显的优势C 安全权限框架shiro ;  Shiro 是一个用

springmvc mybatis 整合

获取[下载地址]   [免费支持更新]三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单;freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等完整模块B 集成阿里巴巴数据库连接池druid;  数据库连接池  阿里巴巴的 druid.Drui