Rewrite database service example using springboot+mybatis

Posted by flunn on Tue, 30 Jul 2019 20:03:05 +0200

In the interface test framework I wrote before, MySQL was used to record various requests, responses, use cases, and so on. To improve the storage speed, I wrote a separate database storage service and deployed it on the servers on the internal network.When there is information that needs to be recorded, the information is sent directly to the fixed interface of this service, which enables asynchronous storage of the database.After learning the springboot and mybatis frameworks, I feel like using mybatis to write this function again.Since the code for the previous service is preserved, only the implementation code for the new function is shared below.

Here is the code for the springboot startup class:

package com.fun;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@MapperScan("com.fun.dao")//Scan Package Underneath Interface
@SpringBootApplication(exclude ={MongoAutoConfiguration.class})
public class ApiTestMysqlserviceApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ApiTestMysqlserviceApplication.class, args);
		System.out.println("-----------------------------------start-----------------------------------");
	}
}

Here is the code for userDao and userService:

package com.fun.dao;
 
 
import com.fun.model.RequestBean;
 
public interface UserDao {
 
    int insertRequest(RequestBean requestBean);
}

package com.fun.user.impl;
 
import com.fun.dao.UserDao;
import com.fun.model.RequestBean;
import com.fun.model.UserDomain;
import com.fun.user.UserService;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service(value = "userService")
public class UserServiceImpl implements UserService {
 
    @Override
    public int insertRequest(RequestBean requestBean) {
        return userDao.insertRequest(requestBean);
    }
 
}

package com.fun.user;
 
import com.fun.model.RequestBean;
import com.fun.model.UserDomain;
 
public interface UserService {
 
    int insertRequest(RequestBean requestBean);
}

    @PostMapping("/test")
    @ResponseBody
    public ResultUtil test( RequestBean requestBean) {
        logger.info(requestBean.toString());
        int i = userService.insertRequest(requestBean);
        return ResultUtil.build(i);
    }

The following is the configuration of mapper.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.fun.dao.UserDao">
    <sql id="REQUEST_TABLE">
        request
    </sql>
 
    <insert id="insertRequest" parameterType="com.fun.model.RequestBean">
        INSERT INTO
        <include refid="REQUEST_TABLE"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            domain,api,type,expend_time,data_size,status,code,method,local_ip,local_name,create_time,
        </trim>
        <trim prefix="VALUES(" suffix=")" suffixOverrides=",">
            #{domain},#{api},#{type},#{expend_time},#{data_size},#{status},#{code},#{method},#{local_ip},#{local_name},#{create_time}
        </trim>
    </insert>
</mapper>

The following is the project's property configuration:

spring.datasource.url=jdbc:mysql://****:3306/fan?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 
 
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.fun.model
 
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.returnPageInfo=check

Here is the information stored in the database:

Welcome to interesting children's shoes

Topics: Programming Mybatis Spring MySQL Database