环境:jdk1.8 / mysql5.7 / springboot实现书籍增删改查的demo
第一步、准备数据库创建表DROP TABLE IF EXISTS `book`; CREATE TABLE `book` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '书籍名称', `author` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '作者', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of book -- ---------------------------- INSERT INTO `book` VALUES (1, '西游记', '吴承恩'); INSERT INTO `book` VALUES (2, '三国演义', '罗贯中'); INSERT INTO `book` VALUES (3, '水浒传', '施耐庵'); INSERT INTO `book` VALUES (4, '红楼梦', '曹雪芹'); SET FOREIGN_KEY_CHECKS = 1;第二步、创建springboot项目 在pom.xml文件中添加mybatis-plus逆向工程依赖,添加依赖后记得导入刷新
在com.hapek下创建utils包,在utils下创建GenerateTest类 (标有***都是可能要根据自己的情况修改配置)com.baomidou mybatis-plus-boot-starter 3.4.2 com.baomidou mybatis-plus-generator 3.3.2 org.apache.velocity velocity 1.7
package com.hapek.com.hapek.utils; //注意是自己文件所在的包名 ***
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class GenerateTest {
public static void main(String[] args) {
//创建generator对象
AutoGenerator autoGenerator = new AutoGenerator();
//数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("数据库密码"); //数据库密码 ***
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/数据库名"); //替换成自己的数据库名 ***
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
globalConfig.setAuthor("admin");
globalConfig.setOpen(false);
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
//包信息
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.hapek"); //生成文件所在的目录 ***
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setController("controller");
autoGenerator.setPackageInfo(packageConfig);
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("表名"); //需要逆向生成的表名,多张表可用逗号隔开,这里表名写book ***
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true); //是否使用lombok ***
autoGenerator.setStrategy(strategyConfig);
//运行
autoGenerator.execute();
}
}
修改完成配置后,点击运行,生成目录结构如下图
在resources文件目录下创建application.yml文件(注意文件位置)
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:com/hapek/mapper/xml/*.xml
server:
port: 8181
在启动类上加入 @MapperScan(value = “com.hapek.mapper”)
在BookController中编写业务接口 先编写一个查询操作测试
@RestController
@RequestMapping("/book")
public class BookController {
@Resource
private BookServiceImpl bookService;
@GetMapping("/findAll")
public List findAll(){
return bookService.list();
}
}
运行测试
分析一下为什么只需要在controller层中编写接口方法即可。
在service层中BookService 继承了 IService 而在IService中中已经集成了基础的CRUD的操作,故BookService也拥有这些基础的CRUD方法。故直接在controller层中注入service即可,而在mapper层中BookMapper 继承了 baseMapper, baseMapper中已经封装好了CRUD这样的方法,故BookMapper不必再写CRUD相关的sql语句。
接下来在BookcController中继续编写删除,插入,增加操作
// 新增用户
@PostMapping
public boolean addBook(@RequestBody Book book) {
return bookService.save(book);
}
// 修改用户
@PutMapping
public boolean updateBook(@RequestBody Book book) {
return bookService.save(book);
}
// 删除用户
@DeleteMapping("{id}")
public boolean delBook(@PathVariable("id") Long id) {
return bookService.removeById(id);
}
// 根据id查询用户
@GetMapping("findById/{id}")
public Book findById(@PathVariable Long id) {
return bookService.getById(id);
}
在test中编写测试类测试相关功能:略



