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 )-->
<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.4.3.1</version>
 </dependency>
 <!--mysql drive -->
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
 </dependency>

2.yaml To configure

spring:
  datasource:
    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

mybatis-plus:
  configuration:
    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

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

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

    @ApiModelProperty(" Gender ")
    private Integer sex;

    @ApiModelProperty(" date ")
    private Date date;

}

4.DAO
dao Interface needs to inherit BaseMapper, Generics are entity classes

public interface UserDao extends BaseMapper<UserEntity> {
    
}

5.service
service Interface needs to inherit IService, Generics are entity classes

public interface UserService extends IService<UserEntity> {
    
}

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

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

7.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.example.demo.dao.CacheTestDao">
</mapper>

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.
https://en.cdmana.com/2022/01/202201262336570023.html

Random recommended