导包
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
构建SqlSessionFactory
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/exam?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/GameMapper.xml"/>
</mappers>
</configuration>
Mapper和Mapper.xml
<?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="dao.GameMapper">
<select id="getGames" resultType="pojo.Game">
select * from game where id=#{id};
</select>
<insert id="insertGames" parameterType="pojo.Game" >
insert into exam.game (id,name) VALUES (#{id},#{name});
</insert>
<update id="updateUser" parameterType="pojo.Game">
update exam.game set name=#{name} where id=#{id}
</update>
<delete id="deleteGames">
delete from game where id=#{id}
</delete>
</mapper>
public interface GameMapper {
//查询
public List<Game> getGames(int id);
//插入
public void insertGames(Game game);
//删除
public void deleteGames(int id);
//修改
public void updateUser(Game game);
}
命名空间
指定mapper的namespace作用有两个,一是实现接口的绑定,二是利用更长的全限定名来将不同的语句隔离开来。
注入
如果使用#{}进行占位处理,mybati会进行预编译,这样注入问题一般不需要考虑。
如果用${}进行拼接处理,需要考虑注入的情况
缓存
默认开启一级缓存,一级缓存是针对每个session的缓存。
在mapper的配置文档中加入cache标签开启二级缓存,二级缓存是针对这个mapper的。
缓存的溢出移除算法默认是LRU,可以选择更改为FIFO或者weak和soft(基于GC)的缓存算法,指定cache标签的eviction属性即可,同时,可以指定缓存的刷新间隔(flushInterval)和size等等属性。
当进行了增删改操作后缓存会被清除。
动态SQL
感觉有点鸡肋
用法是向CRUD标签中插入标签实现根据传入参数动态构建SQL的功能。
if
if会将语句拼接在当前所在位置,举个例子
public List<Game> dSqlQuery(String name);
<select id="dSqlQuery" resultType="pojo.Game">
select *
from game
where 1 = 2
<if test="name!=null">
or name like #{name}
</if>
</select>
choose、when、otherwise
类似与switch语句。
trim、where、set
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
用于动态更新语句的类似解决方案叫做 set。set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。
可以使用trim属性来自定义实现where和set的功能。
foreach
用于in查询中,对传入的集合进行遍历。
这个标签属性有点多,简单总结一下。
1、collection表示如何来得到这个集合,如果传入的直接为一个List,那么collection值就为list,如果直接传入的为一个array不可变数组,那么collection值就为array,如果传入的为一个dto,比如dto里面的array变量名为idLists,那么collection的值就为idLists。
2、item表示集合中每一个元素进行迭代时的别名,比如item为value,那么,每次获取的都使用#{value}即可。
3、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,一般很少使用这个。
4、open表示该语句以什么开始。
5、separator表示在每次进行迭代之间以什么符号作为分隔符。
6、close表示以什么结束。
实现一下
public List<Game> forEach(List list);
<select id="forEach" resultType="pojo.Game">
select *
from game
where id in
<foreach collection="list" item="id" open="(" separator="," close=")">#{id}
</foreach>
</select>