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


相关推荐

  • 第十章 :hbase集群搭建,测试

    第十章 :hbase集群搭建,测试第十章 :hbase集群搭建,测试

    2022年4月23日
    51
  • Could not initialize class org.xerial.snappy.Snappy

    Could not initialize class org.xerial.snappy.Snappy

    2021年5月13日
    163
  • Linux 之 zsh

    Linux 之 zsh安装现在好多linux发行版好像都自带zsh的,比如说centos。[root@master~]#chsh-l/bin/sh/bin/bash/usr/bin/sh/usr/bin/bash/usr/bin/tmux/bin/zsh/usr/bin/fish如果实在没有的话,就用yum安装一个。yuminstallzsh…

    2025年5月23日
    1
  • HTML5知识初级题目

    在HTML5中,onblur和onfocus是:在HTML5中,哪个元素用于组合标题元素?HTML5中不再支持下面哪个元素?HTML5中不再支持下面哪个元素?HTML5之前的HT

    2021年12月21日
    49
  • C语言中输入输出所有格式控制符

    C语言中输入输出所有格式控制符title:C语言中输入输出所有格式控制符date:2020-01-1617:02:06description:C语言输入输出的所有格式控制详解C语言中输入输出所有格式控制符 最近在重温C语言,发现C语言的输入输出函数scanf和printf函数在控制输入输出时有许多控制符来控制输入输出数据的格式。于是就打算来整理一下。参考百度百科词条 scanf()是C语言中的一个输入…

    2022年7月24日
    18
  • python判断是否为空_python 判断对象是否为空

    python判断是否为空_python 判断对象是否为空在实际的工作当中,我们难免要与空值打交道,相信不少初学者都会写出下面的代码:ifaisNone:dosomething.else:dotheotherthing.python学习网,大量的免费python视频教程,欢迎在线学习!这样写看起来不错,但实际上会有问题。一般来讲,Python中会把下面几种情况当做空值来处理:NoneFalse0,0.0,0L”,(),[],{}其中Non…

    2022年6月12日
    166

发表回复

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

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