• ssm和springboot整合


    官网:

    MyBatis-PlusMyBatis-Plus 官方文档https://baomidou.com/

    pom.xml

    1. <dependency>
    2. <groupId>com.baomidougroupId>
    3. <artifactId>mybatis-plus-boot-starterartifactId>
    4. <version>3.4.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.baomidougroupId>
    8. <artifactId>mybatis-plus-generatorartifactId>
    9. <version>3.4.1version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.freemarkergroupId>
    13. <artifactId>freemarkerartifactId>
    14. <version>2.3.31version>
    15. dependency>

    application.yum

    1. server:
    2. port: 8080
    3. spring:
    4. application:
    5. name: springboot05
    6. datasource:
    7. driver-class-name: com.mysql.jdbc.Driver
    8. password: 123456
    9. url: jdbc:mysql://localhost:3306/t280?useUnicode=true&characterEncoding=UTF-8
    10. username: root
    11. logging:
    12. level:
    13. com.ruojuan.springboot05: debug
    14. mybatis-plus:
    15. mapper-locations: classpath:mappers/**/*.xml
    16. type-aliases-package: com.ruojuan.springboot05.book.model

    Mybatis-plus代码生成类:

    1. package com.ruojuan.springboot05.mp;
    2. import com.baomidou.mybatisplus.annotation.DbType;
    3. import com.baomidou.mybatisplus.annotation.IdType;
    4. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    5. import com.baomidou.mybatisplus.core.toolkit.StringPool;
    6. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    7. import com.baomidou.mybatisplus.generator.AutoGenerator;
    8. import com.baomidou.mybatisplus.generator.InjectionConfig;
    9. import com.baomidou.mybatisplus.generator.config.*;
    10. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    11. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    12. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    13. import java.util.ArrayList;
    14. import java.util.List;
    15. import java.util.Scanner;
    16. /**
    17. * @author ruojuan
    18. * @site www.ruojuan.com
    19. * @company 玉渊工作室
    20. * @create 2022年11月01日 16:37
    21. **/
    22. /**
    23. * mybatis-plus代码生成
    24. */
    25. public class MPGenerator {
    26. /**
    27. *

    28. * 读取控制台内容
    29. *

    30. */
    31. public static String scanner(String tip) {
    32. Scanner scanner = new Scanner(System.in);
    33. StringBuilder help = new StringBuilder();
    34. help.append("请输入" + tip);
    35. System.out.println(help.toString());
    36. if (scanner.hasNext()) {
    37. String ipt = scanner.next();
    38. if (StringUtils.isNotBlank(ipt)) {
    39. if ("quit".equals(ipt)) return "";
    40. return ipt;
    41. }
    42. }
    43. throw new MybatisPlusException("请输入正确的" + tip + "!");
    44. }
    45. public static void main(String[] args) {
    46. // 代码生成器
    47. AutoGenerator mpg = new AutoGenerator();
    48. // 1.全局配置
    49. GlobalConfig gc = new GlobalConfig();
    50. //System.getProperty("user.dir")指工作区间 就是20200904sal
    51. String projectPath = System.getProperty("user.dir") + "/springboot05";
    52. System.out.println(projectPath);
    53. gc.setOutputDir(projectPath + "/src/main/java");
    54. gc.setOpen(false);
    55. gc.setBaseResultMap(true);//生成BaseResultMap
    56. gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
    57. gc.setEnableCache(false);// XML 二级缓存
    58. gc.setBaseResultMap(true);// XML ResultMap
    59. gc.setBaseColumnList(true);// XML columList
    60. //gc.setSwagger2(true); //实体属性 Swagger2 注解
    61. gc.setAuthor("ruojuan");
    62. // 自定义文件命名,注意 %s 会自动填充表实体属性!
    63. gc.setMapperName("%sMapper");
    64. gc.setXmlName("%sMapper");
    65. gc.setServiceName("%sService");
    66. gc.setServiceImplName("%sServiceImpl");
    67. gc.setControllerName("%sController");
    68. gc.setIdType(IdType.AUTO);
    69. mpg.setGlobalConfig(gc);
    70. // 2.数据源配置
    71. DataSourceConfig dsc = new DataSourceConfig();
    72. dsc.setDbType(DbType.MYSQL);
    73. dsc.setUrl("jdbc:mysql://localhost:3306/t280?useUnicode=true&characterEncoding=UTF-8");
    74. dsc.setDriverName("com.mysql.jdbc.Driver");
    75. dsc.setUsername("root");
    76. dsc.setPassword("123456");
    77. mpg.setDataSource(dsc);
    78. // 3.包配置
    79. PackageConfig pc = new PackageConfig();
    80. String moduleName = scanner("模块名(quit退出,表示没有模块名)");
    81. if (StringUtils.isNotBlank(moduleName)) {
    82. pc.setModuleName(moduleName);
    83. }
    84. pc.setParent("com.ruojuan.springboot05")
    85. .setMapper("mapper")
    86. .setService("service")
    87. .setController("controller")
    88. .setEntity("model");
    89. mpg.setPackageInfo(pc);
    90. // 4.自定义配置
    91. InjectionConfig cfg = new InjectionConfig() {
    92. @Override
    93. public void initMap() {
    94. // to do nothing
    95. }
    96. };
    97. // 如果模板引擎是 freemarker
    98. String templatePath = "/templates/mapper.xml.ftl";
    99. // 自定义输出配置
    100. List focList = new ArrayList<>();
    101. // 自定义配置会被优先输出
    102. focList.add(new FileOutConfig(templatePath) {
    103. @Override
    104. public String outputFile(TableInfo tableInfo) {
    105. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
    106. if (StringUtils.isNotBlank(pc.getModuleName())) {
    107. return projectPath + "/src/main/resources/mappers/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    108. } else {
    109. return projectPath + "/src/main/resources/mappers/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    110. }
    111. }
    112. });
    113. cfg.setFileOutConfigList(focList);
    114. mpg.setCfg(cfg);
    115. // 配置模板
    116. TemplateConfig templateConfig = new TemplateConfig();
    117. templateConfig.setXml(null);
    118. mpg.setTemplate(templateConfig);
    119. // 5.策略配置
    120. StrategyConfig strategy = new StrategyConfig();
    121. // 表名生成策略(下划线转驼峰命名)
    122. strategy.setNaming(NamingStrategy.underline_to_camel);
    123. // 列名生成策略(下划线转驼峰命名)
    124. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    125. // 是否启动Lombok配置
    126. strategy.setEntityLombokModel(true);
    127. // 是否启动REST风格配置
    128. strategy.setRestControllerStyle(true);
    129. // 自定义实体父类strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
    130. // 自定义service父接口strategy.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService");
    131. // 自定义service实现类strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
    132. // 自定义mapper接口strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
    133. strategy.setSuperEntityColumns("id");
    134. // 写于父类中的公共字段plus
    135. strategy.setSuperEntityColumns("id");
    136. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    137. strategy.setControllerMappingHyphenStyle(true);
    138. //表名前缀(可变参数):“t_”或”“t_模块名”,例如:t_user或t_sys_user
    139. strategy.setTablePrefix("t_", "t_sys_");
    140. //strategy.setTablePrefix(scanner("请输入表前缀"));
    141. mpg.setStrategy(strategy);
    142. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    143. // 执行
    144. mpg.execute();
    145. }
    146. }

    然后自动生成:

     

     MvcBookController.java开发:

    基本的增删改查:

    1. package com.ruojuan.springboot05.book.controller;
    2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    3. import com.ruojuan.springboot05.book.model.MvcBook;
    4. import com.ruojuan.springboot05.book.service.MvcBookService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.*;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map;
    10. /**
    11. *

    12. * 前端控制器
    13. *

    14. *
    15. * @author ruojuan
    16. * @since 2022-11-01
    17. */
    18. @RestController
    19. @RequestMapping("/mp")
    20. public class MvcBookController {
    21. @Autowired
    22. private MvcBookService bookService;
    23. //查
    24. @GetMapping("/list")
    25. public List list(){
    26. System.out.println("1212_"+123);
    27. return bookService.list();
    28. }
    29. //查单个
    30. @GetMapping("/get")
    31. public MvcBook get(MvcBook book){
    32. return bookService.getById(book.getBid());
    33. }
    34. //按条件查询
    35. @GetMapping("/listBy")
    36. public List listBy(MvcBook book){
    37. //如果使用的是mybatis,那么我们需要重新写语句,现在不需要
    38. QueryWrapper qw = new QueryWrapper();
    39. qw.like("bname",book.getBname());
    40. return bookService.list(qw);
    41. }
    42. //增加
    43. @PutMapping("/add")
    44. public boolean add(MvcBook book){
    45. return bookService.save(book);
    46. }
    47. //删除
    48. @DeleteMapping("/delete")
    49. public boolean delete(MvcBook book){
    50. return bookService.removeById(book.getBid());
    51. }
    52. //修改
    53. @PostMapping("/update")
    54. public boolean update(MvcBook book){
    55. return bookService.saveOrUpdate(book);
    56. }
    57. }

    多表的:

    Controller.java

    1. //多表联查用户账户对应的
    2. @GetMapping("/getList")
    3. public List getList(String uname){
    4. Map map = new HashMap();
    5. map.put("username",uname);
    6. List maps = bookService.queryUserRole(map);
    7. return maps;
    8. }

    service 

    1. public interface MvcBookService extends IService {
    2. List queryUserRole(Map map);
    3. }

    impl 

    1. @Service
    2. public class MvcBookServiceImpl extends ServiceImpl implements MvcBookService {
    3. @Autowired
    4. private MvcBookMapper bookMapper;
    5. public List queryUserRole(Map map){
    6. return bookMapper.queryUserRole(map);
    7. }
    8. }

    mapper 

    1. @Repository
    2. public interface MvcBookMapper extends BaseMapper {
    3. List queryUserRole(Map map);
    4. }

    xml 

    1. <select id="queryUserRole" parameterType="java.util.Map" resultType="java.util.Map">
    2. SELECT u.username,r.rolename FROM t_shiro_user u,t_shiro_user_role ur,t_shiro_role r
    3. where u.userid = ur.userid and ur.roleid = r.roleid
    4. <if test="username != null and username != ''">
    5. and u.username = #{username}
    6. if>
    7. select>

  • 相关阅读:
    Java学习笔记---多态综合练习
    (封装)已知的一个类Student
    CSP-J1 CSP-S1 第1轮 初赛 考前强化训练
    [AGC058D]Yet Another ABC String
    数模电路基础知识 —— 8. PN结与三极管的工作原理
    Sealos 安装报错问题解决
    前端架构师之01_JavaScript_Ajax
    java -数据结构,单向链表
    第一百三十七回 WebView
    CNN入门实战:猫狗分类
  • 原文地址:https://blog.csdn.net/weixin_67338832/article/details/127639128