一、settings 标签
在Mybatis全局配置文件中通过 settings 标签控制Mybatis全局开关
如:
//在mybatis.xml中开启log4j
//必须保证导入log4j的jar包
//同时在src下有log4j.properties
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
二、parameterType属性
1、 在***Mapper.xml中 select 、 delete 等标签的parameterType可以控制参数类型。
2 、SqlSession 的 selectList()和 selectOne()的第二个参数和 selectMap()的第三个参数都表示方法的参数.
2.1、示例
People p = session.selectOne("a.b.selById",1);
System.out.println(p);
2.2、在Mapper.xml 中通过#{}获取参数
parameterType 控制参数类型
#{}获取参数内容
使用索引,从 0 开始 #{0}表示第一个参数
也可以使用#{param1}第一个参数
如果只有一个参数(基本数据类型或 String),mybatis对#{}里面内容没有要求只要写内容即可.
如果参数是对象#{属性名}
如果参数是 map 写成#{key}
<select id="selById" resultType="com.jax.pojo.People" parameterType="int">
select * from people where id=#{0}
</select>
3、 #{} 和 ${} 的区别
#{} 获取参数的内容支持 索引获取,param1获取指定位置参数,并且 SQL 使用?占位符。
字
符
串
拼
接
不
使
用
?
,
默
认
找
{} 字符串拼接不使用?,默认找
字符串拼接不使用?,默认找{内容}内容的 get/set 方法,如果写数字,就是一个数字。
4、 如果在 xml 文件中出现 “<” , “>” ,双引号 等特殊字符时可以使用XML 文件转义标签(xml)
<![CDATA[ 内容 ]]>
5、mybatis 中实现 mysql 分页写法
?不允许在关键字前后进行数学运算,需要在代码中计算完成后传递到 mapper.xml 中,即在 java 代码中计算结果。
//显示几个
int pageSize = 2;
//第几页
int pageNumber = 2;
//如果希望传递多个参数,可以使用对象或 map
Map<String,Object> map = new HashMap<>();
map.put("pageSize", pageSize);
map.put("pageStart", pageSize*(pageNumber-1));
List<People> p = session.selectList("a.b.page",map);
在mapper.xml中代码
<select id="page" resultType="com.jax.pojo.People" parameterType="map">
select * from people limit #{pageStart},#{pageSize}
</select>
三、typeAliases别名
1、系统内置别名;把类型全小写
2、给某个类起别名
alias=”自定义"
<typeAliases>
<typeAlias type="com.jax.pojo.People" alias="peo"/>
</typeAliases>
mapper.xml 中 peo 引用 People 类
<select id="page" resultType="peo" parameterType="map">
select * from people limit #{pageStart},#{pageSize}
</select>
3、直接给某个包下所有类起别名,别名为类名
mybatis.xml文件配置
<typeAliases>
<package name="com.jax.pojo" />
</typeAliases>
mapper.xml中通过类名引用
<select id="page" resultType="People" parameterType="map">
select * from people limit #{pageStart},#{pageSize}
</select>
四、Mybatis实现新增
1、在 mapper.xml 中提供标签,标签没有返回值类型
<insert id="ins" parameterType="People">
insert into people values(default,#{name},#{age})
</insert>
2、通过 session.insert()调用新增方法
```java
int index1 = session.insert("a.b.ins", p);
if(index1>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
五、Mybatis实现修改
1、在mapper.xml中提供标签
<update id="upd" parameterType="People">
update people set name = #{name} where id = #{id}
</update>
2、编写代码
People peo = new People();
peo.setId(3);
peo.setName("王五");
int index = session.update("a.b.upd", peo);
if(index>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
session.commit();
六、Mybatis实现删除
1、在mapper.xml 提供标签
<delete id="del" parameterType="int">
delete from people where id = #{0}
</delete>
2、编写代码
int del = session.delete("a.b.del",3);
if(del>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
session.commit();
七、Mybatis接口绑定方案及多参数传递
1、作用:实现创建一个接口后把mapper.xml由mybatis生成接口的实现类,通过调用接口对象就可以获取 mapper.xml 中编写的 sql语句。(后面 mybatis 和 spring 整合时使用的是这个方案。)
2、实现步骤:
创建一个接口
接口包名和接口名与 mapper.xml 中namespace相同
接口中方法名和 mapper.xml 标签的 id 属性相同
在 mybatis.xml 中使用进行扫描接口和 mapper.xml
3、代码实现步骤:
在 mybatis.xml 中下使用
<mappers>
<package name="com.jax.mapper"/>
</mappers>
在com.jax.mapper下新建接口
public interface LogMapper {
List<Log> selAll();
}
在 com.jax.mapper 新建一个 LogMapper.xml
namespace 必须和接口全限定路径(包名+类名)一致
id 值必须和接口中方法名相同
如果接口中方法为多个参数,可以省略 parameterType
<mapper namespace="com.jax.mapper.LogMapper">
<select id="selAll" resultType="log">
select * from log
</select>
</mapper>
4、多参数实现办法
在接口中声明方法
List<Log> selByAccInAccout(String accin,String accout);
在 mapper.xml 中添加
#{}中使用 0,1,2 或 param1,param2
<!-- 当多参数时,不需要写 parameterType -->
<select id="selByAccInAccout" resultType="log" >
select * from log where accin=#{0} and accout=#{1}
</select>
5、使用注解方式
在接口中声明方法
/**
* mybatis 把参数转换为 map 了,其中@Param("key") 参数内
容就是 map 的 value
* @param accin123
* @param accout3454235
* @return
*/
List<Log> selByAccInAccout(@Param("accin") String accin123,@Param("accout") String accout3454235);
在 mapper.xml 中添加
#{} 里面写@Param(“内容”)参数中内容
<!-- 当多参数时,不需要写 parameterType -->
<select id="selByAccInAccout" resultType="log" >
select * from log where accin=#{accin} and accout=#{accout}
</select>
八、动态SQL
动态SQL:根据不同的条件需要执行不同的 SQL 命令.称为动态 SQL
MyBatis 中动态 SQL 在 mapper.xml 中添加逻辑判断等.
1、if 标签
<select id="selByAccinAccout" resultType="log">
select * from log where 1=1
<!-- OGNL 表达式,直接写 key 或对象的属性.不需要添加任何特字符号 -->
<if test="accin!=null and accin!=''">
and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
and accout=#{accout}
</if>
</select>
2、where 标签
当编写 where 标签时,如果内容中第一个是 and 去掉第一个and
如果 where 中有内容会生成 where 关键字,如果没有内容不生成 where 关键
使用示例
比直接使用 if 少写 where 1=1
<select id="selByAccinAccout" resultType="log">
select * from log
<where>
<if test="accin!=null and accin!=''">
and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
and accout=#{accout}
</if>
</where>
</select>
3、choose、when、otherwise 标签
只要有一个成立,其他都不执行.
代码示例
如果 accin 和 accout 都不是 null 或不是””生成的 sql 中只有 where accin=?
<select id="selByAccinAccout" resultType="log">
select * from log
<where>
<choose>
<when test="accin!=null and accin!=''">
and accin=#{accin}
</when>
<when test="accout!=null and accout!=''">
and accout=#{accout}
</when>
</choose>
</where>
</select>
4、 set 标签用在修改 SQL 中 set 从句
作用:去掉最后一个逗号,如果 set 里面有内容就生成 set 关键字,没有就不生成
示例
id=#{id} 目的防止 set 中没有内容,mybatis 不生成 set 关键字,如果修改中没有 set 从句 SQL 语法错误
<update id="upd" parameterType="log" >
update log
<set>
id=#{id},
<if test="accIn!=null and accIn!=''">
accin=#{accIn},
</if>
<if test="accOut!=null and accOut!=''">
accout=#{accOut},
</if>
</set>
where id=#{id}
</update>
5、trim 标签
标签属性:
prefix:在前面添加内容
prefixOverrides:去掉前面内容
suffix:在后面添加内容
suffixOverrieds:去掉后面内容
执行顺序去掉内容后添加内容
代码示例
<update id="upd" parameterType="log">
update log
<trim prefix="set" suffixOverrides=",">
a=a,
</trim>
where id=100
</update>
6、bind 标签
作用:给参数重新赋值
场景:模糊查询、在原内容前或后添加内容
示例:
<select id="selByLog" parameterType="log"
resultType="log">
<bind name="accin" value="'%'+accin+'%'"/>
#{money}
</select>
7、foreach 标签
作用:循环参数内容,还具备在内容的前后添加内容,还具备添加分隔符功能.
适用场景:in 查询中.批量新增中(mybatis 中 foreach 效率比较低)
如果希望批量新增,SQL 命令
insert into log VALUES
(default,1,2,3),(default,2,3,4),(default,3,4,5)
openSession()必须指定底层 JDBC 的 PreparedStatement.addBatch();
factory.openSession(ExecutorType.BATCH);
示例
属性说明:
collectino=””:要遍历的集合
item:迭代变量, #{迭代变量名}获取内容
open:循环后左侧添加的内容
close:循环后右侧添加的内容
separator:每次循环时,元素之间的分隔符
<select id="selIn" parameterType="list" resultType="log">
select * from log where id in
<foreach collection="list" item="abc" open="("close=")" separator=",">
#{abc}
</foreach>
</select>
8、sql和 include 标签
作用:某些 SQL 片段如果希望复用,可以使用 sql 定义这个片段,如属性名称。
<sql id="mysql">
id,accin,accout,money
</sql>
在 select 或 delete 或 update 或 insert 中使用 include 引用sql代码片段
<select id="">
select
<include refid="mysql"></include>
from log
</select>
九、缓存
1、应用程序和数据库交互的过程是一个相对比较耗时的过程
2、缓存存在的意义:让应用程序减少对数据库的访问,提升程序运行效率
3、MyBatis 中默认 SqlSession 缓存开启
同一个 SqlSession 对象调用同一个 select 时,只有第一次访问数据库,第一次之后把查询结果缓存到 SqlSession 缓存区(内存)中
缓存的是 statement 对象.(简单记忆必须是用一个 select )
在 myabtis 时一个 select 对应一个 statement 对象
有效范围必须是同一个 SqlSession 对象
4、缓存流程
步骤一: 先去缓存区中找是否存在 statement
步骤二:返回结果
步骤三:如果没有缓存 statement 对象,去数据库获取数据
步骤四:数据库返回查询结果
步骤五:把查询结果放到对应的缓存区中
5、SqlSessionFactory 缓存(又叫二级缓存)
有效范围:同一个 factory 内哪个 SqlSession 都可以获取。
当数据频繁被使用,很少被修改,使用二级缓存。
使用二级缓存步骤
在 mapper.xml 中添加
如果不写 readOnly=”true”需要把实体类序列化
<cache readOnly="true"></cache>
当 SqlSession 对象 close()时或 commit()时会把 SqlSession 缓存的数据刷(flush)到 SqlSessionFactory 缓存区中。