JavaEE学习日志持续更新----> 必看!JavaEE学习路线(文章总汇)
JavaEE学习日志(八十三)
MyBatis
数据源配置
获取连接的时间:等到真正要执行sql语句时,才会获得一个连接。
environments属性:
<!--数据库的环境
default:指定默认的环境
-->
<environments default="development">
<!--id:环境唯一的标志-->
<environment id="development">
<!--事务管理:jdbc事务-->
<transactionManager type="JDBC"/>
<!--
dataSource:数据源(数据库连接池)配置
type="POOLED":数据源的类型配置
POOLED:使用mybatis的自带数据源配置
UNPOOLED:不使用数据源配置,使用Connection操作数据库
JNDI:JNDI服务数据源配置
-->
<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>
</environment>
</environments>
事务管理
默认为事务开启
SqlSession sqlSession = sqlSessionFactory.openSession();
想要将事务关闭,传递true
SqlSession sqlSession = sqlSessionFactory.openSession(true);
注意:事务在结束时,必须把事务关闭(mybatis中自动关闭事务)
@Test
public void testSave(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//获取Dao接口的动态代理对象
UserDao dao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setUsername("大大明");
user.setPassword("123");
user.setAddress("济南");
user.setSex("女");
user.setBirthday("1999-2-22");
dao.save(user);
sqlSession.close();
}
动态sql语句
静态sql语句的缺陷
若条件中只给出一个条件,则会报错。
<select id="findByCondition" resultType="user" parameterType="user">
select * from user where username like "%"#{username}"%" and sex = #{sex}
</select>
public class TestFrame {
@Test
public void test(){
//获取sqlsession对象
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("mybatis-config.xml")).openSession();
//获取动态代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//执行方法
User user = new User();
user.setUsername("明");
List<User> userList = userDao.findByCondition(user);
for (User u : userList) {
System.out.println(u);
}
//关闭资源
sqlSession.close();
}
}
if和where标签
解决方式一:使用<if>
标签处理
<!--多条件查询-->
<select id="findByCondition" resultType="user" parameterType="user">
select * from user where 1=1
<if test="username != null">
and username like "%"#{username}"%"
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</select>
解决方式二:使用<where>
标签处理
注意:前面也需要加上and
<!--多条件查询
where:可以处理第一个and,后面的and不能处理
所以多条件都要写上and关键字
-->
<select id="findByCondition" resultType="user" parameterType="user">
select * from user
<where>
<if test="username != null">
and username like "%"#{username}"%"
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</where>
</select>
sql片段的重复使用
重复使用sql语句:使用<sql>
和<include>
标签
<sql id="select_user">
select * from user
</sql>
<select id="findByCondition" resultType="user" parameterType="user">
/*关联使用sql片段*/
<include refid="select_user"></include>
<where>
<if test="username != null">
and username like "%"#{username}"%"
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</where>
</select>
foreach标签
循环标签<foreach>
- collection:list或array
- open:前缀
- close:后缀
- separator:分隔符
- item:循环的每一个对象
- index:循环索引
原始的sql语句
delete from user where id in (7,9)
foreach执行sql
<delete id="delByArray" parameterType="int[]">
delete from user where
<foreach collection="array" open="id in (" close=")" separator="," item="" index="id">
#{id}
</foreach>
</delete>