mybatis-spring整合的三种(逐渐优化)方案
时间:2022-07-23
本文章向大家介绍mybatis-spring整合的三种(逐渐优化)方案,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
第一种就是作者之前写过的,就是通过编写是实现类然后在实现类里面实现接口的方法,然后在applicationcontex.xml文件中注册创建一个该实现类的bean对象,然后在该对象中注入SQLSession的以来即可
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!-- 读取数据库的相关配置文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!-- 创建数据源DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 最大连接数 -->
<property name="maxActive" value="10"></property>
<!-- 最大空闲数 -->
<property name="maxIdle" value="5"></property>
</bean>
<!-- 创建一个SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!--加载sql映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 创建一个CustomerMapperImpl对象,然后向里面注入SQLSessionFactory的对象 -->
<bean id="customerMapper" class="cn.ssm1234.dao.impl.CustomerMapperimpl">
<!-- 关联或者输入sqlSeesionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
第二种简化了操作实现类的操作,该方法中,我们直接注册创建mapper的接口的一个bean对象,然后向里面注入SQLSession的以来即可
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!-- 读取数据库的相关配置文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!-- 创建数据源DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 最大连接数 -->
<property name="maxActive" value="10"></property>
<!-- 最大空闲数 -->
<property name="maxIdle" value="5"></property>
</bean>
<!-- 创建一个SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!--加载sql映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 配置Mapper接口 -->
<bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 关联Mapper接口 -->
<property name="mapperInterface" value="cn.ssm1234.dao.CustomerMapper"></property>
<!-- 关联SqlSessionfactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
第三种就是最简便,这里我们可以直接创建一个mapper接口的扫描器,这样我们只要定义扫描器所扫描的包,那样我们就可以一次性全部扫描出我们要用马屁拍二接口,而不需要没创建一个mapper接口就去创建该mapper接口的对象
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!-- 读取数据库的相关配置文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!-- 创建数据源DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 最大连接数 -->
<property name="maxActive" value="10"></property>
<!-- 最大空闲数 -->
<property name="maxIdle" value="5"></property>
</bean>
<!-- 创建一个SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!--加载sql映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- Mapper接口的扫描器 -->
<!--如果使用Mapper接口包扫描,那每个Mapper接口在spring容器中的id名称为类名:例如CustomerMapper就叫做customerMapper(类名首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper接口包所在的路径 -->
<property name="basePackage" value="cn.ssm1234.dao"></property>
</bean>
<!-- 开启Spring的IOC注解扫描 -->
<context:component-scan base-package="cn.ssm1234"></context:component-scan>
</beans>
这里我们总结一下,在mybatis与Spring的整合过程中,必须的两步就是 1.连接数据库的相关操作 2.配置SQLSession对象 其次就是上述的三种整合方案选择。
- mongodb11天之屠龙宝刀(三)基本操作:增删改查与mysql对比
- Go语言 实现bitmap
- 最近的几个技术问题总结和答疑(八) (r9笔记第72天)
- Java基础-day05-代码题
- MySQL级联复制的同步问题(一) (r9笔记第71天)
- GO语言 TCP传输实例
- JavaScript 性能优化技巧分享
- 一个拷贝操作导致的潜在监听类问题(r9笔记第70天)
- Java基础-day05-基础题
- 【Go 语言社区算法课程 第一季 第3节 hash算法实现
- mongodb11天之屠龙宝刀(四)高级查询:MongoDB内嵌字段查询
- merge语句导致的ORA错误分析(r9笔记第67天)
- Go语言的单例模式(Singleton)
- DPDK 全面分析
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- CentOS 8 如何安装 htop
- Discourse 重复安装过程中的密钥签发问题
- Appium之「元素定位和UiAutomator表达式」
- 机器人软件开发:机器人开源库安装
- 2020-10-05:如何求模平方根?
- leetcode栈之比较含退格的字符串
- Discourse 如何不使用 Let’s Encrypt 而使用 CA 签名的密钥进行安装
- 3分钟短文:Laravel slug,让你的url地址更“好记”
- Qt音视频开发28-Onvif信息获取
- CentOS 8 启用 NTP 服务
- Discourse 如何使用命令行方式进行恢复
- CentOS 8 Apache 启用 SSL
- leetcode栈之二叉树的前序遍历
- 前端学数据结构与算法(七): 从零实现优先队列-堆及其应用
- 前端学数据结构与算法(六):二叉树的四种遍历方式及其应用