【六:(mock数据)spring boot+mybatis+yml】

目录

1.1、代码编写

Demo类

java 复制代码
import com.course.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Log4j
@RestController
@Api(value = "v1",tags = "这是我的第一个版本的demo")
@RequestMapping("v1")
public class Demo {

    //首先获取一个执行sql语句的对象

    @Autowired
    private SqlSessionTemplate template;

    @RequestMapping(value = "/getUserCount",method = RequestMethod.GET)
    @ApiOperation(value = "可以获取到用户数",httpMethod = "GET")
    public int getUserCount(){
       return template.selectOne("getUserCount");
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public int addUser(@RequestBody User user){
        int result = template.insert("addUser",user);
        return result;
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public int updateUser(@RequestBody User user){

       return  template.update("updateUser",user);
    }

    @RequestMapping(value = "/deleteUser",method = RequestMethod.GET)
    public int delUser(@RequestParam int id){
        return template.delete("deleteUser",id);
    }

}

User类

java 复制代码
@Data
public class User {
    private int id;
    private String name;
    private String sex;
    private int age;

}

启动类 APplication

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.annotation.PreDestroy;

@EnableScheduling
@SpringBootApplication
public class Application {

    private  static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        Application.context = SpringApplication.run(Application.class,args);
    }

    @PreDestroy
    public void close(){
        Application.context.close();
    }

}

1.2、配置类

查询语句的配置 mysql.yml

xml 复制代码
<?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.course">
    <select id="getUserCount" resultType="Integer">
        select count(*) from user;
    </select>

    <insert id="addUser" parameterType="com.course.model.User">
        insert into user(id,name,age,sex)
        values(#{id},#{name},#{age},#{sex})
    </insert>

    <update id="updateUser" parameterType="com.course.model.User">
        update user set name=#{name},age=#{age}
        where id=#{id}

    </update>
    <delete id="deleteUser" parameterType="Integer">
        delete from user where id = #{id}
    </delete>

</mapper>

springboot的配置 application.yml

xml 复制代码
#服务端口配置
server:
   port: 8888
#日志配置
logging:
    path: logs
    file: mylog.log

#报告和数据库的配置
spring:
   application:
          name: report
   datasource:
       driver-class-name: com.mysql.jdbc.Driver #数据库驱动
       # 数据库的地址
       url: jdbc:mysql://localhost:3306/course?useSSL=false&useUnicode=true&characterEncoding=utf-8&servetTimeZone=Asia/Shanghai
       username: root
       password: 123456

mybatis:
    # 做映射的 com.course.model包下的对象
    type-aliases-package: com.course.model
    #加载resources/mapper下的文件
    mapper-locations:
       - mapper/*

日志的配置 logback.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
格式化日式的时间,格式,路径等
-->
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n"/>
    <property name="LOG_PATH" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}"/>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    
    <appender name="CRAWLER_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/event.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/event.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>
    
    <logger name="com.business.intelligence.util.CrawlerLogger" level="INFO" additivity="false">
        <appender-ref ref="CRAWLER_LOG"/>
    </logger>

    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>
    
</configuration>

数据库的配置 mybatis-config.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!--  映射到哪个包里面-->
    <typeAliases>
        <package name="com.course.model"/>
    </typeAliases>

    <!--  映射到哪个sql文件里面-->
    <mappers>
        <mapper resource="mapper/mysql.xml"/>
    </mappers>
</configuration>

1.3、测试:

1.3.1、测试获取用户数

1.3.2、添加用户



1.3.3、数据的更新



1.3.4、数据的删除


相关推荐
sin2201几秒前
idea创建springBoot的五种方式
java·spring boot·intellij-idea
皓木.7 分钟前
苍穹外卖——准备工作
java·数据库·mybatis
一休哥助手16 分钟前
Spring Boot 项目中 Maven 剔除无用 Jar 引用的最佳实践
spring boot·maven·jar
Q_192849990619 分钟前
基于Spring Boot的旅游推荐系统
spring boot·后端·旅游
愤怒的代码21 分钟前
Spring Boot对访问密钥加密解密——RSA
java·spring boot·后端
美美的海顿22 分钟前
springboot基于Java的校园导航微信小程序的设计与实现
java·数据库·spring boot·后端·spring·微信小程序·毕业设计
愤怒的代码24 分钟前
Spring Boot中幂等性的应用
java·spring boot·后端
憶巷2 小时前
MyBatis中动态SQL执行原理
java·sql·mybatis
苹果酱05672 小时前
Golang的文件解压技术研究与应用案例
java·vue.js·spring boot·mysql·课程设计
互联网杂货铺2 小时前
单元测试/系统测试/集成测试知识总结
自动化测试·软件测试·测试工具·职场和发展·单元测试·测试用例·集成测试