【六:(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、数据的删除


相关推荐
莳光.16 分钟前
122、java的LambdaQueryWapper的条件拼接实现数据sql中and (column1 =1 or column1 is null)
java·mybatis
程序猿麦小七21 分钟前
基于springboot的景区网页设计与实现
java·spring boot·后端·旅游·景区
蓝田~29 分钟前
SpringBoot-自定义注解,拦截器
java·spring boot·后端
theLuckyLong30 分钟前
SpringBoot后端解决跨域问题
spring boot·后端·python
A陈雷31 分钟前
springboot整合elasticsearch,并使用docker desktop运行elasticsearch镜像容器遇到的问题。
spring boot·elasticsearch·docker
.生产的驴32 分钟前
SpringCloud Gateway网关路由配置 接口统一 登录验证 权限校验 路由属性
java·spring boot·后端·spring·spring cloud·gateway·rabbitmq
小扳35 分钟前
Docker 篇-Docker 详细安装、了解和使用 Docker 核心功能(数据卷、自定义镜像 Dockerfile、网络)
运维·spring boot·后端·mysql·spring cloud·docker·容器
v'sir1 小时前
POI word转pdf乱码问题处理
java·spring boot·后端·pdf·word
李少兄1 小时前
解决Spring Boot整合Redis时的连接问题
spring boot·redis·后端
冰逸.itbignyi1 小时前
SpringBoot之AOP 的使用
java·spring boot