tkMapper的使用

基于MyBatis提供了很多第三方插件,这些插件通常可以完成数据操作方法的封装、数据库逆向工程工作(根据数据表生成实体类、生成映射文件)

  • myBatis-plus
  • tkMapper

tkMapper是一个MyBatis插件,是在MyBatis的基础上提供了很多的工具,让开发变简单,提高开发效率。

  • 提供了针对单表通用的数据库操作方法
  • 提供了逆向工程(根据数据表生成实体类、Dao、映射文件)
1. 整合tkMapper
1.1 添加tkMapper依赖
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>
1.2 修改启动类的@MapperScan注解的包为tk.mybatis.spring.annotation.MapperScan

在这里插入图片描述

2.使用tkMapper
2.1 创建数据表
CREATE TABLE `schools` (
  `s_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学校id',
  `s_name` varchar(100) NOT NULL COMMENT '学校名称',
  `s_loc_provice` varchar(100) NOT NULL DEFAULT '' COMMENT '学校所处省',
  `s_loc_city` varchar(100) DEFAULT '' COMMENT '学校所处市',
  `s_loc_district` varchar(100) DEFAULT '' COMMENT '学校所处区',
  `s_loc_specific` varchar(100) DEFAULT '' COMMENT '学校区下的具体位置',
  PRIMARY KEY (`s_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='学校基本信息表';
2.2 创建实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "schools") // 指定实体类对应的数据表的表名
public class School {

	@Id
    private Integer sId;
    private String sName;
    private String sLocProvice;
    private String sLocCity;
    private String sLocDistrict;
    private String sLocSpecific; // 注意:该命名方式对应数据表:s_loc_specific
}

如果数据表的命名方式和实体类不一致,则可以采取注解的方式来对应
比如:

public class School {

    @Column(name = "s_id")
    @Id
    private Integer sId;
}
2.3 创建Dao
import com.computerskills.competition.entity.School;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface SchoolDao extends Mapper<School>, MySqlMapper<School> {
}
2.4 测试
  • 右键:
    在这里插入图片描述
  • 选择test
    在这里插入图片描述
  • 选择Junit4
    在这里插入图片描述
  • 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
    <version>4.13.2</version>
</dependency>
  • 编写测试类
import com.computerskills.competition.CompetitionApplication;
import com.computerskills.competition.entity.School;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CompetitionApplication.class)
public class SchoolDaoTest {

    @Autowired
    private SchoolDao schoolDao;

    @Test
    public void test() {
        School school = new School();
        school.setSName("成都大学");
        school.setSLocProvice("四川省");
        school.setSLocCity("成都市");
        school.setSLocDistrict("成华区");
        school.setSLocSpecific("成洛大道2025号");
        int i = schoolDao.insert(school);
        System.out.println(i);
    }
}
3. tkMapper-条件查询
  • 基本查询
@Test
public void test() {
   // 条件查询
   // 1. 创建一个Example封装类别School查询条件
   Example example = new Example(School.class);
   // 2. 创建条件容器
   Example.Criteria criteria = example.createCriteria();
   // 3.选择sId == 1 或者 sId == 2的数据 ,这里的sId对应实体类的字段
//        criteria.andEqualTo("sId",1);
//        criteria.orEqualTo("sId",2);
   // 4. 查询sName中含有`大学`字样的数据
   criteria.andLike("sName", "%大学");
   List<School> schoolList = schoolDao.selectByExample(example);
   for (School school: schoolList) {
       System.out.println(school);
   }

   // 查询记录总条数
   System.out.println(schoolDao.selectCount(new School()));
   // 查询记录总条数(满足条件)
   System.out.println(schoolDao.selectCountByExample(example));
  • 分页查询
@Test
public void test2() {
    // 基本的分页查询
    int pageNum = 2;
    int pageSize = 10;
    int start = (pageNum-1)*pageSize;
    // 构造分页信息
    RowBounds rowBounds = new RowBounds(start, pageSize);
    List<School> schoolList = schoolDao.selectByRowBounds(new School(),rowBounds);
    for (School school: schoolList) {
        System.out.println(school);
    }
}
@Test
public void test3() {
    // 带有条件的分页查询
    Example example = new Example(School.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("sId",1);

    int pageNum = 1;
    int pageSize = 10;
    int start = (pageNum-1)*pageSize;
    RowBounds rowBounds = new RowBounds(start, pageSize);
    List<School> schoolList = schoolDao.selectByExampleAndRowBounds(example,rowBounds);

    for (School school: schoolList) {
        System.out.println(school);
    }
}
4.逆向工程
4.1 添加逆向工程依赖

mybatis的一个maven依赖

 <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin -->
 <plugin>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-maven-plugin</artifactId>
     <version>1.3.5</version>

     <dependencies>
         <!--mysql-->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>8.0.27</version>
         </dependency>
         <!--mapper-->
         <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
         <dependency>
             <groupId>tk.mybatis</groupId>
             <artifactId>mapper</artifactId>
             <version>4.1.5</version>
         </dependency>
     </dependencies>
 </plugin>
4.2 配置
  • resources/generator目录下创建generatorConfig.xml
    内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
<!--    引入数据库连接配置-->
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value=""></property>
        <property name="endimgDelimiter" value=""></property>
<!--    配置GeneralDao-->
    <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
        <property name="mappers" value="com.computerskills.competition.general.GeneralDao"></property>
    </plugin>

<!--    配置数据库连接-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/used"
                        userId="root"
                        password="123456">
        </jdbcConnection>

<!--    配置实体类存放路径-->
    <javaModelGenerator targetPackage="com.computerskills.competition.entity" targetProject="src/main/java"/>

<!--    配置XML 存放路径-->
    <sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mappers"/>

<!--    配置 DAO 存放路径-->
    <javaClientGenerator targetPackage="com.computerskills.competition.dao" targetProject="src/main/java" type="XMLMAPPER"/>

<!--    配置需要指定生成的数据库和表 %代表所有表-->
<!--    <table tableName="%">-->
        <!-- mysql 配置  -->
<!--        <generatedKey column="id" sqlStatement="Mysql" identity="true"/>-->
<!--    </table>-->
    <!-- 生成指定表   -->
    <table tableName="goods">
            <!--   mysql 配置     -->
        <generatedKey column="g_id" sqlStatement="Mysql" identity="true"></generatedKey>
    </table>
    </context>
</generatorConfiguration>
  • 将配置文件设置到逆向工程的maven插件中
 <!--     配置资源文件路径    -->
 <configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
 </configuration>

在这里插入图片描述

4.3 运行

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值