Use mybatis + mapper springboot

First introduced pom-related

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>

New MyMapper class

package com.chao.notify.mapper.base;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {

}

Uuid primary key for the new UUIdGenId

public class UUIdGenId implements GenId<String> {
    @Override
    public String genId(String s, String s1) {
        return UUID.randomUUID().toString().replace("-","");
    }
}

New model class

@Table(name = "t_user")
public class TUser {

    @Id
    @KeySql(genId = UUIdGenId.class)
    private String id;

    @Column(unique = true)
    private String username;

    private String password;

    private String salt;

    private String name;
    /**
     * 0 未知
     * 1 男
     * 2 女
     */
    private Integer sex;

    private String phone;

    private String address;

    private String notes;

    /**
     * Disabled 
     * true to disable 
     * false enable 
     * / 
    Private Boolean Disabled; 

    / ** 
     * User Roles 
     * admin Administrator 
     * public ordinary user 
     * / 
    Private String Role; 

    / ** 
     * Create a time 
     * / 
    Private a Date AT; 

    / ** 
     * login time 
     * / 
    Private a Date loginAt; 

    public String getId () {
         return ID; 
    } 

    public  void the setId (String ID) {
         the this .id = ID; 
    } 

    public String the getUsername () {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Date getAt() {
        return at;
    }

    public void setAt(Date at) {
        this.at = at;
    }

    public Date getLoginAt() {
        return loginAt;
    }

    public void setLoginAt(Date loginAt) {
        this.loginAt = loginAt;
    }
}

New UserMapper class

@Mapper
public interface UserMapper extends MyMapper<TUser> {

}

application.properties Configuration

. spring.datasource.driver-class-name = com.mysql.cj.jdbc Driver 
the Spring .datasource.url = jdbc: MySQL: // 127.0 . 0.1 : 3306 / serverTimezone the Test = Asia / on Shanghai & useSSL =? false 
the Spring .DataSource. = username root 
the Spring .datasource.password = root 
the Spring .datasource.type = com.alibaba.druid.pool. DruidDataSource 

# initialize the number of links the minimum number of free links maximum number of links 
spring.datasource.druid.initial-size = 5 
the Spring .DataSource = IDLE-.druid.min . 5 
Spring .datasource.druid.max-Active = 20 is 
# timeout acquired connection milliseconds
spring.datasource.druid.max- the wait = 60000 
# intervals the frequency of such detection, an idle connection is detected to be closed, in milliseconds 
spring.datasource.druid. Time -between the runs---eviction of millis = 60000 



MyBatis .Type -aliases- Package Penalty for = com.chao.notify. Model 
Mapper .mappers = com.chao.notify.mapper.base.MyMapper

 

Guess you like

Origin www.cnblogs.com/rchao/p/11018683.html