tkmapper教程_trello怎么用

2022-11-08 21:36:53 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

tkMapper

  • tkMapper整合
    • 整合mybatis
      • 依赖
      • 配置类
      • 启动类扫描
    • 整合tkMapper
      • 依赖
      • 修改启动类的`@MapperScan`注解的包
  • tkMapper使用
    • 创建数据表
    • 创建实体类
    • 创建Mapper接口
    • 测试
  • tkMapper的常用方法
    • 增删改
      • 添加
      • 修改
      • 删除
    • 查询
      • 查询所有
      • 根据主键查询
      • 条件查询
      • 分页查询
      • 带条件分页查询
    • 关联查询
  • 逆向工程
    • 添加逆向工程依赖

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

  • tkMapper就是一个MyBatis插件,是在MyBatis的基础上提供了很多工具,让开发变得简单,提高开发效率。
  • 提供了针对单表通用的数据库操作方法
  • 逆向工程(根据数据表生成实体类、dao接口、映射文件)

tkMapper整合

整合mybatis

依赖

代码语言:javascript复制
		<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>

配置类

代码语言:javascript复制
server:
port: 8080
servlet:
context-path: /
#连接数据库的信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/fmmall?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
mybatis:
# 扫描映射文件
mapper-locations: classpath:mapper/*.xml
# 配置别名扫描的包
type-aliases-package: com.blb.entity
configuration:
# 开启驼峰映射配置
map-underscore-to-camel-case: true

启动类扫描

代码语言:javascript复制
@SpringBootApplication
@MapperScan("com.blb.mapper")
public class TkmapperApplication { 

public static void main(String[] args) { 

SpringApplication.run(TkmapperApplication.class, args);
}
}

整合tkMapper

依赖

代码语言:javascript复制
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>

修改启动类的@MapperScan注解的包

  • tk.mybatis.spring.annotation.MapperScan
代码语言:javascript复制
package com.blb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.blb.mapper")
public class TkmapperApplication { 

public static void main(String[] args) { 

SpringApplication.run(TkmapperApplication.class, args);
}
}

tkMapper使用

创建数据表

代码语言:javascript复制
CREATE TABLE `users`  (
`user_id` int(64) NOT NULL AUTO_INCREMENT COMMENT '主键id 用户id',
`username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名 用户名',
`password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码 密码',
`nickname` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称 昵称',
`realname` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '真实姓名 真实姓名',
`user_img` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '头像 头像',
`user_mobile` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号 手机号',
`user_email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱地址 邮箱地址',
`user_sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别 M(男) or F(女)',
`user_birth` date NULL DEFAULT NULL COMMENT '生日 生日',
`user_regtime` datetime(0) NOT NULL COMMENT '注册时间 创建时间',
`user_modtime` datetime(0) NOT NULL COMMENT '更新时间 更新时间',
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户 ' ROW_FORMAT = Compact;

创建实体类

代码语言:javascript复制
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name="users")// 表名和数据库名不一致无法映射
public class User { 

private int userId;
private String username;
private String password;
private String nickname;
private String realname;
private String userImg;
private String userMobile;
private String userEmail;
private String userSex;
private Date userBirth;
private Date userRegtime;
private Date userModtime;
}

创建Mapper接口

tkMapper已经完成了对单表的通用操作的封装,封装在Mapper接口和MySqlMapper接口;因此如果我们要完成对单表的操作,只需自定义Mapper接口继承Mapper接口和MySqlMapper接口

代码语言:javascript复制
package com.blb.mapper;
import com.blb.entity.User;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface UserMapper extends Mapper<User>, MySqlMapper<User> { 

}

测试

代码语言:javascript复制
@SpringBootTest
class TkmapperApplicationTests { 

@Autowired
private UserMapper userMapper;
@Test
void testInsert() { 

User user=new User();
user.setUsername("aaaa");
user.setPassword("1111");
user.setUserImg("img/default.png");
user.setUserRegtime(new Date());
user.setUserModtime(new Date());
int insert = userMapper.insert(user);
System.out.println(insert);
}
}

tkMapper的常用方法

代码语言:javascript复制
package com.blb.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Id;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Category { 

@Id
private Integer categoryId;
private String categoryName;
private Integer categoryLevel;
private Integer parentId;
private String categoryIcon;
private String categorySlogan;
private String categoryPic;
private String categoryBgColor;
}
代码语言:javascript复制
package com.blb.mapper;
import com.blb.entity.Category;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface CategoryMapper extends Mapper<Category>, MySqlMapper<Category> { 

}

增删改

添加

代码语言:javascript复制
Category category = new Category(0,"测试类别3",1,0,"03.png","xixi","aaa.jpg","black");
//int i = categoryMapper.insert(category);
int i = categoryMapper.insertUseGeneratedKeys(category);
System.out.println(category.getCategoryId());
assertEquals(1,i);

注意如果要主键回填一定要在对应的实体类的主键上添加@Id

修改

代码语言:javascript复制
Category category = new Category(48,"测试类别4",1,0,"04.png","heihei","aaa.jpg","black");
int i = categoryDAO.updateByPrimaryKey(category);
// 根据自定义条件修改,Example example就是封装条件的
// int i1 = categoryDAO.updateByExample( Example example);
assertEquals(1,i);

删除

代码语言:javascript复制
  int i = categoryDAO.deleteByPrimaryKey(48);
// 根据条件删除
//int i1 = categoryDAO.deleteByExample(Example example);
assertEquals(1,i);

查询

查询所有

代码语言:javascript复制
 //查询所有
List<Category> categories = categoryDAO.selectAll();
for (Category category: categories) { 

System.out.println(category);
}

根据主键查询

代码语言:javascript复制
//根据主键查询
Category category = categoryDAO.selectByPrimaryKey(47);
System.out.println(category);

条件查询

代码语言:javascript复制
//条件查询
//1.创建一个Example封装 类别Category查询条件
Example example = new Example(Category.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("categoryLevel",1);
criteria.orLike("categoryName","%干%");
List<Category> categories = categoryDAO.selectByExample(example);
for (Category category: categories) { 

System.out.println(category);
}

分页查询

代码语言:javascript复制
//分页查询
int pageNum = 2;
int pageSize = 10;
int start = (pageNum-1)*pageSize;
RowBounds rowBounds = new RowBounds(start,pageSize);
List<Category> categories = categoryDAO.selectByRowBounds(new Category(), rowBounds);
for (Category category: categories) { 

System.out.println(category);
}
//查询总记录数
int i = categoryDAO.selectCount(new Category());
System.out.println(i);

带条件分页查询

代码语言:javascript复制
//带条件分页
//条件
Example example = new Example(Category.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("categoryLevel",1);
//分页
int pageNum = 2;
int pageSize = 3;
int start = (pageNum-1)*pageSize;
RowBounds rowBounds = new RowBounds(start,pageSize);
List<Category> categories = categoryDAO.selectByExampleAndRowBounds(example,rowBounds);
for (Category category: categories) { 

System.out.println(category);
}
//查询总记录数(满足条件)
int i = categoryDAO.selectCountByExample(example);
System.out.println(i);

关联查询

就想原来一样在接口里写方法,写mybatis的xml即可

逆向工程

逆向工程,根据创建好的数据表,生成实体类、DAO、映射文件、

添加逆向工程依赖

代码语言:javascript复制
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
代码语言:javascript复制
<?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.blb.general.GeneralMapper"/>
</plugin>
<!-- 配置数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/fmmall?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF-8" userId="root" password="123456">
</jdbcConnection>
<!-- 配置实体类存放路径 -->
<javaModelGenerator targetPackage="com.blb.entity" targetProject="src/main/java"/>
<!-- 配置 XML 存放路径 -->
<sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mapper"/>
<!-- 配置 DAO 存放路径 -->
<javaClientGenerator targetPackage="com.blb.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
<!-- 配置需要指定生成的数据库和表,% 代表所有表 -->
<table tableName="%">
<!-- mysql 配置 -->
<!-- <generatedKey column="id" sqlStatement="Mysql" identity="true"/>-->
</table>
<!-- <table tableName="tb_roles">-->
<!-- &lt;!&ndash; mysql 配置 &ndash;&gt;-->
<!-- <generatedKey column="roleid" sqlStatement="Mysql" identity="true"/>-->
<!-- </table>-->
<!-- <table tableName="tb_permissions">-->
<!-- &lt;!&ndash; mysql 配置 &ndash;&gt;-->
<!-- <generatedKey column="perid" sqlStatement="Mysql" identity="true"/>-->
<!-- </table>-->
</context>
</generatorConfiguration>
代码语言:javascript复制
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184975.html原文链接:https://javaforall.cn

0 人点赞