spring-mybatis整合-SqlSessionTemplate

spring-mybatis整合-SqlSessionTemplateSqlSessionTemplate是MyBatis-Spring的核心。这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL方法,翻译异常。SqlSessionTemplate是线程安全的,可以被多个DAO所共享使用。当调用SQL方法时,包含从映射器getMapper()方法返回的方法,SqlSessionTemplate将会保证使用的SqlSession是和当前S

大家好,又见面了,我是你们的朋友全栈君。

SqlSessionTemplate是MyBatis-Spring的核心。这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL方法。SqlSessionTemplate是线程安全的,可以被多个DAO所共享使用。

当调用SQL方法时,包含从映射器getMapper()方法返回的方法,SqlSessionTemplate将会保证使用的SqlSession是和当前Spring的事务相关的。此外,它管理session的生命周期,包含必要的关闭,提交或回滚操作。

SqlSessionTemplate实现了SqlSession,这就是说要对MyBatis的SqlSession进行简易替换。

SqlSessionTemplate通常是被用来替代默认的MyBatis实现的DefaultSqlSession,因为它不能参与到Spring的事务中也不能被注入,因为它是线程安全的。相同应用程序中两个类之间的转换可能会引起数据一致性的问题。

SqlSessionTemplate对象可以使用SqlSessionFactory作为构造方法的参数来创建。

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory"/>
	</bean>

代码

spring配置文件

<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        	http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
        	default-lazy-init="false">

	<!-- 数据 -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/lpWeb"/>
		<property name="username" value="root"/>
		<property name="password" value="root123"/>
	</bean>
	 
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="mapperLocations" value="classpath*:config/mapper/*.xml"/>
	</bean>
	
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory"/>
	</bean>
	 <bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
	</bean> 
	
	
</beans>

映射文件PersonMapper.xml

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.com.mybatis.dao.mapper.PersonMapper">
	<resultMap type="cn.com.mybatis.mapper.Person" id="personmap">
		<id column="USER_NAME" property="username" javaType="string" jdbcType="VARCHAR"/>
		<result column="SEX" property="sex" javaType="string" jdbcType="VARCHAR"/>
		<result column="AGE" property="age" javaType="int" jdbcType="INTEGER"/>
	</resultMap>
	
	<select id="getPerson" resultMap="personmap">
		select * from person
	</select>
	
	<insert id="insert" parameterType="java.util.Map">
	    insert into person (USER_NAME, SEX, AGE) values (
	    	#{username}, #{sex}, #{age}
	    )
	</insert>
	
	<update id="update">
	update person set AGE = #{age}
	where USER_NAME = #{username}
	</update>
	
	<delete id="delete">
	delete from person;
	</delete>
</mapper>

测试类:

package mybatis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:spring-mybatis4.xml"})
public class TestSqlsessionTemplate {

	@Autowired
	SqlSessionTemplate sqlSessionTemplate;
	
	@Test
	public void testTemplate(){
		try{
			System.out.println(sqlSessionTemplate.selectList("getPerson"));
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/138360.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • plot函数详解_plot函数参数

    plot函数详解_plot函数参数plot(X,Y):绘制Y关于X的函数。plot(X,Y,LineSpec):指定线形,标记,颜色等。详细plot(_,Name,Value):指定线的属性。举例:plot(x,y1,’k-‘,x,y2,’b–‘,x,y3,’r:’,’linewidth’,1.5);figure(1)plot(x,Psum,’k-‘,x,Pz1,’b–‘,x,Pr1,’r:’,’line

    2022年10月15日
    3
  • ajax的实现_培训的基本内容有哪些?

    ajax的实现_培训的基本内容有哪些? 点击这里下载PDF文件。  点击这里下载示例文件。  点击这里下载视频文件。  相关内容:AJAX培训第二讲:使用AJAX框架(上)  “AJAX培训第二讲:使用AJAX框架”现在拆成了两部分,现在发布是第一部分,探讨了AJAX框架相关内容,并给出了一些最简单的例子。  如果大家对于讲座的内容有任何疑问,请在Q&A专用文章里进行提问,当然如果您有其它任何疑问的话,也能在那里提出,我会尽快为您

    2025年10月29日
    3
  • GIS Experience (六):QGIS安装及使用教程

    GIS Experience (六):QGIS安装及使用教程目录

    2022年6月15日
    41
  • 关于harbor私有仓库忘记登录密码

    关于harbor私有仓库忘记登录密码

    2021年5月30日
    131
  • java buttongroup方框_Swing之ButtonGroup用法实例 | 学步园

    java buttongroup方框_Swing之ButtonGroup用法实例 | 学步园1就是起作用范围的,不是组件,不能被容器添加,目的让其中一个起作用,例如radiobuttonpackagecom.szsm.swing.framepanel;importjava.awt.FlowLayout;importjavax.swing.ButtonGroup;importjavax.swing.JRadioButton;importcom.szsm.swing.parent…

    2025年7月27日
    3
  • 详细设计说明书编写规范「建议收藏」

    详细设计说明书编写规范「建议收藏」第1章引言  1.1目的  使项目详细设计说明书的编写规范化,从而规范软件管理。尽可能详细地描述程序的各成份的设计考虑,以利于编制程序。  [此处加入编写目的]  1.2背景  说明该软件系统名称,开发者,详细设计原则和方案  [此处加入项目背景资料]  1.3参考资料  列出有关的参考资料名称,作者,发表日期,出版单位  [此处加入参考资料]  

    2022年5月25日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号