Myabtis之Tkmapper逆向工程
tkmapper的作用和mybatis-plus的功能一样,都是提供了封装好的基础sql供我们使用,除此之外,也提供逆向工程来实现数据库字段向实体类的映射和mapper的自动创建
一、环境搭建
在这之前 完成mybatis的数据源配置
- 导入依赖
<!-- TkMapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
TkMapper的逆向工程不同于Mybatis-plus,Mybatis-plus是一个Java配置类和一个SpringBoot启动器,而TkMaper是一个插件加xml配置文件的形式
-
导入插件
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--这个地址是逆向工程xml的配置路径 在这里引入--> <configurationFile> ${basedir}/src/main/resources/Genertator/GeneratorConfig.xml </configurationFile> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.4</version> </dependency> </dependencies> </plugin> </plugins> </build>
-
编写通用Mapper接口
//编写接口 继承两个通用Mapper 在xml文件中引入 这样自动生成的文件会自动继承这个接口
public interface GenertatorMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
-
编写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> <!-- 引入数据库连接配置 --> <!-- <properties resource="jdbc.properties"/>--> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- 配置 GeneralDAO --> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="com.llf.GenertatorMapper.GenertatorMapper"/> </plugin> <!-- 配置数据库连接 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mydb2?serverTimezone=UTC" userId="root" password="123456"> </jdbcConnection> <!-- 配置实体类存放路径 --> <javaModelGenerator targetPackage="com.llf.Pojo" targetProject="src/main/java"/> <!-- 配置 XML 存放路径 Mapper是存放xml的包名--> <sqlMapGenerator targetPackage="/" targetProject="src/main/resources/Mapper"/> <!-- 配置 Mapper 存放路径 --> <javaClientGenerator targetPackage="com.llf.Mapper" targetProject="src/main/java" type="XMLMAPPER"/> <!-- 配置需要指定生成的数据库和表,% 代表所有表 --> <table tableName="student"></table> </context> </generatorConfiguration>
-
具体的包结构
一切完成后 点开右侧Maven—》Plugins—》mybatis-generator—》mybatis-generator:generator
双击运行即可
二、功能测试
基本的增删改查
System.out.println("======================增加========================");
Student student = new Student("1811062017", "王五", "男", 12, "CS");
int insert = studentMapper.insert(student);
if (insert!=0){
System.out.println("添加成功");
}
System.out.println("======================修改========================");
Student student1 = new Student("1811062017", "王五", "男", 13, "CS");
studentMapper.updateByPrimaryKey(student1);
if (student1.getSage()==13){
System.out.println("修改成功");
}
System.out.println("======================查询========================");
List<Student> studentList = studentMapper.selectAll();
for (Student student2 : studentList) {
System.out.println(student2);
}
System.out.println("查询成功");
System.out.println("======================删除========================");
int i = studentMapper.deleteByPrimaryKey(student1);
if (i!=0){
System.out.println("删除成功");
}
因为TkMapper的大多数方法虽然叫做xxxByPrimaryKey
但是它仍接受的是一个对象,因此我们要在实体类中显示的添加注解@id
来声明哪一个属性是主键id,但是如果是自动生成的实体类,他是会自动添加id注解
@AllArgsConstructor
@NoArgsConstructor
@Data
// 为了避免产生属性匹配失败的情况 应该将所有数据类型使用它的包装类来实现
public class Student {
@Id
private String sno;
private String sname;
private String sex;
private Integer sage;
private String sdept;
}
三、使用条件构造器完成条件查询
//条件查询 sdept为cs的人
Example example = new Example(Student.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("sdept","cs");
List<Student> students = studentMapper.selectByExample(example);
for (Student student : students) {
System.out.println(student);
}
//分页
Integer pageNum=2;
Integer pageSize=3;
//计算起始页
Integer start=(pageNum-1)*pageSize;
RowBounds rowBounds = new RowBounds(start,pageSize);
List<Student> studentList = studentMapper.selectByRowBounds(new Student(), rowBounds);
for (Student student : studentList) {
System.out.println(student);
}
//两个条件一起查询 即使cs 又是男生
Example example = new Example(Student.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("sdept","cs");
criteria.andEqualTo("sex","male");
List<Student> studentList = studentMapper.selectByExample(example);
for (Student student : studentList) {
System.out.println(student);
}