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


相关推荐

  • UVA 12627 – Erratic Expansion

    UVA 12627 – Erratic Expansion

    2022年2月4日
  • php微信自动回复机器人,微信自动回复机器人功能怎么实现?[通俗易懂]

    php微信自动回复机器人,微信自动回复机器人功能怎么实现?[通俗易懂]原标题:微信自动回复机器人功能怎么实现?微信自动回复机器人功能怎么实现?最近有不少小伙伴都在询问这个问题。很多人在微信营销的过程中,都会有这样的问题,微信好友太多,想要都在第一时间回复,实在没有精力。下面小编就给大家分享如何使用微信自动回复机器人,大家再也不用担心回复不过来而忙的焦头烂额啦。首先通过米云微信软件,扫码将所以微信号登录,就可以将所有微信号的对话集成,全部聊天里整合了所有微信号的聊天…

    2022年10月1日
  • 大学四年,我把私藏的自学「学习网站/实用工具」都贡献出来了

    大学四年,我把私藏的自学「学习网站/实用工具」都贡献出来了在分享之前,先说说初学者如何学习编程,这个话题想必非常的重要,要学好编程,给你一些学习网站也好、实用工具也好,但前提是你知道如何去学习它。见过很多初学者,以及小鹿我刚开始学习的时候,也是自己瞎摸索,找不到路子,看什么书?看什么资料?编程的方向太多了,如果确定自己的方向?尤其是上大一、大二甚至大三还没有确定自己到底是学习前端还是后天,每天这学一点,那学一块,掌握那么多,没有一门精通的,去面试的时候…

    2022年6月12日
  • bs和cs架构的区别和优缺点_百年灵b1p1和b1x1区别

    bs和cs架构的区别和优缺点_百年灵b1p1和b1x1区别BS和CS架构的区别BS就是浏览器服务器架构(网站)CS就是需要安装的那些应用程序app二者比较:标准:BS开发更标准一些,因为CS需要在不同的系统上执行,BS只需要在浏览器上执行效率:CS效率更高,CS属于安装的软件,很多内容已经安装在电脑中了,只需要联网获取数据即可,而BS运行在浏览器上,所有的数据必须经过下载才能使用;升级:BS无缝升级,CS需要删除老版本,再安装新版本安全性:CS更为安全,因为必须安装软件才能使用;BS安全度较低,只要有浏览器就可以使用开发成本:CS开发成本更高

    2022年10月17日
  • python最长回文子串动态规划_最长回文子串问题

    python最长回文子串动态规划_最长回文子串问题问题描述回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。输入一个字符串Str,输出Str里最长回文子串的长度。方法一:暴力求解遍历每一个子串,再判断这个子串是不是回文串,最后判断这个串是不是最长的回文子串。遍历子串的复杂度是O(n^2),判断是不是回文串的复杂度是O(n),所以这个算法的复杂度是O(n^3)。方法二:动态规划法用一个二维的数组ai来表示从第i位到第j位的子…

    2022年6月1日
  • ZUUL-API网关[通俗易懂]

    ZUUL-API网关[通俗易懂]更多干货 分布式实战(干货) springcloud实战(干货) mybatis实战(干货) springboot实战(干货) React入门实战(干货) 构建中小型互联网企业架构(干货) python学习持续更新 ElasticSearch笔记 kafkastorm实战(干货) …

    2022年8月15日

发表回复

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

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