current position:Home>Spring boot integrates mybatis plus

Spring boot integrates mybatis plus

2022-01-26 23:36:59 A migrant worker carrying bricks

1.maven rely on

<!--mybatis-plus Of springboot Support ( Integrate mybatis-plus To put mybatis、mybatis-spring Get rid of , To avoid conflict )-->
 <!--mysql drive -->

2.yaml To configure

    url: jdbc:mysql://localhost:3306/haha?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # Turn on sql journal 
    map-underscore-to-camel-case: true #  This configuration is to map the underlined table fields to entity class attributes in Hump format 
    mapper-locations: classpath:mapper/*.xml # To configure xml Address  

3. Entity class

@TableName(value = "user") //mybatis-plus annotation 
public class UserEntity implements Serializable {
    private static final long serialVersionUID = 1903236519513043621L;

    private Long id;
    @ApiModelProperty(" name ")
    private String name;

    @ApiModelProperty(" Gender ")
    private Integer sex;

    @ApiModelProperty(" date ")
    private Date date;


dao Interface needs to inherit BaseMapper, Generics are entity classes

public interface UserDao extends BaseMapper<UserEntity> {

service Interface needs to inherit IService, Generics are entity classes

public interface UserService extends IService<UserEntity> {

serviceImpl Realization service Interface , And inheritance ServiceImpl class , The generics are DAO Interface and entity classes

public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements PersonService {


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-// Mapper 3.0//EN"
<mapper namespace="com.example.demo.dao.CacheTestDao">

notes : Remember to add @MapperScan(basePackages = “com.example.dao”) annotation , Or in each dao add @Mapper

copyright notice
author[A migrant worker carrying bricks],Please bring the original link to reprint, thank you.

Random recommended