先感谢大佬文章帮我走完最后一步连接如下:https://blog.csdn.net/ZZ2713634772/article/details/79458489#commentBox
一,新建项目
1.按正常操作流程,完成创建
二,在pom.xml文件中引入相关依赖
<dependencies>
<!--spring boot 关键依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--spring boot 测试依赖,可以不要-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--swagger2相关-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--json相关-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
三,使用mybatis也要添加相关配置
主要目的是使Mapper与相对应的Xml文件进行绑定
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<!-- 此配置不可缺,否则mybatis的Mapper.xml将会丢失 -->
<includes>
<include>classpath*:mybatis/*.xml</include>
</includes>
</resource>
<!--指定资源的位置-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
这里也要添加对应数据库信息,我使用较为简单和原始的属性文件配置
spring.datasource.name=zhouzexinTest
spring.datasource.url=XXXXX
spring.datasource.username=XXXXXX
spring.datasource.password=XXXX
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
###########################mybatis配置###############################
#全局映射器启用缓存
mybatis.configuration.cache-enabled=true
#查询时,关闭关联对象及时加载以提高性能
mybatis.configuration.lazy-loading-enabled=false
#按需加载字段(加载字段由SQL指定),不会加载关联表的所有字段,以提高性能
mybatis.configuration.aggressive-lazy-loading=false
#允许返回不同的结果集以达到通用的效果
mybatis.configuration.multiple-result-sets-enabled=true
#对于批量更新操作缓存SQL以提高性能
mybatis.configuration.default-executor-type=REUSE
#数据库执行超时时间
mybatis.configuration.default-statement-timeout=25000
mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.example.demo.Mapper
四,创建对应的包
目的:方便管理文件
dto ---“是对应实体类,可以了解解为Bean,pojo
映射器---“为与对应XML文件的接口,可理解为前半部分道层,主要目的是完成对应的Xml内SQL语句的绑定
服务 - >主要完成基础操作,可以理解到道
IMPL --->对应实现类
控制器---“完成对应消息的控制与转发,可理解为(MVC中控制层)
五,介绍类
最重要的两个配置类
DemoApplication 和 SwaggerConfig
spring boot的起点为DemoApplication,具体为什么,可以自行百度,有很多大佬已经解释的很清楚了,SwaggerConfig为Swagger的配置类
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* <pre>类名: SwaggerConfig</pre>
* <pre>描述: TODO</pre>
* <pre>版权: zhouzexin</pre>
* <pre>日期: 2018-11-20 15:24</pre>
*
* @author zhouzexin
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 测试使用 Swagger2 构建 API")
//创建人
.contact(new Contact("zhouzexin", "https://mp.csdn.net/postedit/84344724", ""))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
控制层
package com.example.demo.controller;
import com.example.demo.dto.DemoDTO;
import com.example.demo.dto.ResultDTO;
import com.example.demo.dto.VehicleDTO;
import com.example.demo.serives.VehicleSerives;
import com.sun.net.httpserver.Authenticator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
/**
* <pre>类名: DemoController</pre>
* <pre>描述: TODO</pre>
* <pre>日期: 2018-11-19 17:21</pre>
*
* @author zhouzexin
*/
@Api(value = "demo接口",tags = "demo接口")
@RestController
@RequestMapping
public class DemoController {
@Autowired
private VehicleSerives vehicleSerives;
@RequestMapping(value = "/demo4",method = RequestMethod.POST)
@ApiOperation(value = "新增")
public ResultDTO<DemoDTO> saveDemo(@RequestBody @Valid @ApiParam("Demo参数") VehicleDTO vehicleDTO){
System.out.println(" Code : "+ (vehicleDTO.getCode()==null||vehicleDTO.getCode().equals("")?"null":vehicleDTO.getCode()));
System.out.println(" Brandmode "+ (vehicleDTO.getBrandmode()==null||vehicleDTO.getBrandmode().equals("")?"null":vehicleDTO.getBrandmode()));
System.out.println(" VehicleType :"+(vehicleDTO.getVehicleType()==null||vehicleDTO.getVehicleType().equals("")?"null":vehicleDTO.getVehicleType()));
System.out.println(" getGoodid :"+(vehicleDTO.getGoodId()==null||vehicleDTO.getGoodId().equals("")?"null":vehicleDTO.getGoodId()));
boolean insertVehicle = vehicleSerives.insertVehicle(vehicleDTO);
ResultDTO resultDTO =new ResultDTO("新增车辆 " ,insertVehicle,insertVehicle,12);
return resultDTO;
}
@RequestMapping(value = "/demo1",method = RequestMethod.GET)
@ApiOperation("展示")
public String demo1(){
System.out.println("===================================\n demo1 \n===================================");
return "Hello Spring Boot demo1";
}
@RequestMapping(value = "/demo2",method = RequestMethod.GET)
@ApiOperation("参数测试_必有参数")
public String index2(@RequestParam @Valid DemoDTO demoDTO){
System.out.println("demo2===================================");
return "Hello Spring Boot"+ demoDTO;
}
@RequestMapping(value = "/demo3",method = RequestMethod.GET)
@ApiOperation("参数测试_参数")
public ResultDTO<VehicleDTO> index3(@RequestParam(value = "code" ,required = false)@Valid String code){
VehicleDTO vehicleDTO = vehicleSerives.getVehicle(code);
System.out.println("===================================\ndemo3\n===================================");
String r = "Hello Spring Boot !!!\n"+(code==null||"".equals(code)?"没有输入":("车辆编号 :"+code)) +
"\n"+(vehicleDTO==null?"无此车":vehicleDTO);
return new ResultDTO <>("r",vehicleDTO) ;
}
@RequestMapping(value = "/demo/{uname}/{upwd}",method = RequestMethod.GET)
@ApiOperation("参数测试mother_参数")
public String login(@PathVariable @ApiParam("用例测试名") @Valid String uname, @PathVariable @ApiParam("用例测试密码") @Valid String upwd) {
ModelAndView mv = new ModelAndView();
// 等同于request.setAttribute("name", uname)
System.out.println("姓名 " + uname);
System.out.println("密码 " + upwd);
mv.addObject("name", uname);
mv.setViewName("show");
mv.addObject("pwd",upwd);
System.out.println(mv);
return mv.toString();
}
}
如果缺少对应的豆(POJO)实体,自己创建就好,
服务(DAO)中
写对应业务操作
package com.example.demo.service;
import com.example.demo.dto.VehicleDTO;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <pre>类名: VehicleSerives</pre>
* <pre>描述: TODO</pre>
* <pre>日期: 2018-11-22 10:39</pre>
*
* @author zhouzexin
*/
@Service
public interface VehicleSerives {
VehicleDTO getVehicle(String Code);
boolean deleteVehicle(String Code);
List<VehicleDTO> qryAllVehicleDetail();
boolean insertVehicle(VehicleDTO vehicleDTO);
}
映射里
package com.example.demo.Mapper;
import com.example.demo.dto.VehicleDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public interface VehicleMapper {
int insertVehicle(VehicleDTO VehicleDTO);
List<VehicleDTO> qryAllVehicleDetail();
List<VehicleDTO> qryVehicleDetail(String code);
int deleteByVehicleID(String VehicleID);
int updateVehicle(VehicleDTO VehicleDTO);
}
对应的Xml中,其中比较重要的是id【比如选中的id,对应绑定接口里的方法名,mybatis原理也有大佬解释的很详细】,namespace【绑定对应Mapper(就上面写的) 】,关于这些Xml中具体描述可以找mybatis的相关文章。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.Mapper.VehicleMapper">
<resultMap id="BaseResultMap" type="com.example.demo.dto.VehicleDTO">
<id column="good_id" jdbcType="VARCHAR" property="goodId" />
<result column="org_id" jdbcType="BIGINT" property="orgId" />
<result column="type_id" jdbcType="BIGINT" property="typeId" />
<result column="code" jdbcType="VARCHAR" property="code" />
<result column="vehicle_type" jdbcType="VARCHAR" property="vehicleType" />
<result column="vehicle_class" jdbcType="VARCHAR" property="vehicleClass" />
</resultMap>
<sql id="Base_Column_List">
good_id, org_id, type_id, code, vehicle_type, vehicle_class
</sql>
<select id="qryVehicleDetail" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_vehicle_good
where code = #{code,jdbcType=VARCHAR}
</select>
<delete id="deleteByVehicleID" parameterType="java.lang.String">
delete from sys_vehicle_good
where code = #{VehicleID,jdbcType=VARCHAR}
</delete>
<insert id="insertVehicle" parameterType="com.example.demo.dto.VehicleDTO">
insert into sys_vehicle_good
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="goodId != null">
good_id,
</if>
<if test="orgId != null">
org_id,
</if>
<if test="typeId != null">
type_id,
</if>
<if test="code != null">
code,
</if>
<if test="vehicleType != null">
vehicle_type,
</if>
<if test="vehicleClass != null">
vehicle_class,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="goodId != null">
#{goodId,jdbcType=VARCHAR},
</if>
<if test="orgId != null">
#{orgId,jdbcType=INTEGER},
</if>
<if test="typeId != null">
#{typeId,jdbcType=INTEGER},
</if>
<if test="code != null">
#{code,jdbcType=VARCHAR},
</if>
<if test="vehicleType != null">
#{vehicleType,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateVehicle" parameterType="com.example.demo.dto.VehicleDTO">
update sys_vehicle_good
<set>
<if test="orgId != null">
org_id = #{orgId,jdbcType=INTEGER},
</if>
<if test="typeId != null">
type_id = #{typeId,jdbcType=INTEGER},
</if>
<if test="code != null">
code = #{code,jdbcType=VARCHAR},
</if>
<if test="vehicleType != null">
vehicle_type = #{vehicleType,jdbcType=VARCHAR},
</if>
<if test="vehicleClass != null">
vehicle_class = #{vehicleClass,jdbcType=VARCHAR},
</if>
</set>
where good_id = #{goodId,jdbcType=VARCHAR}
</update>
</mapper>
然后写好对应实体类
在DemoApplication中启动
浏览器中访问的http://本地主机:8080 /招摇,ui.html#/就可以了