pom.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.5.2</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.lagou</groupId>
    12. <artifactId>mybatis</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>mybatis</name>
    15. <description>Demo project for Spring Boot</description>
    16. <properties>
    17. <java.version>11</java.version>
    18. </properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.mybatis.spring.boot</groupId>
    22. <artifactId>mybatis-spring-boot-starter</artifactId>
    23. <version>2.2.0</version>
    24. </dependency>
    25. <dependency>
    26. <groupId>mysql</groupId>
    27. <artifactId>mysql-connector-java</artifactId>
    28. <scope>runtime</scope>
    29. </dependency>
    30. <dependency>
    31. <groupId>org.projectlombok</groupId>
    32. <artifactId>lombok</artifactId>
    33. <optional>true</optional>
    34. </dependency>
    35. <dependency>
    36. <groupId>org.springframework.boot</groupId>
    37. <artifactId>spring-boot-starter-test</artifactId>
    38. <scope>test</scope>
    39. </dependency>
    40. <dependency>
    41. <groupId>junit</groupId>
    42. <artifactId>junit</artifactId>
    43. <scope>test</scope>
    44. </dependency>
    45. <!-- !!!分页starter -->
    46. <dependency>
    47. <groupId>com.github.pagehelper</groupId>
    48. <artifactId>pagehelper-spring-boot-starter</artifactId>
    49. <version>1.2.3</version>
    50. </dependency>
    51. </dependencies>
    52. <build>
    53. <plugins>
    54. <plugin>
    55. <groupId>org.springframework.boot</groupId>
    56. <artifactId>spring-boot-maven-plugin</artifactId>
    57. <configuration>
    58. <excludes>
    59. <exclude>
    60. <groupId>org.projectlombok</groupId>
    61. <artifactId>lombok</artifactId>
    62. </exclude>
    63. </excludes>
    64. </configuration>
    65. </plugin>
    66. <plugin>
    67. <groupId>org.apache.maven.plugins</groupId>
    68. <artifactId>maven-compiler-plugin</artifactId>
    69. <configuration>
    70. <source>8</source>
    71. <target>8</target>
    72. </configuration>
    73. </plugin>
    74. </plugins>
    75. </build>
    76. </project>

    application.yml

    1. spring:
    2. datasource:
    3. driver-class-name: com.mysql.cj.jdbc.Driver
    4. url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&characterEncoding=UTF-8
    5. username: root
    6. password: root
    7. mybatis:
    8. configuration:
    9. map-underscore-to-camel-case: true
    10. type-aliases-package: com.lagou.mybatis.entitiy
    11. #配置MyBatis的xml配置文件路径
    12. mapper-locations: classpath:mapper/*.xml
    13. # 分页配置
    14. pagehelper:
    15. helper-dialect: mysql
    16. reasonable: true
    17. support-methods-arguments: true
    18. params: count=countSql

    com/lagou/mybatis/MybatisApplication.java

    1. package com.lagou.mybatis;
    2. import org.mybatis.spring.annotation.MapperScan;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. @SpringBootApplication
    6. // 配置 mappper扫描,省去每个mapper写 @Mapper注解
    7. @MapperScan("com.lagou.mybatis.mapper")
    8. public class MybatisApplication {
    9. public static void main(String[] args) {
    10. SpringApplication.run(MybatisApplication.class, args);
    11. }
    12. }

    com/lagou/mybatis/entity/User.java

    1. package com.lagou.mybatis.entity;
    2. import lombok.Data;
    3. import java.util.Date;
    4. @Data
    5. public class User {
    6. private Integer id;
    7. private String username;
    8. private Date birthday;
    9. private String sex;
    10. private String address;
    11. }

    com/lagou/mybatis/mapper/UserMapper.java

    1. package com.lagou.mybatis.mapper;
    2. import com.lagou.mybatis.entity.User;
    3. import java.util.List;
    4. public interface UserMapper {
    5. List<User> findAll();
    6. }

    resoource/mapper/UserMapper.xml

    1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="com.lagou.mybatis.mapper.UserMapper">
    4. <!--查询所有-->
    5. <select id="findAll" resultType="com.lagou.mybatis.entity.User">
    6. select * from user
    7. </select>
    8. </mapper>

    com/lagou/mybatis/MybatisApplicationTests.java

    package com.lagou.mybatis;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.lagou.mybatis.entity.User;
    import com.lagou.mybatis.mapper.UserMapper;
    import org.junit.jupiter.api.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.util.List;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    class MybatisApplicationTests {
        @Autowired
        private UserMapper userMapper;
    
        @Test
        public void testPageHelper() {
            PageHelper.startPage(1,1);
            List<User> users = userMapper.findAll();
            PageInfo<User> pageInfo = new PageInfo<>(users);
            System.out.println("total:"+pageInfo.getTotal());
            System.out.println("page num:"+pageInfo.getPageNum());
            System.out.println("page size:"+pageInfo.getPageSize());
            users.stream().forEach(System.out::println);
        }
    }