Spring 和 Mybatis 整合

Spring本身的Config文件:

在IDEA下面配置好文件后, 在WEB-INF下面有三个配置文件分别是web.xml, applicationContext.xml, dispatcher-servlet.xml.

其中, web.xml是总体的配置, 具体内容为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>

可见web.xml中配置了其他两个配置项, 注册了一个sevlet作为dispacher, appcontext对应applicationContext.xml, 是注册其他bean的. 我们整合也是配置的这个文件. 原始的applicationContext.xml只有一个beans的定义.

修改过的applicationContext.xml的内容:

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="/WEB-INF/jdbc.properties"/> <!--&lt;!&ndash; 1. 数据源 : DriverManagerDataSource &ndash;&gt;-->
<!--<bean id="dataSource"-->
<!--class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--<property name="driverClassName" value="com.mysql.jdbc.Driver" />-->
<!--<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />-->
<!--<property name="username" value="root" />-->
<!--<property name="password" value="123456" />-->
<!--</bean>--> <!--&lt;!&ndash;-->
<!--2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源--> <!--MyBatis定义数据源,同意加载配置-->
<!--&ndash;&gt;-->
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<!--<property name="dataSource" ref="dataSource"></property>-->
<!--<property name="configLocation" value="classpath:config/mybatis-config.xml" />-->
<!--</bean>--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载Mybatis全局配置文件 -->
<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml"/>
</bean> <!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,如果需要扫描多个包中间用半角逗号隔开 -->
<property name="basePackage" value="lyb.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean> <!--
4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 5. 使用声明式事务
transaction-manager:引用上面定义的事务管理器
-->
<tx:annotation-driven transaction-manager="txManager" />
</beans>

主要是增加了几个bean, dataSource的bean是配置数据库信息的, 根据jdbc的properties来加载数据库; sqlSessionFactory是配置Mybatis的配置文件的;

还有一个bean是配置mapper的文件夹的; 最后一个是配置事务的.

其中配置Mybatis的文件的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <!--&lt;!&ndash; 实体类,简称 -设置别名 &ndash;&gt;-->
<!--<typeAliases>-->
<!--<typeAlias alias="User" type="com.tgb.model.User" />-->
<!--</typeAliases>-->
<!-- 实体接口映射资源 -->
<!--
说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml
-->
<mappers>
<mapper resource="lyb/mapper/StudentMapper.xml" />
</mappers> </configuration>

就是Mybatis的mapper注册的配置项.

其他的就是在实体类的同一个文件夹下面写好实体类对应的sql语句的xml就好了.

Spring 和 Mybatis 整合的更多相关文章

  1. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  2. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  3. Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...

  4. Mybatis学习--spring和Mybatis整合

    简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...

  5. 九 spring和mybatis整合

    1       spring和mybatis整合 1.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...

  6. Mybatis学习(7)spring和mybatis整合

    整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...

  7. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

  8. SpringMVC, Spring和Mybatis整合案例一

    一  准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二  整合思路 Dao层: 1.SqlMapConfig. ...

  9. MyBatis学习七:spring和MyBatis整合

    <\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...

  10. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

随机推荐

  1. 2018ACM-ICPC宁夏邀请赛 A-Maximum Element In A Stack(栈内最大值)

    Maximum Element In A Stack 20.91% 10000ms 262144K   As an ACM-ICPC newbie, Aishah is learning data s ...

  2. Fedora/CentOS使用技巧

    命令 获取系统安装包的编译源码及脚本 # dnf download --source package # yumdownloader --source virt-viewer 远程连接windows ...

  3. Unity 5着色器系统代码介绍(下)

    http://forum.china.unity3d.com/thread-25738-1-10.html 上一篇对着色器系统的工作原理做了介绍,现在我们将继续深入,将目光聚焦在标准着色器的光照函数. ...

  4. 2014 Noip提高组 Day2

    P2038 无线网络发射器选址 [题目描述] 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 ...

  5. js获取当前地址栏的域名、Url、相对路径和参数以及指定参数

    以下代码整理于网络 1.设置或获取对象指定的文件名或路径. window.location.pathname 例:http://localhost:8086/topic/index?topicId=3 ...

  6. Struts2拦截器再认识

    拦截器(Interceptor)是 Struts 2 的核心组成部分. Struts2 很多功能都是构建在拦截器基础之上的,例如文件的上传和下载.国际化.数据类型转换和数据校验等等. Struts2 ...

  7. Codeforces Round #566 (Div. 2) C. Beautiful Lyrics

    链接: https://codeforces.com/contest/1182/problem/C 题意: You are given n words, each of which consists ...

  8. bzoj2662: [BeiJing wc2012]冻结 最短路 建图

    好久没有1A题啦♪(^∇^*) 一个sb建图,我居然调样例调了10min 看起来是双向边,其实在建图的时候要当成有向图, 否则他会时间倒流(233) 把每个点裂成k个点,然后把每条边裂成4条边(正向反 ...

  9. 2017ACM/ICPC广西邀请赛 Duizi and Shunzi

    题意:就是一个集合分开,有两种区分 对子:两个相同数字,顺子:连续三个不同数字,问最多分多少个 解法:贪心,如果当前数字不构成顺子就取对子 /2,如果可以取顺子,那么先取顺子再取对子 #include ...

  10. tomcat7 fail to start inside Ubuntu Docker container

    The tomcat startup script needs some special privileges. Concrete it needs to check all running proc ...