Mybatis的标签使用和动态SQL
1.配置别名:
<typeAliases>
<!--用于配置别名-->
<typeAlias type="com.xmj.domain.User" alias="user"></typeAlias>
<!--该包下的实体类对会被注册别名,不在区分大小写-->
<package name="com.xmj.domain"/>
</typeAliases>
在使用了typeAlias配置了别名以后,别不用再指定全类名,可以直接使用别名,type属性是要配置别名的全限定类名,alias属性是指定的别名。使用package属性可以给包下的所有实体类都配置别名,使其不区分大小写。
在Mybatis中使用int,Integer,lang.Integer都没有区别的原因就是mybatis自动配置了别名
2.引入外部文件
<properties resource="jdbcConfig.properties"></properties>
使用该标签可以从外部的文件中映入jdbc基本配置信息
jdbcConfig.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/eesy?serverTimezone=UTC
username=root
password=root
(在该文件下定义配置信息)
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
(在mybatis配置文件下引用)
3.mybatis的连接池
mybatis的连接池分为三种:
UNPOOLED 不使用连接池的数据源
POOLED 使用连接池的数据源
JNDI 使用 JNDI 实现的数据源
在dateSource的type属性下修改,在使用连接池时,真正连接打开的时间点,只是在我们执行SQL语句时,才会进行。数据库连接是我们最为宝贵的资源,只有在要用到的时候,才去获取并打开连接,当我们用完了就再 立即将数据库连接归还到连接池中。
4.mybatis事务的自动提交
在源码中找到DefaultSqlSessionFactory类下openSession()方法,发现有传入参数为boolean类型的同名方法,在创建SqlSession对象时,使用session = factory.openSession(true); 便可实现自动提交。不推荐使用自动提交
5.动态SQL
1)if标签
<select id="findByCondtion" resultType="com.xmj.domain.User" parameterType="com.xmj.domain.User">
select * from user where 1=1
<if test="userName!=null">
and username=#{userName}
</if>
<if test="sex!=null"
and sex=#{sex}
</if>
</select>
用于指定条件查询,test属性下的时java语法的语句,可以与where标签合用
2)where标签
<select id="findByCondtion" resultType="com.xmj.domain.User" parameterType="com.xmj.domain.User">
select * from user
<where>
<if test="userName!=null">
and username=#{userName}
</if>
<if test="sex!=null">
and sex=#{sex}
</if>
</where>
</select>
两种方法效果相同,但使用where标签更好
3.foreach标签
<!-- 查询所有用户在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="queryvo">
<!-- select * from user where id in (1,2,3,4,5); -->
select* from user
<where>
<if test="ids != null and ids.size() > 0">
<foreach collection="ids" open="id in ( " close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
SQL 语句: select 字段 from user where id in (?)
标签用于遍历集合,它的属性: collection:代表要遍历的集合元素,注意编写时不要写#{} open:代表语句的开始部分 close:代表结束部分
6.抽取SQL语句
<!--抽取SQL语句-->
<sql id="defaultSql">
select * from user
</sql>
在使用时,只需引入即可
<sql id="defaultSql">
<include refid="defaultSql"></include>
</sql>
7.mybatis的一对一和多对一映射
主要思想是使用resultMap标签来实现对象的映射
<!--定义封装User和Account-->
<resultMap id="accountuser" type="com.xmj.domain.Account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!--一对一关系的映射-->
<association property="user" column="uid" javaType="com.xmj.domain.User">
<id property="id" column="id"></id>
<result property="userName" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
</association>
</resultMap>
<select id="findAll" resultMap="accountuser"> <!--id的值必须和对应打方法名一致-->
select u.*,a.id as aid,a.uid as uid,a.money from user u,account a where a.uid=u.id
</select>
在association标签中可以指定一个对象,javaType属性是要将器封装到哪一个类中。
同理对一个对象的集合可以用
<resultMap id="useraccount" type="com.xmj.domain.User">
<id property="id" column="id"></id>
<result property="userName" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="irthday"></result>
<collection property="accounts" ofType="com.xmj.domain.Account">
<id column="aid" property="id"></id>
<result column="uid" property="uid"></result>
<result column="money" property="money"></result>
</collection>
</resultMap>