package com.nanfeng.getdata.config; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class PageConfig { /** * 3.4.0之前的版本用这个 * @return */ /* @Bean public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); }*/ /** * 3.4.0之后提供的拦截器的配置方式 * * @return */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }
package com.nanfeng.getdata; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.nanfeng.getdata.mapper") public class GetdataApplication { public static void main(String[] args) { SpringApplication.run(GetdataApplication.class, args); } }
package com.nanfeng.getdata.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.nanfeng.getdata.entity.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper extends BaseMapper<User> { /** * 这个sql写的有问题,mp实现多表操作其实就是这样实现的(或者建立xml映射文件) * @return */ //@Select("select name, age from tb1,tb2 where id=1") // public User selectManyTable(); }
# datasource spring: datasource: url: jdbc:mysql:///mptest username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis-plus: global-config: db-config: # 表名前缀 table-prefix: tb_ # id生成策略 数据库自增 #id-type: auto #configuration: # 日志 #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 枚举包 #type-enums-package: com.itheima.mp.enums
package com.itheima.mp; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.itheima.mp.entity.User; import com.itheima.mp.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.HashMap; import java.util.List; import java.util.Map; @SpringBootTest @RunWith(SpringRunner.class) class UserMapperTest { @Autowired private UserMapper userMapper; /** * 根据id查询 */ @Test public void testSelectById() { User user = userMapper.selectById("1"); System.out.println(user); } /** * 添加 */ @Test public void testInsert() { User user = new User(); user.setId("666666666"); //user.setUserName("itcast"); //user.setPassword("itheima"); int count = userMapper.insert(user); System.out.println(count); } /** * 删除 */ @Test public void testDelete() { /* //1. 根据id删除 int count = userMapper.deleteById(8L); */ /* //2. 根据id集合批量删除 List ids = new ArrayList(); ids.add(6); ids.add(7); userMapper.deleteBatchIds(ids); */ //3. 根据map构造条件,删除 Map<String, Object> map = new HashMap<>(); //delete from tb_user where user_name = ? and age = ? map.put("user_name","zhangsan"); map.put("age","18"); userMapper.deleteByMap(map); } /** * 修改 */ @Test public void testUpdateById() { User user = new User(); //user.setId(2L); //user.setPassword("1111111"); int count = userMapper.updateById(user); } /** * 分页查询: * 1. 当前页码:currentPage * 2. 每页显示条数:size * * 注意:使用mp的分页要设置一个拦截器!!! */ @Test public void testSelectPage() { int current = 1;//当前页码 int size = 2;//每页显示条数 IPage<User> page = new Page(current,size); userMapper.selectPage(page,null); List<User> records = page.getRecords();//当前页的数据 long pages = page.getPages();//总页数 2 long total = page.getTotal();//总记录数 4 System.out.println(records); System.out.println(pages); System.out.println(total); } }
<?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 https://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.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itheima</groupId> <artifactId>mybatis_plus_springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis_plus_springboot</name> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>