准备工作
首先新建一张表,用于记录读过的书,有几个简单的字段:
CREATE TABLE `reading_list` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`book_id` bigint(20) NOT NULL COMMENT '书籍ID',
`start_date` varchar(16) NOT NULL COMMENT '开始日期',
`end_date` varchar(16) NOT NULL COMMENT '结束日期',
`deleted` tinyint(4) DEFAULT '0' COMMENT '是否删除',
`gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
`gmt_update` bigint(20) NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_gmt_create` (`gmt_create`),
KEY `idx_gmt_update` (`gmt_update`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='阅读记录';
mybatis配置
接下来写mybatis的配置文件,可以直接从官网上copy:
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias type="org.xinghe.xh.entity.ReadingListEntity" alias="ReadingListEntity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/muggle_java"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/ReadingListMapper.xml"/>
</mappers>
</configuration>
这里配置了几个节点:
- settings:节点是调整设置,影响mybatis的运行时行为
- typeAliases:类型别名设置
- environments:mybatis运行环境设置
- mappers:mybatis映射器配置,执行的SQL会在这里找
代码编写
接口类
public interface ReadingListDao {
int insert(ReadingListEntity readingListEntity);
List<ReadingListEntity> getList();
ReadingListEntity getById(Long id);
}
对应的mapper文件
<?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="org.xinghe.xh.dao.ReadingListDao">
<insert id="insert">
insert into reading_list(book_id,start_date,end_date,deleted,gmt_create,gmt_update)
values(#{bookId},#{startDate},#{endDate},0,#{gmtCreate},#{gmtUpdate})
</insert>
<select id="getList" resultType="org.xinghe.xh.entity.ReadingListEntity">
select * from reading_list where deleted = 0
</select>
<select id="getById" resultType="org.xinghe.xh.entity.ReadingListEntity">
select * from reading_list where deleted = 0 and id = #{id}
</select>
</mapper>
测试
@TestInstance(Lifecycle.PER_CLASS)
class ReadingListDaoTest {
private ReadingListDao readingListDao;
@BeforeAll
void before() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = factory.openSession();
readingListDao = sqlSession.getMapper(ReadingListDao.class);
}
@Test
void getList() {
List<ReadingListEntity> list = readingListDao.getList();
System.out.println("查询结果:" + list);
}
}
运行结果:
完美通过。
mybatis的使用非常简单,基本上官网上都说完了。如果要使用缓存,也只需要在mapper文件添加一行代码:
<?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="org.xinghe.xh.dao.ReadingListDao">
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
</mapper>
对应的测试:
@Test
void getById() {
ReadingListEntity entity = readingListDao.getById(4L);
System.out.println("第一次查询" + entity.toString());
ReadingListEntity second = readingListDao.getById(4L);
System.out.println("第二次查询" + second.toString());
}
运行结果:
日志上可以看出,第一次查询未命中缓存,所以查询了数据库;第二次查询命中缓存,直接返回结果。
总结
总的来说,mybatis的使用很简单,参照官网很快就能上手。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。