今天学习了下mybatis,
对其中的autoCommit自动提交事务比较好奇,
研究了下,把配置和代码都放上
mapper.xml如下:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- namespace隔离sql --> 6 <mapper namespace="User"> 7 <!-- id:statement的标识 --> 8 <!-- 9 #{}表示占位符(转为?),可以防止SQL注入;如果输入参数类型是简单类型,输入参数名称可以是value或者其它任意名称 10 --> 11 <select id="getUserById" parameterType="int" resultType="org.pine.mybatis.po.User"> 12 select * from user t where t.id = #{id} 13 </select> 14 15 <!-- resultType:单条记录的类型 --> 16 <select id="listUserByUserame1" parameterType="java.lang.String" resultType="org.pine.mybatis.po.User"> 17 select * from user t where t.username like #{username} 18 </select> 19 20 <!-- 21 ${}表示拼接SQL(拼接参数),不能防止SQL注入;如果输入参数类型是简单类型,输入参数名称只能是value 22 --> 23 <select id="listUserByUsername2" parameterType="java.lang.String" resultType="org.pine.mybatis.po.User"> 24 select * from user t where t.username like '%${value}%' 25 </select> 26 27 <!-- 28 输入参数类型为User类型,是一个pojo 29 #{}表示占位符,输入参数名称为username,是pojo里面的一个属性 30 --> 31 <insert id="insertUser0" parameterType="org.pine.mybatis.po.User"> 32 INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address}) 33 </insert> 34 35 <insert id="insertUser1" parameterType="org.pine.mybatis.po.User"> 36 INSERT INTO USER(id,username,birthday,sex,address) VALUES(default,#{username},#{birthday},#{sex},#{address}) 37 </insert> 38 39 <insert id="insertUser2" parameterType="org.pine.mybatis.po.User"> 40 INSERT INTO USER(id,username,birthday,sex,address) VALUES(null,#{username},#{birthday},#{sex},#{address}); 41 </insert> 42 </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(); } }