SpringBoot集成mybatis-plus

SpirngBoot学习记录-2.0-集成mybatis-plus以及简单测试

  • 简述
    • 1:导入所需的依赖
    • 2:配置 application.properties 或者 application.yml
    • 3:编写实体类
    • 4:编写mapper
    • 5:测试
  • 结尾

简述

本文讲述Spring配置mybatis-plus以及一个测试案例

1:导入所需的依赖

这里是我的pom

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        
		<!--mysql->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--lombok 可选-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--  mybatis-plus  -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        
    </dependencies>

2:配置 application.properties 或者 application.yml

我这里以 properties 为例

#我是用的是mysql8.0如果是其他版本,请注意url与driver的区别,切勿直接粘贴复制
spring.datasource.url= jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
logging.level.cn.bdqn.mapper = debug
server.port=8081

3:编写实体类

User实体类

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author zouxm
 * @create 2019-10-11 9:14
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
//上面是lombok,作用是生成get,set,有参无参与连写set,有兴趣的可以去安装找一下这个插件
@TableName("t_user")
public class User {
    private int id;
    private String name;
    private int age;
    private String email;
    private String password;
}

4:编写mapper

  1. IUserMapper
  2. 继承BaseMapper泛型填写实体类
/**
 * @author zouxm
 * @create 2019-10-11 16:29
 */
public interface IUserMapper extends BaseMapper<User> {
	/**
	*测试可以什么都不用去编写
	*/
}

进入BaseMapper我们可以看到其中有很多的常用的SQL操作,类似与hibernate的用法,由于我们 IUserMapper 继承了它,则正常使用就OK了

public interface BaseMapper<T> extends Mapper<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);
}

  1. 添加扫描Mapper的注解 MapperScan
@SpringBootApplication
@MapperScan("cn.zxm.mapper")
public class Sb0707MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(Sb0707MybatisPlusApplication.class, args);
    }

}

5:测试

在Spring提供的测试类进行测试

  1. 全查询
public class Sb0707MybatisPlusApplicationTests {
    @Autowired
    UserMapper userMapper;

    @Test
    public void selectAll() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        System.out.println(userList);
    }
}

查询结果

Preparing: SELECT id,password,name,email,age FROM t_user
Parameters: 
Total: 22
[User(id=1,name=zhouxp,age=23,email=test1@baomidou.com,password=123456),
User(id=3, name=Tom3, age=28, email=test3@baomidou.com, password=null), 
User(id=4, name=Sandy, age=21,email=test4@baomidou.com,password=123456), 
User(id=5, name=Billie, age=24,email=test5@baomidou.com,password=123456),
User(id=6, name=zxp, age=10, email=zxpmail@163.com, password=123456), 
User(id=7, name=zhang, age=12, email=zxpmail1@163.com, password=123456),
User(id=10, name=zxp, age=18, email=zxpmail@163.com, password=123456), 
User(id=11, name=zxp, age=18, email=zxpmail@163.com,password=123456), 
User(id=12, name=zxp, age=18, email=zxpmail@163.com,password=123456), 
User(id=16, name=test, age=18, email=test@163.com, password=345678), 
User(id=18, name=zhou01, age=10, email=zxpmail@163.com,password=123456),
User(id=19,name=zhou02,zhouxp,age=10,email=zxpmail@163.com,password=123456),
User(id=20,name=DeleteTHIS,age=10,email=zxpmail@163.com,password=123456), 
User(id=21,name=DeleteTHIS,age=45,email=zxpmail@163.com,password=123456),
User(id=23, name=admin, age=26, email=adminq, password=123456), 
User(id=25, name=admin123,age=26,email=zxpmail@163.com,password=123456), 
User(id=26, name=lisi, age=18, email=zxpmail@163.com, password=123456), 
User(id=27, name=zhang san, age=40, email=z@163.com, password=null), 
User(id=28, name=zhou1, age=34, email=z@13.com, password=123456), 
User(id=29, name=张三, age=10, email=2240241948@qq.com, password=123456), 
User(id=30, name=null, age=10, email=2240241948@qq.com, password=123456), 
User(id=31, name=za, age=18, email=2240241948@qq.com, password=123456)]
  1. 根据条件进行查询
@Test
    public void selectByLike() {
        QueryWrapper<User> wapper = new QueryWrapper<>();
        wapper.like("name", "z");
        List<User> users = userMapper.selectList(wapper);
        System.out.println(users);
    }

查询结果

Preparing: SELECT id,password,name,email,age FROM t_user WHERE (name LIKE ?) 
Parameters: %z%(String)
Total: 11
[
User(id=1, name=zhouxp, age=23,email=test1@baomidou.com,password=123456), 
User(id=6, name=zxp, age=10, email=zxpmail@163.com, password=123456), 
User(id=7, name=zhang, age=12, email=zxpmail1@163.com, password=123456), 
User(id=10, name=zxp, age=18, email=zxpmail@163.com, password=123456), 
User(id=11, name=zxp, age=18, email=zxpmail@163.com, password=123456), 
User(id=12, name=zxp, age=18, email=zxpmail@163.com, password=123456), 
User(id=18, name=zhou01, age=10, email=zxpmail@163.com, password=123456),
User(id=19,name=zhou02,zhouxp,age=10,email=zxpmail@163.com,password=123456), 
User(id=27, name=zhang san, age=40, email=z@163.com, password=null), 
User(id=28, name=zhou1, age=34, email=z@13.com, password=123456), 
User(id=31, name=za, age=18, email=2240241948@qq.com, password=123456)
]

结尾

本文章参考 mybatis-plus 中文文档
https://mp.baomidou.com/guide/

你可能感兴趣的:(SpringBoot学习记录,#)