mybatis-spring整合的三种(逐渐优化)方案

简介: 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对象

其次就是上述的三种整合方案选择。

相关文章
|
1月前
|
缓存 监控 Java
《深入理解Spring》性能监控与优化——构建高性能应用的艺术
本文系统介绍了Spring生态下的性能监控与优化实践,涵盖监控体系构建、数据库调优、缓存策略、线程池配置及性能测试等内容,强调通过数据驱动、分层优化和持续迭代提升应用性能。
|
1月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
121 8
|
2月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
529 5
|
7月前
|
前端开发 Java UED
从基础到进阶:Spring Boot + Thymeleaf 整合开发中的常见坑与界面优化
本文深入探讨了 **Spring Boot + Thymeleaf** 开发中常见的参数绑定问题与界面优化技巧。从基础的 Spring MVC 请求参数绑定机制出发,分析了 `MissingServletRequestParameterException` 的成因及解决方法,例如确保前后端参数名、类型一致,正确设置请求方式(GET/POST)。同时,通过实际案例展示了如何优化支付页面的视觉效果,借助简单的 CSS 样式提升用户体验。最后,提供了官方文档等学习资源,帮助开发者更高效地掌握相关技能。无论是初学者还是进阶用户,都能从中受益,轻松应对项目开发中的挑战。
383 0
|
7月前
|
存储 Java 数据库
Spring Boot 注册登录系统:问题总结与优化实践
在Spring Boot开发中,注册登录模块常面临数据库设计、密码加密、权限配置及用户体验等问题。本文以便利店销售系统为例,详细解析四大类问题:数据库字段约束(如默认值缺失)、密码加密(明文存储风险)、Spring Security配置(路径权限不当)以及表单交互(数据丢失与提示不足)。通过优化数据库结构、引入BCrypt加密、完善安全配置和改进用户交互,提供了一套全面的解决方案,助力开发者构建更 robust 的系统。
236 0
|
6月前
|
人工智能 负载均衡 Java
Spring AI Alibaba 发布企业级 MCP 分布式部署方案
本文介绍了Spring AI Alibaba MCP的开发与应用,旨在解决企业级AI Agent在分布式环境下的部署和动态更新问题。通过集成Nacos,Spring AI Alibaba实现了流量负载均衡及节点变更动态感知等功能。开发者可方便地将企业内部业务系统发布为MCP服务或开发自己的AI Agent。文章详细描述了如何通过代理应用接入存量业务系统,以及全新MCP服务的开发流程,并提供了完整的配置示例和源码链接。未来,Spring AI Alibaba计划结合Nacos3的mcp-registry与mcp-router能力,进一步优化Agent开发体验。
2444 14
|
11月前
|
JSON Java API
利用Spring Cloud Gateway Predicate优化微服务路由策略
Spring Cloud Gateway 的路由配置中,`predicates`​(断言)用于定义哪些请求应该匹配特定的路由规则。 断言是Gateway在进行路由时,根据具体的请求信息如请求路径、请求方法、请求参数等进行匹配的规则。当一个请求的信息符合断言设置的条件时,Gateway就会将该请求路由到对应的服务上。
977 72
利用Spring Cloud Gateway Predicate优化微服务路由策略
|
10月前
|
Java 关系型数据库 数据库连接
简单易懂的 MyBatis 分库分表方案
本文介绍了一种基于 MyBatis 框架的数据库分库分表方案——shardino。不同于复杂插件方式,该方案通过客户端代码包装实现简便易懂的操作,显式处理分库分表逻辑,确保开发者清晰了解数据分布。项目地址:[https://github.com/pyloque/shardino](https://github.com/pyloque/shardino)。方案中,帖子表按 userId 字段 hash 分为 64 张表,平均分配到多个主从库中,配置文件管理 MySQL 组对象,支持读写分离和权重随机选择从库。代码示例展示了如何计算 partition number 并进行具体操作。
324 22
简单易懂的 MyBatis 分库分表方案
|
11月前
|
SQL Java 数据库连接
MyBatis-Plus高级用法:最优化持久层开发
MyBatis-Plus 通过简化常见的持久层开发任务,提高了开发效率和代码的可维护性。通过合理使用条件构造器、分页插件、逻辑删除和代码生成器等高级功能,可以进一步优化持久层开发,提升系统性能和稳定性。掌握这些高级用法和最佳实践,有助于开发者构建高效、稳定和可扩展的企业级应用。
659 13
下一篇
oss云网关配置