Springboot+Mybatis+lombok

Keywords: Mybatis JDBC xml Lombok

Note: here are some records of learning, I am not doing software development. Like here, we need to integrate springboot+spring+mybatis. We are still learning.

 

1, lombok configuration

(1) Plug in download

lombok offline download address: https://plugins.jetbrains.com/plugin/6317-lombok/versions

Download according to your version of idea

(2) Installation

(3) Introduce Maven dependency

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
</dependency>

 

2, Write profile

Spring boot core configuration file

server:
  port: 8888

logging:
  path: logs
  file: mylog.log

spring:
  application:
    name: myTest
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dmall?characterEncoding=utf-8
    username: root
    password: root

mybatis:
  type-aliases-package: org.example.mybatis.pojo
  mapper-locations:
    - mapper/*

mybatis core configuration file

<?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="org.example.mybatis.pojo"/>
    </typeAliases>
    <mappers>
        <mapper resource="mapper/RolesMapper.xml" />
        <mapper resource="mapper/PromoterMapper.xml" />
    </mappers>

    <!--&lt;!&ndash;read properties File configuration in&ndash;&gt;
    <properties resource="jdbc.properties">
        <property name="jdbc.username" value="root"/>
        <property name="jdbc.password" value="root"/>
    </properties>

    &lt;!&ndash;Configure package scanning&ndash;&gt;
    <typeAliases>
        <package name="org.example.mybatis.pojo"/>
    </typeAliases>

    &lt;!&ndash; and spring After integration environments Configuration will be abolished &ndash;&gt;
    <environments default="development">
        <environment id="development">
            &lt;!&ndash; Use jdbc transaction management &ndash;&gt;
            <transactionManager type="JDBC" />
            &lt;!&ndash; Database connection pool &ndash;&gt;
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments>

    &lt;!&ndash;Load mapping file&ndash;&gt;
    <mappers>
        <mapper resource="mapper/MemberMapper.xml" />
        <mapper resource="mapper/UserMapper.xml" />
        <mapper resource="mapper/RolesMapper.xml" />
        <package name="org.example.mybatis.mapper.UserMapper"/>
        <package name="org.example.mybatis.mapper.MemberMapper"/>
        <package name="org.example.mybatis.mapper.RolesMapper"/>
    </mappers>-->
</configuration>

Three, code

springboot starts. The application.class file needs to be placed in the package path.

package org.example;

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;

@SpringBootApplication
@EnableScheduling
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();
    }
}

java bean

package org.example.mybatis.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

@Data
public class Promoter {
    @ApiModelProperty(value = "Primary key id")
    private Integer id;

    @ApiModelProperty(value = "Trade name")
    private String shopName;

    @ApiModelProperty(value = "state")
    private Integer status;

    @ApiModelProperty(value = "Submission time")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date applyTime;

    @ApiModelProperty(value = "Transit time")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date passTime;

    @ApiModelProperty(value = "Remarks")
    private String remake;

}

Test class:

package org.example.mybatis.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.example.mybatis.pojo.Promoter;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Log4j
@RestController
@RequestMapping(value = "Promoter")
@Api(value = "Promoter", description = "Promoter Administration")
public class PromoterController {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @RequestMapping(value = "findAllPromoter", method = RequestMethod.GET)
    @ApiOperation(value = "Query all Promoter", httpMethod = "GET")
    public List<Promoter> findAllPromoter(){
        List<Promoter> allPromoter = sqlSessionTemplate.selectList("findAllPromoter");
        return allPromoter;
    }
}

Write Mapper file

<?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="org.example.mybatis.pojo">
    <select id="findAllPromoter" resultType="org.example.mybatis.pojo.Promoter">
        select * from dmall_promoter
    </select>
</mapper>

 

Published 31 original articles, won praise 7, visited 4736
Private letter follow

Posted by adam291086 on Wed, 29 Jan 2020 06:31:20 -0800