Mybatis-Plus 基础项目配置代码

导入相关的依赖

这里用到了阿里的数据库连接池

lombok


    com.baomidou
    mybatis-plus-boot-starter
    3.5.1


    com.alibaba
    druid-spring-boot-starter
    1.2.11


    mysql
    mysql-connector-java

springboot yaml配置

spring:
  datasource:
    username: 账号
    password: 密码
    url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&sercerTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
# 配置
mybatis-plus:
  type-aliases-package: com.mybatisplus.pojo
  mapper-locations: classpath*:/mapper/**/*.mapper

实体类

lombok注解get/set方法

package com.mybatisplus.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

@Data
@EqualsAndHashCode(callSuper = false)
public class Department implements Serializable  {
    private static final long serialVersionUID = 1L;
    @TableId(value = "deptno", type = IdType.AUTO)
    private Integer deptno;
    private String dname;
    private String loc;
}

service

package com.mybatisplus.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.mybatisplus.pojo.Department;

public interface DepartmentService extends IService {
}

实现类

package com.mybatisplus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mybatisplus.mapper.DepartmentMapper;
import com.mybatisplus.pojo.Department;
import com.mybatisplus.service.DepartmentService;
import org.springframework.stereotype.Service;

@Service
public class DepartmentServiceImpl extends ServiceImpl implements DepartmentService {
}

启动类扫描注解@MapperScan("com.mybatisplus.mapper")

package com.mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.mybatisplus.mapper")
@SpringBootApplication
public class MybatisPlusTaskApplication {

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

}

你可能感兴趣的:(java,mybatis)