准备工作
创建数据库
创建数据库以及表
创建数据库并设置字符集为utf-8:
CREATE DATABASE `mp` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
创建表和测试数据:
-- -- 表的结构 `tb_user` -- CREATE TABLE IF NOT EXISTS `tb_user` ( `id` bigint(20) NOT NULL 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 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- -- 表中的数据 `tb_user` -- INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`, `birthday`) VALUES (1, 'zhangsan', '123456', '张三', 18, 'test1@yyl.cn', '2022-03-09 00:00:00'), (2, 'lisi', '123456', '李四', 20, 'test2@yyl.cn', '2022-03-09 00:00:00'), (3, 'wangwu', '123456', '王五', 28, 'test3@yyl.cn', '2022-03-09 00:00:00'), (4, 'zhaoliu', '123456', '赵六', 21, 'test4@yyl.cn', '2022-03-09 00:00:00'), (5, 'sunqi', '123456', '孙七', 24, 'test5@yyl.cn', '2022-03-09 00:00:00');
工程搭建
工程结构
创建工程
创建maven工程,分别填写GroupId ArtifactId和Version,如下:
<groupId>cn.yyl.mp</groupId> <artifactId>yyl-mp-springboot</artifactId> <version>1.0-SNAPSHOT</version>
导入依赖
导入maven依赖,由于本例采用Spring boot技术,使用mybatis-plus-boot-starter能与其便捷集成: 版本规划:
Spring boot: 2.1.3.RELEASE mybatis-plus: 3.1.0
pom文件如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--简化代码的工具包--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--mybatis-plus的springboot支持--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> </dependencies> </project>
编写application.properties
spring.application.name = yyl-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=root # Logger Config logging.level.root: debug
编写pojo
package cn.yyl.mp.pojo; import com.baomidou.mybatisplus.annotation.IdType; 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 lombok.ToString; import java.time.LocalDateTime; /** * @author Administrator * @version 1.0 **/ @Data @ToString @NoArgsConstructor @AllArgsConstructor @TableName("tb_user") public class User { @TableId(value = "id",type= IdType.ID_WORKER)//指定主键的字段名 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; }
注解说明:
lomlok: @Data:lombok的注解,使用它可以省略getter/setter方法。 @NoArgsConstructor: 生成无参构造 方法 @AllArgsConstructor:生成所有参数构造 方法,参数顺序与属性定义顺序一致。 mybaits-plus: @TableName:指定表名 @TableId:指定主键名 @TableField:指定列名
编写mapper
package cn.yyl.mp.mapper; import cn.yyl.mp.pojo.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * Created by Administrator. */ public interface UserMapper extends BaseMapper<User> { }
该mapper类继承mybatisplus中的BaseMapper类
BaseMapper类中有一些常用的数据库查询方法,使用Base类之外的查询方法可以自己手动配置xml实现:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.baomidou.mybatisplus.core.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; public interface BaseMapper<T> { int insert(T entity); int deleteById(Serializable id); int deleteByMap(@Param("cm") Map<String, Object> columnMap); int delete(@Param("ew") Wrapper<T> wrapper); int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList); int updateById(@Param("et") T entity); int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper); T selectById(Serializable id); List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList); List<T> selectByMap(@Param("cm") Map<String, Object> columnMap); T selectOne(@Param("ew") Wrapper<T> queryWrapper); Integer selectCount(@Param("ew") Wrapper<T> queryWrapper); List<T> selectList(@Param("ew") Wrapper<T> queryWrapper); List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper); List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper); IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); }
编写启动类
package cn.yyl.mp; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author Administrator * @version 1.0 **/ @SpringBootApplication @MapperScan("cn.yyl.mp.mapper") //设置mapper接口的扫描包 public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class,args); } }
注解说明:
@SpringBootApplication springboot启动入口标识 @MapperScan("cn.yyl.mp.mapper") mapper接口的扫描包
测试
编写测试用例
编写UserMapper的测试用例 ,使用UserMapper查询用户列表。
在test下创建测试类,包名为 cn.yyl.mp。
package cn.itcast.mp; import cn.itcast.mp.mapper.UserMapper; import cn.itcast.mp.pojo.User; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.List; /** * @author Administrator * @version 1.0 **/ @RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest { @Autowired UserMapper userMapper; x @Test public void testSelect(){ //查询tb_user记录 List<User> users = userMapper.selectList(null); System.out.println(users.toString()); } }
运行测试
[ User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=test1@itcast.cn, birthday=2022-03-09T00:00), User(id=2, userName=lisi, password=123456, name=李四, age=20, email=test2@itcast.cn, birthday=2022-03-09T00:00), User(id=3, userName=wangwu, password=123456, name=王五, age=28, email=test3@itcast.cn, birthday=2022-03-09T00:00), User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=test4@itcast.cn, birthday=2022-03-09T00:00), User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=test5@itcast.cn, birthday=2022-03-09T00:00) ]