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. sql中的begin....end

    Begin...End之间的是一个语句块,一般Begin...End用在  ( 相当于  {})whileif等语句中在T_SQL中,if/while后只能紧跟一条sql语句,如果有多条则应该用Beg ...

  2. js用"."和"[]"获取属性的区别

    在JavaScript中通常使用”."运算符来存取对象的属性的值.或者使用[]作为一个关联数组来存取对象的属性. 对象的属性和方法统称为对象的成员. 访问对象的属性 在JavaScript中 ...

  3. Web.config中的设置 forms 中的slidingExpiration的设置

    在ASP.NET 网站中,使用 Forms Authentication时,一般的设置是如下的: <authentication mode="Forms"> <f ...

  4. Fedora下Msitools使用

    msitools学习 msitools使用 wixl-heat使用 概述 打包程序就是把程序依赖的所有库文件和可执行文件以及其他一些资源文件按照源目录结构进行压缩,知道自己的程序依赖哪些库是简单的,但 ...

  5. Linux运用一些常用命令

    今天搜集整理了一些Linux服务器运维常用命令,希望对大家有帮助:1.删除0字节文件 find -type f -size 0 -exec rm -rf {} 2.查看进程按内存从大到小排列 ps - ...

  6. 关于HTML5用SVG画图

    SVG在HTML5中的应用 SVG(Scalable Vector Graphics)是用来绘制矢量图的HTML5标签.只要定义好XML属性就能够获得与其一致的图像元素. 使用SVG之前先将标签加入到 ...

  7. 7.Python初窥门径(数据类型补充,操作及注意事项)

    python(数据类型补充,转换及注意事项) 数据类型补充 str str.capitalize() 首字母大写 str.title() 每个单词首字母大写 str.count() 统计元素在str中 ...

  8. 01分数规划初探?!By cellur925

    都要\(NOIp\)了为啥我还在看这种玄学玩意..... \(01\)分数规划:这是一个问题模型\(qwq\),一般是在求\[\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i ...

  9. Spring配置文件xsi:schemaLocation无法解析导致启动失败的解决方案

    今天遇到过情况,spring的配置文件在本地读取没有问题,扔到线上服务器运行就报无法解析xml,找了很久问题,发现是因为线上服务器无法上网,导致无法下载相关的xsd文件,没办法不能上网就只有使用本地的 ...

  10. SpringBoot配置属性二

    server配置 server.address指定server绑定的地址 server.compression.enabled是否开启压缩,默认为false. server.compression.e ...