mybatis中的连接池以及事务控制
mybatis中连接池使用及分析
连接池:
我们在实际开发中都会使用连接池。因为它可以减少我们获取连接所消耗的时间。
mybatis中的连接池提供了3种方式的配置:
配置的位置:
主配置文件SqlMapConfig.xml中的dataSource标签,type属性就是表示采用何种连接池方式。
type属性的取值:
POOLED 采用传统的javax.sql.DataSource规范中的连接池,mybatis中有针对规范的实现
UNPOOLED 采用传统的获取连接的方式,虽然也实现Javax.sql.DataSource接口,但是并没有使用池的思想。
JNDI 采用服务器提供的JNDI技术实现,来获取DataSource对象,不同的服务器所能拿到DataSource是不一样。
注意:如果不是web或者maven的war工程,是不能使用的。tomcat服务器,采用连接池就是dbcp连接池。
数据源配置就是在
SqlMapConfig.xml
文件中,具体配置如下:
<!-- 配置数据源(连接池)信息 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
MyBatis
在初始化时,根据
<dataSource>
的
type
属性来创建相应类型的的数据源
DataSource
,即:
type=”POOLED”
:
MyBatis
会创建
PooledDataSource
实例
type=”UNPOOLED”
:
MyBatis
会创建
UnpooledDataSource
实例
type=”JNDI”
:
MyBatis
会从
JNDI
服务上查找
DataSource
实例,然后返回使用
mybatis中的事务
它是通过sqlsession对象的commit方法和rollback方法实现事务的提交和回滚
@Before // 测试方法之前执行
public void init() throws Exception{
// 1、读取配置文件,生成字节输入流
in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2、获取SQLSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3、获取SqlSessiond对象
sqlSession = factory.openSession();
// 4、获取dao得代理对象
userDao = sqlSession.getMapper(IUserDao.class);
}
@After // 测试方法之后执行
public void destroy() throws Exception{
// 提交事务
sqlSession.commit();
// 6、释放资源
sqlSession.close();
in.close();
}
Mybatis 的动态 SQL 语句 if标签和where标签
public interface IUserDao {
// 查询所有用户
List<User> findAll();
// 根据id查询用户信息
User findById(Integer userId);
// 根据名称模糊查询用户信息
List<User> findByName(String username);
// 根据queryVo条件查询用户
List<User> findUserByVo(QueryVo vo);
// 根据传入的参数条件查询 user就是查询的条件
List<User> findUserByCondition(User user);
}
<?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">
<mapper namespace="com.itcase.dao.IUserDao">
<!-- 配置 查询结果的列名和实体类的属性名的对应关系 -->
<resultMap id="userMap" type="user">
<!-- 主键字段的对应-->
<id property="userId" column="id"></id>
<!-- 非主键字段的对应-->
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
<!-- 配置查询所有-->
<select id="findAll" resultType="user">
select * from user;
</select>
<!-- 根据id查询用户 -->
<select id="findById" parameterType="INT" resultType="user">
select * from user where id=#{uid};
</select>
<!-- 根据名称模糊查询-->
<select id="findByName" parameterType="string" resultType="user">
select * from user where username like '%${value}%';
</select>
<!-- 根据queryVo条件查询-->
<select id="findUserByVo" parameterType="com.itcase.domain.QueryVo" resultType="user">
select * from user where username like #{user.username};
</select>
<!-- 根据条件查询 -->
<select id="findUserByCondition" resultType="user" parameterType="user">
select * from user where 1=1
<if test="username != null">
and username = #{username}
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</select>
</mapper>
<!-- 根据条件查询 -->
<select id="findUserByCondition" resultType="user" parameterType="user">
select * from user
<where>
<if test="username != null">
and username = #{username}