GithubHelp home page GithubHelp logo

mybatisdemo2's Introduction

MyBatis-Plus简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

MyBatis-Plus官网

依赖

<!--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.0.6</version>
</dependency>
<!--mysql-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    <version>8.0.13</version>
</dependency>

创建并配置数据库

use mydb;

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id`    int(11) NOT NULL AUTO_INCREMENT,
  `name`  varchar(255)     DEFAULT NULL,
  `age`   varchar(255)     DEFAULT NULL,
  `email` varchar(255)     DEFAULT NULL,
  PRIMARY KEY (`id`)
)
  ENGINE = MyISAM
  AUTO_INCREMENT = 19
  DEFAULT CHARSET = utf8;

INSERT INTO `user` (id, name, age, email)
VALUES (1, 'Jone', 18, '[email protected]'),
       (2, 'Jack', 20, '[email protected]'),
       (3, 'Tom', 28, '[email protected]'),
       (4, 'Sandy', 21, '[email protected]'),
       (5, 'Billie', 24, '[email protected]');

INSERT INTO `user` (id, name, age, email)
VALUES (6, 'Jone6', 10, '[email protected]'),
       (7, 'Jack7', 22, '[email protected]'),
       (8, 'Tom8', 283, '[email protected]'),
       (9, 'Sandy9', 241, '[email protected]'),
       (10, 'Billie0', 224, '[email protected]');

ALTER TABLE user MODIFY id BIGINT(20) AUTO_INCREMENT;

application.properties:

# database
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

编写实体类

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

编写Mapper类

public interface UserMapper extends BaseMapper<User> {

}

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

}

开始使用

@Resource
private UserMapper userMapper;

@Test
public void testSelect() {
    System.out.println(("----- selectAll method test ------"));
    List<User> userList = userMapper.selectList(null);
    Assert.assertEquals(5, userList.size());
    userList.forEach(System.out::println);
}

打印结果:

----- selectAll method test ------
User(id=1, name=Jone, age=18, [email protected])
User(id=2, name=Jack, age=20, [email protected])
User(id=3, name=Tom, age=28, [email protected])
User(id=4, name=Sandy, age=21, [email protected])
User(id=5, name=Billie, age=24, [email protected])

分页

添加分页配置类

//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

测试:

@Test
public void test1(){
    System.out.println("----- baseMapper 自带分页 ------");
    Page<User> page = new Page<>(1, 5);
    IPage<User> userIPage = userMapper.selectPage(page,null);
    Assert.assertSame(page, userIPage);
    System.out.println("总条数 ------> " + userIPage.getTotal());
    System.out.println("当前页数 ------> " + userIPage.getCurrent());
    System.out.println("当前每页显示数 ------> " + userIPage.getSize());
    List<User> records = userIPage.getRecords();
    records.forEach(System.out::println);
    System.out.println("----- baseMapper 自带分页 ------");
}

打印:

----- baseMapper 自带分页 ------
总条数 ------> 10
当前页数 ------> 1
当前每页显示数 ------> 5
User(id=1, name=Jone, age=18, [email protected])
User(id=2, name=Jack, age=20, [email protected])
User(id=3, name=Tom, age=28, [email protected])
User(id=4, name=Sandy, age=21, [email protected])
User(id=5, name=Billie, age=24, [email protected])
----- baseMapper 自带分页 ------

条件查找并分页

添加mapper类中方法

public interface UserMapper extends BaseMapper<User> {

    /*按照年龄查找*/
    IPage<User> selectAge10(Page page, @Param("age") Integer age);

}

添加mapper的xml,映射mapper类

<?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.example.demo.mapper.UserMapper">
    <select id="selectAge10" resultType="com.example.demo.entity.User">
        SELECT * FROM user WHERE age=#{age}
    </select>
</mapper>

添加mapper的xml文件扫描

#
mybatis-plus.mapper-locations=classpath:/mapper/*.xml

测试

/*分页查找指定年龄*/
@Test
public void test3() {
    System.out.println("----- baseMapper 自带分页 ------");
    Page<User> page = new Page<>(1, 5);
    IPage<User> userIPage = userMapper.selectAge10(page, 20);
    Assert.assertSame(page, userIPage);
    System.out.println("总条数 ------> " + userIPage.getTotal());
    System.out.println("当前页数 ------> " + userIPage.getCurrent());
    System.out.println("当前每页显示数 ------> " + userIPage.getSize());
    List<User> records = userIPage.getRecords();
    records.forEach(System.out::println);
    System.out.println("----- baseMapper 自带分页 ------");
}

打印:

----- baseMapper 自带分页 ------
总条数 ------> 2
当前页数 ------> 1
当前每页显示数 ------> 5
User(id=2, name=Jack, age=20, [email protected])
User(id=10, name=Billie0, age=20, [email protected])
----- baseMapper 自带分页 ------

mybatisdemo2's People

Contributors

ablexq avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.