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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 最近游戏更新 未整理 无图片 续传_续薪火浅浅烂

    最近游戏更新 未整理 无图片 续传_续薪火浅浅烂模拟人生之超级明星(TheSims:Superstar)三项属性修改器http://patch.ali213.net/download.asp?id=2872黑客帝国(EntertheMatrix)v1.51升级档五项属性修改器http://patch.ali213.net/download.asp?id=2868黑客帝国(EntertheMatrix)v1.52升级档激活成功教程版

    2022年9月21日
    2
  • mac安装idea以及激活方法2021【2021最新】

    (mac安装idea以及激活方法2021)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~3YVY…

    2022年3月30日
    2.0K
  • Python里divmod_Python divmod

    Python里divmod_Python divmod前言我们都知道,python中//代表整数运算中的取整,%代表整数运算中的取余,那么有什么函数可以同时取到整数和余数吗?答案是有的,使用python内置函数divmoddivmod首先看一下源

    2022年7月31日
    10
  • 用Java IO流实现下载文件

    用Java IO流实现下载文件  @RequestMapping(value="download")   publicStringdownload(HttpServletResponseresponse,Modelmodel){             //通过文件名找出文件的所在目录      StringURL="D:/one/two.txt";      //得到要下载的文件…

    2022年5月30日
    49
  • ubuntu远程桌面控制_ubuntu 远程控制

    ubuntu远程桌面控制_ubuntu 远程控制已经使用的,备份Section”Monitor”Identifier”Monitor0″HorizSync28.0-80.0VertRefresh48.0-75.0#https://arachnoid.com/modelines/#1920×1080@60.00Hz(GTF)hsync:67.08kHz;pclk:172…

    2022年8月21日
    9
  • 图像分割的评价指标_图像实例分割

    图像分割的评价指标_图像实例分割图像分割可分为:语义分割,实例分割,全景分割1、语义分割(semanticsegmentation):标注方法通常是给每个像素加上标签;常用来识别天空、草地、道路等没有固定形状的不可数事物(stuff)。2、实例分割(instancesegmentation):标注方法通常是用包围盒(bbox?)或分割掩码标记目标物体;常用来识别人、动物或工具等可数的、独立的明显物体(things…

    2022年8月23日
    4

发表回复

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

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