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


相关推荐

  • ETH显卡矿机_eth矿机组装

    ETH显卡矿机_eth矿机组装显卡矿机搭建选择合适显卡选择硬件选择挖矿软件挖矿系统mineros挖矿软件注意:每个币种的软件都不一样挖矿系统和软件也有多种具体对应的官网都会有教程选择合适显卡主流显卡算力对比选择硬件选择挖矿软件前提准备自己的钱包地址选择矿池地址挖矿系统mineros步骤:注册账号刻盘启动挖矿和监控矿机状态挖矿软件NBMiner…

    2022年9月27日
    0
  • 语音识别/合成开源项目

    语音识别/合成开源项目转自:https://blog.csdn.net/github_19776427/article/details/52530066语音识别项目:http://www.oschina.net/project/tag/203/tts-speech sf.net http://www.codesoso.net/Search?q=%D3%EF%D2%F4%CA%B6%B1%F0&amp;l=chttp:/…

    2022年6月26日
    49
  • 服务器可以ghost备份吗_服务器可以用dism备份吗

    服务器可以ghost备份吗_服务器可以用dism备份吗带RAID服务器能GHOST备份吗?一、不可以的原因:1、从saymantec上查询到不行:Ghost与RAID的兼容性情形本文介绍Ghost与使用RAID的计算机的兼容性。解释请注意:无论驱动器使用软件级RAID还是硬件级RAID,赛门铁克都不提供制作RAID驱动器映像的技术支持。能否成功制作RAID驱动器映像取决于特定的计算机模型、驱动程序控制器、硬盘驱动器和…

    2022年9月5日
    2
  • 海思hi3798mv300和310的区别_海思3516

    海思hi3798mv300和310的区别_海思3516在网上搜寻海思HI3519或者hi3516实时识别除了官方给的SDK中有Rfcn的实时识别例子之外,其它都是读图片例子。官方例子中给了如下网络参考:Rfcn、Segnet、FasterRcnn、Cnn、Ssd、Yolov1、Yolov2、Yolov3、Lstm、Pvanet在SDK中文件夹路径为/rp-hi3516dv300-busybox/smp/a7_linux/mpp/sample/svp/nnie其中有一个sample_nnie_main.c文件,官方例子如下:/************

    2022年9月23日
    0
  • Java分布式应用技术架构介绍

    Java分布式应用技术架构介绍分布式架构的演进系统架构演化历程-初始阶段架构初始阶段的小型系统应用程序、数据库、文件等所有的资源都在一台服务器上通俗称为LAMP特征:应用程序、数据库、文件等所有的资源都在一台服务器上。描述:通常服务器操作系统使用linux,应用程序使用PHP开发,然后部署在Apache上,数据库使用Mysql,汇集各种免费开源软件以及一台廉价服务器就可以开始系统

    2022年6月12日
    33
  • 搭建环境是什么意思_如何搭建安卓开发环境

    搭建环境是什么意思_如何搭建安卓开发环境WebIDE下载网址:https://tools.hana.ondemand.com/#sapui5下载好之后打开进入WEBIDE\eclipse\config_master\service.destinations\destinations,在destinations文件下面拷入SAPGUI的客户端配置(txt文档),URL填入GUI上配置的服务器地址就行,端口就配8080(有冲突的话在后台关掉),格式如下之后返回\WEBIDE\eclipse打开orion运行运…

    2022年10月10日
    0

发表回复

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

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