SpringBoot整合MybatisPlus并实现分页查询

首先https://start.spring.io/下载一个springBoot的demo。

 

然后增加pomyila依赖,引入以下pom,除了MybatisPlus其他自己分配



	4.0.0

	com.shiyan.my
	my
	0.0.1-SNAPSHOT
	war

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.3.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			mysql
			mysql-connector-java
			runtime
		
		
		
			com.baomidou
			mybatis-plus-boot-starter
			3.0.6
		
		
		
			org.freemarker
			freemarker
		
		
		
			com.alibaba
			fastjson
			1.2.15
		
		
			com.github.pagehelper
			pagehelper
			4.1.4
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			io.springfox
			springfox-swagger2
			2.2.2
		
		
			io.springfox
			springfox-swagger-ui
			2.2.2
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
		demo
	



application.properties配置文件如下:具体参数自己配置

server.port=8080
server.servlet.context-path=/sys
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis-plus.mapper-locations=classpath:/mapper/*.xml
mybatis-plus.type-aliases-package=com.shiyan.demo.beans

mybatis-plus.global-config.id-type=0
mybatis-plus.global-config.db-column-underline=true
mybatis-plus.global-config.capital-mode=true

logging.level.com.shiyan.demo.mapper=debug

    

代码生成器部分代码,不完善

@Test
	public void testGenerator() {
		// 全局配置
		GlobalConfig config = new GlobalConfig();
		config.setActiveRecord(true) // 是否支持AR模式
				.setAuthor("ais") // 作者
				.setOutputDir("E:\\xuexi\\demo\\src\\main\\java")
				// 生成路径
				.setFileOverride(true)// 文件覆盖
				.setServiceName("%sService").setBaseColumnList(true).setBaseResultMap(true) // 设置生成的service接口名
				/* 首字母是否为I */
				.setIdType(IdType.AUTO); // 主键策略//数据源配置
		DataSourceConfig dsConfig = new DataSourceConfig();
		dsConfig.setDbType(DbType.MYSQL).setUrl("jdbc:mysql://localhost:3306/user")
				.setDriverName("com.mysql.jdbc.Driver").setUsername("root").setPassword("root");
		// 策略配置
		StrategyConfig stConfig = new StrategyConfig();
		stConfig.setCapitalMode(true) // 全局大写命名
				//.setDbColumnUnderline(true) // 表名 字段名 是否使用下滑线命名
				.setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
				.setInclude("tbl_employee") // 生成的表
				.setTablePrefix("tbl_"); // 表前缀
		// 包名策略
		PackageConfig pkConfig = new PackageConfig();
		pkConfig.setParent("com.shiyan.demo").setController("controller").setEntity("beans").setService("service");
		AutoGenerator ag = new AutoGenerator().setGlobalConfig(config).setDataSource(dsConfig).setStrategy(stConfig)
				.setPackageInfo(pkConfig);
		ag.execute();
	}

重点:MybatisPlusConfig的配置主要是加载一些插件注入bean

@Configuration
@EnableTransactionManagement
@MapperScan("com.shiyan.demo.mapper*") 
//扫描dao或者是Mapper接口
public class MybatisPlusConfig {
	
	
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor page = new PaginationInterceptor();
		page.setDialectType("mysql");
		
		return page;
	}

	@Bean
	public PerformanceInterceptor performanceInterceptor() {
		PerformanceInterceptor page = new PerformanceInterceptor();
		page.setFormat(true);
		return page;
	}
	
	//配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}

SwaggerConfig的配置文件如下

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Bean
	public Docket createRestApi() throws IOException {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.shiyan.demo")).paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() throws IOException {
		return new ApiInfoBuilder().title("誓言--------").termsOfServiceUrl("http://data.***.com/").contact("***").version("0.0.1").build();
	}
}

EmployeeController  分页的有两个,一个是MybatisPlus自己封装的一个是PageHelper。

MybatisPlus分页插件:单表操作可以用selectPage,下面是测试代码,个人推荐用MybatisPlus的分页,强大,简洁

SpringBoot整合MybatisPlus并实现分页查询_第1张图片

@Api("测试")
@RestController
public class EmployeeController {

	@Resource
	private EmployeeService employeeService;

	@ApiOperation(value = "获取所有用户")
	@PostMapping("/queryAll")
	public List queryEmployees() {
		List selectList = employeeService.list();

		return selectList;
	}

	@ApiOperation(value = "新增用户")
	@PostMapping("/insert")
	public Integer insert(@RequestBody Employee employee) {
		Employee employee2 = new Employee();
		employee2.setAge(employee.getAge());
		employee2.setGender(employee.getGender());
		employee2.setLastName(employee.getLastName());
		employee2.setEmail(employee.getEmail());
		employeeService.save(employee2);
		return employee2.getId();
	}

	@ApiOperation(value = "修改用户")
	@PostMapping("/update")
	public boolean update(@RequestBody Employee employee) {
		Employee employee2 = new Employee();
		employee2.setAge(employee.getAge());
		employee2.setGender(employee.getGender());
		employee2.setLastName(employee.getLastName());
		employee2.setEmail(employee.getEmail());
		employee2.setId(employee.getId());
		boolean update = employeeService.update(employee2, null);
		return update;
	}

	@ApiOperation(value = "删除用户")
	@PostMapping("/delete")
	public boolean delete(@RequestBody Integer id) {

		boolean update = employeeService.removeById(id);
		return update;
	}

	@ApiOperation(value = "根据ID获取用户")
	@PostMapping("/getByid")
	public Employee getByid(@RequestBody Integer id) {

		Employee byid = employeeService.getByid(id);
		return byid;
	}

	@ApiOperation(value = "所有")
	@PostMapping("/getAll")
	public IPage getAllssss(@RequestBody PageInput input) {

		Page page = new Page<>(input.getPageNum(), input.getPageSize());
		IPage all = employeeService.getAll(page);
		return all;
	}

	@ApiOperation(value = "所有2")
	@PostMapping("/getAll2")
	public List getAllssss2(@RequestBody PageInput input) {

		PageHelper.startPage(input.getPageNum(), input.getPageSize());
		List all = employeeService.getAll2();
		Long all2 = employeeService.getAll();
		PageBean pageData = new PageBean<>(input.getPageNum(), input.getPageSize(), all2.intValue());
		pageData.setItems(all);
		List items = pageData.getItems();
		return items;
	}

看下结果:

 

SpringBoot整合MybatisPlus并实现分页查询_第2张图片

菜鸟分享===============================PS:Ais永恒

你可能感兴趣的:(JAVA)