创建数据库:
CREATE DATABASE `mp` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
创建表和测试数据:
-- 创建测试表
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_name` varchar(20) NOT NULL COMMENT '用户名',
`password` varchar(20) NOT NULL COMMENT '密码',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
--插入测试数据
insert into `tb_user`(`id`,`user_name`,`password`,`name`,`age`,`email`,`birthday`) values
(1,'zhangsan','123456','张三',18,'test1@itcast.cn','2019‐09‐26 11:42:01'),
(2,'lisi','123456','李四',20,'test2@itcast.cn','2019‐10‐01 11:42:08'),
(3,'wangwu','123456','王五',28,'test3@itcast.cn','2019‐10‐02 11:42:14'),
(4,'zhaoliu','123456','赵六',21,'test4@itcast.cn','2019‐10‐05 11:42:18'),
(5,'sunqi','123456','孙七',24,'test5@itcast.cn','2019‐10‐14 11:42:23');
spring.application.name = xiaofeifie-mp-springboot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
# Logger Config
logging.level.root: debug
package com.xiaofeifei.mp.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
@TableId("ID")
private Long id;
@TableField("USER_NAME")
private String userName; //驼峰命名,则无需注解
@TableField("PASSWORD")
private String password;
@TableField("NAME")
private String name;
@TableField("AGE")
private Integer age;
@TableField("EMAIL")
private String email;
@TableField("BIRTHDAY")
private LocalDateTime birthday;
}
package com.xiaofeifei.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiaofeifei.mp.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
指定mybatis-plus的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--<plugins>-->
<!--<plugin
interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></plugin>-->
<!--</plugins>-->
</configuration>
如果我们需要一些自定义的方法,我们可以配置mapper的xml,默认xml的路径应该和接口的路径一致,只不过xml放在resource的目录下
<?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="com.xiaofeifei.mp.mapper.UserMapper">
<select id="findById" resultType="com.xiaofeifei.mp.pojo.User" parameterType="java.lang.Long">
select * from tb_user where id = #{id}
</select>
</mapper>
我们可以指定mapper的位置:
在application.properties文件中配置
还可以配置别名:
这样在mapper.xml中就可以不用配置具体的包路径
如果配置后出现:
Property configuration and configLocation can not specified with together
说明:
解决方法:我们直接在mybatis的config中配置
按照条件更新:
删除:
根据主键删除
根据条件删除
按照条件删除的另一种方式:
查询操作:
按照id查询
批量查询
按照条件查询:
查询出数量
分页查询:
首先奥
要配置bean
设置排序
mybatis-plus的代码生成器
代码生成器项目下载
链接: https://pan.baidu.com/s/1IAauZHEc1DXl-Q2ey2OSyg 提取码: u1yt
修改为自己的数据库
运行时:让输入模块名
然后会把数据库中的所有tb开头的表生成代码
也生成对应的xml文件
当我们输入的模块名不存在时,会将所有的表按照全名称进行生成
有的时候我我们service之间互相传递数据(例如微服务之间service之间传递数据,我们就需要一层DTO),我们就用我们的generator来生成DTO
我们要修改代码:
定义一个成员变量:
private static final Boolean IS_DTO = true;
在执行execute方法前加上
然后运行: