通用mapper插件

1、首先添加依赖

<!-- 1. 集成通用 Mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<!-- 建议使用最新版本,最新版本请从项目首页查找 -->
<version>4.1.5</version>
</dependency>

 

2、对应的接口继承于Mapper父类,填写泛型
public interface AdminMapper extends Mapper<Admin>      

泛型里面写pojo实体类

 

3、编写对应的adminMapper.xml文件
创建一个空的对应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="cn.kgc.dao.AdminMapper">


</mapper>


4、最重要******
需要把spring的配置文件修改成通用mapper自带的扫描仪

<!-- 通用mapper的配置和mybatis的前缀不一样 这个是tk开头-->
<!-- 通用 Mapper的扫描 -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kgc.dao"/>
</bean>

 

配置完成之后就可以进行使用了

测试:

使用自带的selectAll方法:::

List<Admin> adminList = adminMapper.selectAll();

for (Admin admin : adminList) {
System.out.println(admin);
}


使用时的注意事项:

1、通用mapper有自带的基本增删改查方法,只需要简单的配置,只支持单表的查询

 

2、@id 主键类型必须为Integer包装类
要不然查询的数据不会显示


3、int count = adminMapper.selectCount(new Admin());
查询总记录数需要新建一个对象入参



 

posted @ 2019-07-15 09:05  不忆过去,不憧未来!  阅读(1130)  评论(0编辑  收藏  举报