首先https://start.spring.io/下载一个springBoot的demo。
然后增加pomyila依赖,引入以下pom,除了MybatisPlus其他自己分配
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shiyan.my</groupId>
<artifactId>my</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>demo</finalName>
</build>
</project>
application.properties配置文件如下:具体参数自己配置
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
代码生成器部分代码,不完善
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的分页,强大,简洁
@Api("测试")
@RestController
public class EmployeeController {
@Resource
private EmployeeService employeeService;
@ApiOperation(value = "获取所有用户")
@PostMapping("/queryAll")
public List<Employee> queryEmployees() {
List<Employee> 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<Employee> getAllssss(@RequestBody PageInput input) {
Page<Employee> page = new Page<>(input.getPageNum(), input.getPageSize());
IPage<Employee> all = employeeService.getAll(page);
return all;
}
@ApiOperation(value = "所有2")
@PostMapping("/getAll2")
public List<Employee> getAllssss2(@RequestBody PageInput input) {
PageHelper.startPage(input.getPageNum(), input.getPageSize());
List<Employee> all = employeeService.getAll2();
Long all2 = employeeService.getAll();
PageBean<Employee> pageData = new PageBean<>(input.getPageNum(), input.getPageSize(), all2.intValue());
pageData.setItems(all);
List<Employee> items = pageData.getItems();
return items;
}
看下结果: