0x00:项目工程图
0x01:mybati.xml全局配置文件
<?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><!--全局配置文件-->
<properties resource="jdbc.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
<setting name="defaultStatementTimeout" value="3"></setting><!--3秒没响应就放弃,锁超时-->
<!--<setting name="localCacheScope" value="STATEMENT"/>--><!--全局取消一级缓存-->
</settings>
<typeAliases>
<package name="com.it.peach.model"></package><!--包别名-->
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/User.xml"/>
</mappers>
</configuration>
- <setting name="localCacheScope" value="STATEMENT"/>全局取消一级缓存
0x02:user.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="com.it.peach.dao.IUserDao"><!--namespace名称空间,必须唯一-->
<!--设置全局的二级缓存-->
<cache eviction="FIFO"
flushInterval="10000"
size="100"
readOnly="true"/>
<!--下面写增删改查sql语句-->
<!--增加-->
<insert id="insertUser" parameterType="User">
insert into user(name,age,sex) values (#{name},#{age},#{sex})
</insert>
<!--删除-->
<delete id="delUser" parameterType="int">
delete from user where nid=#{nid}
</delete>
<!--修改-->
<update id="updateUser" parameterType="User">
update user
<set>
<if test="name !=null">name=#{name},</if>
<if test="age !=null">age=#{age},</if>
<if test="sex !=null">sex=#{sex},</if>
</set>
where nid=#{nid}
</update>
<!--查询-->
/*flushCache="true" 局部取消一级缓存*/
<select flushCache="true" id="equrysUser" parameterType="User" resultType="User">
select nid,name,age,sex from user
<where>
<if test="name != null">and name like CONCAT('%',#{name},'%')</if>
<if test="age !=null">and age=#{age},</if>
<if test="sex !=null">and sex=#{sex},</if>
</where>
</select>
</mapper>
<cache eviction="FIFO"
flushInterval="10000"
size="100"
readOnly="true"/>
- readOnly设置为false会报错,序列化异常
- flushCache="true" 局部取消一级缓存
0x04:IUserDao
package com.it.peach.dao;
import com.it.peach.model.User;
import java.util.List;
/**
* 接口路径名必须与xml的namespace相同
* 方法名称必须与xml的id相同
* 方法的参数类型必须与xml中的parameType相同
* 方法的返回值必须和xml的resultType或者resultMap相同
* */
public interface IUserDao {
/*返回值 方法名(参数类型)*/
int insertUser(User user);
int delUser(int id);
int updateUser(User user);
List<User> equrysUser(User user);
}
- 接口路径名必须与xml的namespace相同
- 方法名称必须与xml的id相同
- 方法的参数类型必须与xml中的parameType相同
- 方法的返回值必须和xml的resultType或者resultMap相同
0x05:UserTest.java
public class UserTest {
SqlSessionFactory sqlSessionFactory = null;
@Before
public void init() throws IOException {
String resourse = "mybatis.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resourse);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
}
@Test
public void testAdd() {/*增加数据*/
try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
User user = new User(null, "王五2", 17, "女");
// getMapper会通过反射代理生成一个实现类
IUserDao userDao = session.getMapper(IUserDao.class);
int rows = userDao.insertUser(user);
System.out.println("受影响的行数"+rows);
session.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testDel() {/*删除数据*/
try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
IUserDao userDao = session.getMapper(IUserDao.class);
int rows = userDao.delUser(3);
System.out.println("删除书籍的行数"+rows);
session.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testUpdate() {/*修改数据*/
try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
User user = new User(2, "李四1", 16, "女");
IUserDao userDao = session.getMapper(IUserDao.class);
int rows = userDao.updateUser(user);
session.commit();
System.out.println("受影响的行数"+rows);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testFirstLevelCache() {/*一级缓存查询数据*/
try (SqlSession session = sqlSessionFactory.openSession()) {//打开一个链接
User user1 = new User(null, "李", null, null);
IUserDao userDao = session.getMapper(IUserDao.class);
List<User> list=userDao.equrysUser(user1);
list.forEach(us -> System.out.println(us));
System.out.println("分割线=================");
User user2 = new User(null, "李", null, null);
userDao = session.getMapper(IUserDao.class);
List<User> list1=userDao.equrysUser(user2);
list1.forEach(us -> System.out.println(us));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSecondLevelCache() {/*一级缓存查询数据*/
SqlSession session1 = sqlSessionFactory.openSession();//打开一个链接
User user1 = new User(null, "李", null, null);
IUserDao userDao = session1.getMapper(IUserDao.class);
List<User> list=userDao.equrysUser(user1);
list.forEach(us -> System.out.println(us));
session1.close();// 只有session关闭的时候会把数据同步刷新到二级缓存
System.out.println("分割线=================");
SqlSession session2 = sqlSessionFactory.openSession();
User user2 = new User(null, "李", null, null);
userDao = session2.getMapper(IUserDao.class);
List<User> list1=userDao.equrysUser(user2);
list1.forEach(us -> System.out.println(us));
}
}
- session1.close();// 只有session关闭的时候会把数据同步刷新到二级缓存
0x06:mybatis有关缓存的详细说明
https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#cache
mybatis官网:
https://mybatis.org/mybatis-3/zh/index.html