Springboot之restfull接口规范注解(二)

1,springboot逆向mybatis生成接口类

pom依赖包

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

pom依赖插件

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
&lt;version&gt;1.3.5&lt;/version&gt;
&lt;configuration&gt;
    &lt;verbose&gt;true&lt;/verbose&gt;
    &lt;overwrite&gt;true&lt;/overwrite&gt;
&lt;/configuration&gt;

</plugin>

generator.properties配置文件

jdbc.driverLocation=D:/mysql-connector-java-8.0.16.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/test
jdbc.userId=root
jdbc.password=root

generatorConfig.xml文件

javaModelGenerator模块生成的路径
sqlMapGeneratorMapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件,一般指的到resource下新建一个mapper文件夹里面targetProject="src/main/resource">
javaClientGeneratorMapper接口的文件路径targetProject="src/main/java" type="XMLMAPPER">
table数据库表名称
                          ```xml
                          <?xml version="1.0" encoding="UTF-8"?>
                          <!DOCTYPE generatorConfiguration
                                  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
                                  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
                      &lt;generatorConfiguration&gt;
                          &lt;!--导入属性配置--&gt;
                          &lt;properties resource=&quot;generator.properties&quot;&gt;&lt;/properties&gt;

                          &lt;!--指定特定数据库的jdbc驱动jar包的位置--&gt;
                          &lt;classPathEntry location=&quot;${jdbc.driverLocation}&quot;/&gt;

                          &lt;context id=&quot;default&quot; targetRuntime=&quot;MyBatis3&quot;&gt;

                              &lt;!-- optional,旨在创建class时,对注释进行控制 --&gt;
                              &lt;commentGenerator&gt;
                                  &lt;property name=&quot;suppressDate&quot; value=&quot;true&quot;/&gt;
                                  &lt;property name=&quot;suppressAllComments&quot; value=&quot;true&quot;/&gt;
                              &lt;/commentGenerator&gt;

                              &lt;!--jdbc的数据库连接 --&gt;
                              &lt;jdbcConnection
                                      driverClass=&quot;${jdbc.driverClass}&quot;
                                      connectionURL=&quot;${jdbc.connectionURL}&quot;
                                      userId=&quot;${jdbc.userId}&quot;
                                      password=&quot;${jdbc.password}&quot;&gt;
                                  &lt;!--MySQL 不支持 schema 或者 catalog 所以需要添加这个--&gt;
                                  &lt;!-- 不然会出现生成器把其他数据库的同名表生成下来的问题 --&gt;
                                  &lt;!-- 现象就是某个类中出现了数据库表里面没有的字段 --&gt;
                                  &lt;property name=&quot;nullCatalogMeansCurrent&quot; value=&quot;true&quot;/&gt;
                              &lt;/jdbcConnection&gt;


                              &lt;!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--&gt;
                              &lt;javaTypeResolver&gt;
                                  &lt;property name=&quot;forceBigDecimals&quot; value=&quot;false&quot;/&gt;
                              &lt;/javaTypeResolver&gt;


                              &lt;!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
                                  targetPackage     指定生成的model生成所在的包名
                                  targetProject     指定在该项目下所在的路径
                              --&gt;
                              &lt;javaModelGenerator targetPackage=&quot;com.example.test001.bean&quot;
                                                  targetProject=&quot;src/main/java&quot;&gt;

                                  &lt;!-- 是否允许子包,即targetPackage.schemaName.tableName --&gt;
                                  &lt;property name=&quot;enableSubPackages&quot; value=&quot;false&quot;/&gt;
                                  &lt;!-- 是否对model添加 构造函数 --&gt;
                                  &lt;property name=&quot;constructorBased&quot; value=&quot;true&quot;/&gt;
                                  &lt;!-- 是否对类CHAR类型的列的数据进行trim操作 --&gt;
                                  &lt;property name=&quot;trimStrings&quot; value=&quot;true&quot;/&gt;
                                  &lt;!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 --&gt;
                                  &lt;property name=&quot;immutable&quot; value=&quot;false&quot;/&gt;
                              &lt;/javaModelGenerator&gt;

                              &lt;!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --&gt;
                              &lt;sqlMapGenerator targetPackage=&quot;mapper&quot;
                                               targetProject=&quot;src/main/resource&quot;&gt;
                                  &lt;property name=&quot;enableSubPackages&quot; value=&quot;false&quot;/&gt;
                              &lt;/sqlMapGenerator&gt;

                              &lt;!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                                      type=&quot;ANNOTATEDMAPPER&quot;,生成Java Model 和基于注解的Mapper对象
                                      type=&quot;MIXEDMAPPER&quot;,生成基于注解的Java Model 和相应的Mapper对象
                                      type=&quot;XMLMAPPER&quot;,生成SQLMap XML文件和独立的Mapper接口
                              --&gt;
                              &lt;javaClientGenerator targetPackage=&quot;com.example.test001.mapper&quot;
                                                   targetProject=&quot;src/main/java&quot; type=&quot;XMLMAPPER&quot;&gt;
                                  &lt;property name=&quot;enableSubPackages&quot; value=&quot;true&quot;/&gt;
                              &lt;/javaClientGenerator&gt;

                              &lt;!--要执行逆向工程所用到的表--&gt;
                              &lt;table tableName=&quot;user&quot; /&gt;
                      &lt;!--        &lt;table tableName=&quot;category&quot; /&gt;--&gt;
                          &lt;/context&gt;
                      &lt;/generatorConfiguration&gt;
                      ```

2,执行generator生成接口类
1,控制台使用mvn命令:
mvn mybatis-generator:generate
2,双击mvn里面的pulgins插件下的renerator启动插件

image-20220522232558311

执行完成后就会生成三个文件

image-20220522232829247

3,创建一个控制器
注解说明例子
@RestController在类上添加注解,相当于在类上添加@Controller和在方法上添加@ ResponseBody二合一,在类上加了@RestController注解后就不在在每个方法上加@ResponseBody注解了@RestController
public class StudentController {}
@Autowired自动注入注解的对象@Autowired
private StudentService studentService;
@GetMappingget请求的注解,相当于@RequestMapping(value = "/get",method = RequestMethod.GET),后面的port,put,delete用法都是一样@GetMapping(value = "/get/{id}")
@PathVariable路径变量,识别注解上面的变量,如 "/get/{id}",id变量可以通过这个注解识别public Object getName(@PathVariable( "id") Integer id){}
@PathParam定义一个参数,不加这个注解,默认取的是形式参数的变量名public Object inster(
@PathParam("name") String name){}

​ ```

package com.springboot.test002.web;

import com.springboot.test002.model.Student;
import com.springboot.test002.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;

/**

  • restfull接口案例,连接数据库进行增删改差
    */
    @RestController
    public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/get/{id}")
    public Object getName(@PathVariable( "id") Integer id){
    HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
    objectObjectHashMap.put("id",id);
    return objectObjectHashMap;
    }

    @PostMapping(value = "/inster")
    public Object inster(
    @PathParam("name") String name,
    @PathParam("age") Integer age,
    @PathParam("passwod") String password){
    HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
    objectObjectHashMap.put("name",name);
    objectObjectHashMap.put("age",age);
    objectObjectHashMap.put("passwod",password);
    int i = studentService.inster(objectObjectHashMap);
    return i ;
    }
    @PutMapping(value = "/update/{id}")
    public Object update(@PathVariable("id") Integer id,
    @PathParam("name") String name,
    @PathParam("age") Integer age,
    @PathParam("password") String password){
    Student student = new Student();
    student.setId(id);
    student.setName(name);
    student.setAge(age);
    student.setPasswod(password);
    int i = studentService.update(student);
    return i ;
    }
    @DeleteMapping(value = "/delete/{id}")
    public Object delete(
    @PathVariable( "id") Integer id){
    Student student = new Student();
    student.setId(id);
    return studentService.delete(student.getId());
    }

    @RequestMapping(value = "/getUser")
    public Object get(Integer id){
    Student student = new Student();
    student.setId(id);
    return studentService.get(student.getId());
    }
    }

4,application配置文件添加引入mapper的xml路径

指定resource路径下的mapper下的所有xml文件

mybatis:
    mapper-locations: classpath:mapper/*.xml

完整的配置文件

  # 应用服务 WEB 访问端口
  server:
      port: 8081
      servlet:
          context-path: /
  spring:
      datasource:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8&&characterEncoding=utf8
          password: root
          username: root
      redis:
          password: 123456
          host: 127.0.0.1
          port: 6379
  mybatis:
      mapper-locations: classpath:mapper/*.xml
5,执行效果

image-20220523000133185

6,测试代码地址

https://github.com/redesperado/SpringBoot


本文永久更新地址:

https://www.fenxiangbe.com/p/pringboot之restfull接口规范注解(二).html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值