实验: spring-boot 整合 fluent-mybatis 实验过程!!!!
1.参考:
简单整合,会报错误
https://segmentfault.com/a/1190000040467885?utm_source=sf-similar-article
利用maven编译,
https://blog.csdn.net/zhiweihongyan1/article/details/120848199?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164592123216780274162555%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164592123216780274162555&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-5-120848199.pc_search_result_cache&utm_term=fluent+mybatis&spm=1018.2226.3001.4187
打开目标显示才不会错误。
2. 新建工程
附:idea 建立spring-boot程序抓图:参考连接。 https://www.cnblogs.com/cqmcu/p/15926462.html
添加pom
1.8 1.6.13 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java runtime org.projectlombok lombok true com.github.atool fluent-mybatis ${fluent-mybatis.version} com.github.atool fluent-mybatis-processor ${fluent-mybatis.version} org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 org.apache.maven.plugins maven-resources-plugin 3.1.0 org.springframework.boot spring-boot-maven-plugin
注意,mawen编译插件,没有则有问题。
org.apache.maven.plugins
maven-resources-plugin
3.1.0
3. application.yml配置
server: port: 8080 # 端口号 spring: datasource: # 数据库参数配置 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8 username: root password: 123456
4. EntityGeneratorTests.java 自动代码生成
package com.example.helloworld; import cn.org.atool.generator.FileGenerator; import cn.org.atool.generator.annotation.Table; import cn.org.atool.generator.annotation.Tables; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class EntityGeneratorTests { // 数据源 url static final String url = "jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8"; // 数据库用户名 static final String username = "root"; // 数据库密码 static final String password = "123456"; @Test public void generate() { // 引用配置类,build方法允许有多个配置类 FileGenerator.build(Empty.class); } @Tables( // 设置数据库连接信息 url = url, username = username, password = password, // 设置entity类生成src目录, 相对于 user.dir srcDir = "src/main/java", // 设置entity类的package值 basePack = "com.example.helloworld", // 设置dao接口和实现的src目录, 相对于 user.dir daoDir = "src/main/java", // 设置哪些表要生成Entity文件 tables = {@Table(value = {"person"})} ) static class Empty { //类名随便取, 只是配置定义的一个载体 } }
5. 利用maven编译
6.显示target
7.编译产生代码
8. 产生目标代码
9.添加映射扫描
10.Controller测试代码编写
@RestController @RequestMapping("/person") public class PersonController { @Resource PersonDao personDao; @Resource PersonMapper personMapper; //数据库的操作:增删改查 /** * 根据ID查询数据1 * @param id * @return */ @GetMapping("/getById") public PersonEntity getById(Integer id){ return personDao.selectById(id); } /** * 根据ID删除 * @param id */ @GetMapping("/deleteById") public void deleteById(Integer id){ personDao.deleteById(id); } /** * 根据ID进行更新 * @param personEntity * @return */ @PostMapping("/updateById") public PersonEntity updateById(@RequestBody PersonEntity personEntity){ boolean b = personDao.updateById(personEntity); if (b){ return personDao.selectById(personEntity.getId()); } return null; } /** * 新增 * @param personEntity * @return */ @PostMapping("/insert") public Integer insert(@RequestBody PersonEntity personEntity){ return personDao.save(personEntity); } }
11.利用postman工具测试
12-完整代码下载地址:
链接:https://pan.baidu.com/s/1YCuKfQHFyCmIVxkYFnHIOw
提取码:ca61
--来自百度网盘超级会员V5的分享