关于spring和ibatis的整合
没大段时间写博客,工作的时候记录一些,等有时间了再写博客总结吧。现在都是加班来会议一天到底学到了什么,然后记录一些... 觉得盲目的工作实在是太无趣了。
spring现在普及度很广,在项目中就像千手观音一般,无所不能。
而ibatis几十年来的orm,现已经转成myBitis,鉴于现在orm的数目是在太多,ibatis也越来越少人用了,事实上一个orm在spring看来只是千手观音中的一个手上的法宝罢了。
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="classpath:applicationContext-dao.xml" /> <import resource="classpath:applicationContext-resources.xml" /> <import resource="classpath:applicationContext-service.xml" /> </beans>
相当清晰。
spring拿到ibatis法宝:
applicationContext-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-lazy-init="true"> <!-- Transaction manager for a single JDBC DataSource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- SqlMap setup for iBATIS Database Layer --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:/sql-map-config.xml</value> </property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userDao" class="com.syezon.webapp.dao.ibatis.UserDaoImpl"> <property name="dataSource" ref="dataSource" /> <property name="sqlMapClient" ref="sqlMapClient" /> </bean> </beans>
配置过单独的ibatis的话,就知道我们是要自己配置transactionManager和sqlMapClient的。
现在我们把这两个对象交给spring管理了
注意到sqlMapClient参数的configLocation:classpath:/sql-map-config.xml
我们来看看:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <settings enhancementEnabled="true" maxTransactions="20" maxRequests="32" maxSessions="10" useStatementNamespaces="true" /> <!-- Identify all SQL Map XML files to be loaded by this SQL map. Relative to classpath --> <sqlMap resource="sqlmaps/SaleS2dSQL.xml" /> <sqlMap resource="sqlmaps/DealerSQL.xml" /> </sqlMapConfig>
resource就是写sql文的文件啦,那么就让我们开始写sql吧。
来两条select语句吧:其中蕴含了大部分可能遇到的问题,包括
isEqual 判断,<![CDATA[是为了尖括号能够使用
<select id="getUsedAdmins" parameterClass="java.util.Map" resultMap="adminNameResult"> <![CDATA[ select id,name from tb_admin where status = 1 ]]> <isEqual property="type" compareValue="3"> <![CDATA[ and roleid=3 ]]> </isEqual> <isEqual property="type" compareValue="4"> <![CDATA[ and roleid>3 and roleid<6 ]]> </isEqual> </select>
<select id="getAdmins" parameterClass="java.util.Map" resultMap="adminResult"> select * from (select rownum rn,m.* from(select * from tb_admin where status=1 and roleid=1 <isNotEqual property="roleid" compareValue="0" prepend="and"> id in(select user_id from qx_user_role where role_id=#roleid#) </isNotEqual> <![CDATA[ order by id desc)m ) where rn>#firstrow# and rn<#maxrow# ]]> </select> <select id="getAdmin" parameterClass="java.lang.Long" resultMap="adminResult"> <![CDATA[ select * from tb_admin where id = #value# and status = 1 ]]> </select>
insert语句可能会牵涉到自增长自然主键
http://www.cnblogs.com/killbug/archive/2012/12/23/2830252.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述