autoCommit自动提交事务

autoCommit自动提交事务

今天学习了下mybatis,

对其中的autoCommit自动提交事务比较好奇,

研究了下,把配置和代码都放上

mapper.xml如下:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace隔离sql -->
<mapper namespace="User">
<!-- id:statement的标识 -->
<!--
#{}表示占位符(转为?),可以防止SQL注入;如果输入参数类型是简单类型,输入参数名称可以是value或者其它任意名称
-->
<select id="getUserById" parameterType="int" resultType="org.pine.mybatis.po.User">
select * from user t where t.id = #{id}
</select> <!-- resultType:单条记录的类型 -->
<select id="listUserByUserame1" parameterType="java.lang.String" resultType="org.pine.mybatis.po.User">
select * from user t where t.username like #{username}
</select> <!--
${}表示拼接SQL(拼接参数),不能防止SQL注入;如果输入参数类型是简单类型,输入参数名称只能是value
-->
<select id="listUserByUsername2" parameterType="java.lang.String" resultType="org.pine.mybatis.po.User">
select * from user t where t.username like '%${value}%'
</select> <!--
输入参数类型为User类型,是一个pojo
#{}表示占位符,输入参数名称为username,是pojo里面的一个属性
-->
<insert id="insertUser0" parameterType="org.pine.mybatis.po.User">
INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})
</insert> <insert id="insertUser1" parameterType="org.pine.mybatis.po.User">
INSERT INTO USER(id,username,birthday,sex,address) VALUES(default,#{username},#{birthday},#{sex},#{address})
</insert> <insert id="insertUser2" parameterType="org.pine.mybatis.po.User">
INSERT INTO USER(id,username,birthday,sex,address) VALUES(null,#{username},#{birthday},#{sex},#{address});
</insert>
</mapper>

MybatisFirst3.java如下:

package org.pine.mybatis.first;

import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.pine.mybatis.po.User; public class MybatisFirst3 {
@Test
public void testInsert0() throws Exception{
String fileName ="SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(fileName);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();//通过这种方式打开的SqlSession,autoCommit默认为false,需要手动提交事务 User user = new User();
user.setUsername("孙坤鹏");
user.setBirthday(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("1991-09-13").getTime()));
user.setSex("男");
user.setAddress("河南新乡"); sqlSession.insert("User.insertUser2", user);
sqlSession.commit(); sqlSession.close();
inputStream.close();
} @Test
public void testInsert1() throws Exception{
String fileName ="SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(fileName);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);//通过这种方式打开的SqlSession,autoCommit为true,会自动提交事务 User user = new User();
user.setUsername("王起鹏");
user.setBirthday(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("1992-07-20").getTime()));
user.setSex("男");
user.setAddress("河南驻马店"); sqlSession.insert("User.insertUser2", user); sqlSession.close();
inputStream.close();
} @Test
public void testInsert2() throws Exception{ String fileName ="SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(fileName);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(false);//通过这种方式打开的SqlSession,autoCommit为false,需要手动提交事务 User user = new User();
user.setUsername("杨豆豆");
user.setBirthday(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("1989-06-19").getTime()));
user.setSex("男");
user.setAddress("河南驻马店"); sqlSession.insert("User.insertUser2", user);
sqlSession.commit(); sqlSession.close();
inputStream.close(); }
}
05-11 17:28