大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
tkMapper插件的详细讲解
一、tkMapper简介
tkMapper就是一个MyBatis插件,是在MyBatis的基础上提供的开发工具,可以让开发变得简单,提高开发效率。
作用:
1、提供了针对单表通用的数据库操作方法
2、逆向工程(根据数据表生成实体类、dao接口、映射文件)
二、tkMapper整合
1、基于SpringBoot完成MyBatis的整合
2、整合tkMapper
2.1、添加tkMapper的依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2.2、修改启动类的@MapperScan注解的包为tk.mybatis.spring.annotation.MapperScan
package com.qfedu.tkmapperdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.qfedu.tkmapperdemo.dao")
public class TkmapperDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TkmapperDemoApplication.class, args);
}
}
三、tkMapper的使用
1、创建数据表
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`user_id` int(64) NOT NULL AUTO_INCREMENT COMMENT '主键id;用户id',
`username` varchar(255) NOT NULL COMMENT '用户名;用户名',
`password` varchar(64) NOT NULL COMMENT '密码;密码',
`nickname` varchar(255) DEFAULT NULL COMMENT '昵称;昵称',
`realname` varchar(90) DEFAULT NULL COMMENT '真实姓名;真实姓名',
`user_img` varchar(1024) NOT NULL COMMENT '头像;头像',
`user_mobile` varchar(255) DEFAULT NULL COMMENT '手机号;手机号',
`user_email` varchar(255) DEFAULT NULL COMMENT '邮箱地址;邮箱地址',
`user_sex` varchar(1) DEFAULT NULL COMMENT '性别;M(男) or F(女)',
`user_birth` datetime DEFAULT NULL COMMENT '生日;生日',
`user_regtime` datetime NOT NULL COMMENT '注册时间;创建时间',
`user_modtime` datetime NOT NULL COMMENT '更新时间;更新时间',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用户';
2、创建实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
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;
}
3、创建DAO接口
tkMapper已经完成了对单表的通用操作的封装,封装在Mapper接口和MySqlMapper接口;因此如果我们要完成对单表的操作,只需自定义DAO接口继承Mapper接口和MySqlMapper接口。
public interface UserDao extends Mapper<User>, MySqlMapper<User> {
}
4、测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void test(){
User user = new User();
user.setUsername("aaaa");
user.setPassword("1111");
user.setUserImg("img/default.png");
user.setUserRegtime(new Date());
user.setUserModtime(new Date());
int i = userDao.insert(user);
System.out.println(i);
}
}
四、tkMapper提供的常用方法
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest {
@Autowired
private CategoryDAO categoryDAO;
@Test
public void testInsert() {
Category category = new Category(0, "测试类别2", 1, 0, "0.2png", "haha", "aaa.jpg", "black");
//int i = categoryDAO.insert(category);
int i = categoryDAO.insertUseGeneratedKeys(category);
System.out.println(category.getCategoryId());
assertEquals(1, i);
}
@Test
public void testUpdate() {
Category category = new Category(47, "测试类别3", 1, 0, "0.3png", "haha3", "aaa.jpg", "black");
int i = categoryDAO.updateByPrimaryKey(category);
//根据自定义条件修改,Example example就是封装条件的
//categoryDAO.updateByExample(Example example);
assertEquals(1, i);
}
@Test
public void testDelete(){
int i = categoryDAO.deleteByPrimaryKey(47);
//根据条件删除
//categoryDAO.deleteByExample(Example example);
assertEquals(1,i);
}
@Test
public void testSelect1(){
//查询所有
List<Category> categories = categoryDAO.selectAll();
for(Category category : categories){
System.out.println(category);
}
}
@Test
public void testSelect2(){
//根据主键查询
Category category = categoryDAO.selectByPrimaryKey(46);
System.out.println(category);
}
@Test
public void testSelect3(){
//条件查询
//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);
}
}
@Test
public void testSelect4(){
//分页查询
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);
}
@Test
public void testSelect5(){
//带条件分页查询
//条件
Example example = new Example(Category.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("categoryLevel",1);
//分页
int pageNum = 2;
int pageSize = 6;
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);
}
}
五、如何使用tkMapper进行关联查询
所有的关联查询都可以通过多个单表操作实现
@Test
public void testSelect(){
//查询用户的同时查询订单
Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("username","zhangsan");
//根据用户名查询用户
//1.先根据用户名查询用户信息
List<User> users = userDao.selectByExample(example);
User user = users.get(0);
//2.再根据用户id到订单表查询订单
Example example1 = new Example(Orders.class);
Example.Criteria criteria1 = example1.createCriteria();
criteria1.andEqualTo("userId",user.getUserId());
List<Orders> ordersList = orderDAO.selectByExample(example1);
//3.将查询到订单集合设置到user
user.setOrdersList(ordersList);
System.out.println(user);
}
也可自定义连接查询,在DAO接口中自定义方法,创建Mapper文件
六、逆向工程
逆向工程,根据创建好的数据表,生成实体类、DAO、映射文件
1、添加逆向工程依赖
此依赖是一个mybatis的maven插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin>
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 name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<!--配置 GeneralDAO-->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.qfedu.tkmapperdemo.general.GeneralDAO"/>
</plugin>
<!--配置数据库连接-->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/fmmall"
userId="root"
password="mysql">
</jdbcConnection>
<!--配置实体类存放路径-->
<javaModelGenerator targetPackage="com.qfedu.tkmapperdemo.beans" targetProject="src/main/java"/>
<!--配置 XML 存放路径-->
<sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mappers"/>
<!--配置 DAO 存放路径-->
<javaClientGenerator targetPackage="com.qfedu.tkmapperdemo.dao" targetProject="src/main/java" type="XMLMAPPER"/>
<table tableName="%">
<!--mysql 配置-->
<!--<generatedKey column="id" sqlStatement="Mysql" identity="true"/>-->
<!--oracle 配置-->
<!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
</table>
</context>
</generatorConfiguration>
3、将配置文件设置到逆向工程的maven插件
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/185003.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...