Spring 和 Mybatis 整合
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"/>
<!--<!– 1. 数据源 : DriverManagerDataSource –>-->
<!--<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>-->
<!--<!–-->
<!--2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源-->
<!--MyBatis定义数据源,同意加载配置-->
<!--–>-->
<!--<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>
<!--<!– 实体类,简称 -设置别名 –>-->
<!--<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 整合的更多相关文章
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
- Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...
- Mybatis学习--spring和Mybatis整合
简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...
- 九 spring和mybatis整合
1 spring和mybatis整合 1.1 整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...
- Mybatis学习(7)spring和mybatis整合
整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- SpringMVC, Spring和Mybatis整合案例一
一 准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二 整合思路 Dao层: 1.SqlMapConfig. ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
随机推荐
- select2,利用ajax高效查询大数据列表(可搜索、可分页)
二.导入css和js到网站上 1.使用CDN,节省自己网站的流量 ? 1 2 <link href="https://cdnjs.cloudflare.com/ajax/libs/se ...
- VC OnCtlColor函数来修改控件背景颜色
CWnd::OnCtlColor afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ); 返回值:OnCtlColor必须 ...
- C# 生成word 文档 代码 外加 IIS报错解决方案
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- VS中运行后控制台窗口一闪就没了
使用VS2010后,用Ctrl+F5运行程序,结果控制台窗口一闪就没了,也没有出现”press any key to continue…” 或者“请按任意键继续”. 出现这种原因,主要是建立工程时选用 ...
- thinkphp5实现mysql数据库还原
数据库还原其实就是从.sql文件中读取一行一行的命令,然后执行 需要配置数据库文件database.php,数据库名,主机名,用户名,密码这里就不说了,这里说的要配置数据库连接参数 'params' ...
- Java 环境问题汇总
准备java环境时,需要设置JAVA_HOME 和 Path , CLASSPATH 环境变量,它们可以是用户变量,也可以是系统变量. 注意: 系统变量的路径排在用户变量之前. 其中,Windows操 ...
- LeetCode:灯泡开关2
题目 现有一个房间,墙上挂有 n 只已经打开的灯泡和 4 个按钮.在进行了 m 次未知操作后,你需要返回这 n 只灯泡可能有多少种不同的状态. 假设这 n 只灯泡被编号为 [1, 2, 3 ..., ...
- mysql读取不同位置配置文件顺序
读取顺序为: /etc/my.cnf basedir/my.cnf datadir/my.cnf --defaults-extra-file #在读取全局配置文件之后,读取用户配置文件(~/.m ...
- springboot集成log4j
需求: 1.springboot集成log4j 2.mybatis 打印 sql 实现: pom.xml <dependency> <groupId>org.springfra ...
- .NET Core中Circuit Breaker
谈谈Circuit Breaker在.NET Core中的简单应用 前言 由于微服务的盛行,不少公司都将原来细粒度比较大的服务拆分成多个小的服务,让每个小服务做好自己的事即可. 经过拆分之后,就避免不 ...