2mybatis use (annotation)

1pom depends on:

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


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

2yml file

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf-8&useSSL=false

3 objects



public class Category {
    
    
    private Integer id;


    private Integer parentId;


    private String name;


    private Boolean status;


    private Integer sortOrder;


    private Date createTime;


    private Date updateTime;

}

4dao

Insert picture description here

5 test

Insert picture description here
note:

5.1 Although the data can be found, some attributes are null. Because the object is in camel case, the database is underscore parent_id

Insert picture description here
Add configuration in the yml file:

mybatis:
  configuration:
    map-underscore-to-camel-case: true

Namely:
Mybatis defaults to one-to-one correspondence between attribute names and database field names, that is,
database table column: user_name
entity class attribute: user_name

But in java, camel case is generally used to name the
database table column: user_name
entity class attribute: userName

In Springboot, you can turn on the camel case function by setting the map-underscore-to-camel-case attribute to true.

5.2 If there are a lot of mappers, you need @mapper. You don't need to add each one, just add one in the startup category. @mapper can be omitted

Insert picture description here

5xml

5.1dao layer

Insert picture description here

5.2xml

Insert picture description here
Note:
1 The name is the fully qualified class name, otherwise an error will be reported.
2 When mapper and mapper.XML are not in the same directory, they need to be specified in the yml file

mybatis:
  configuration:
    map-underscore-to-camel-case: true # 对象和数据库字段对应
    # 控制台日志配置
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mappers/*.xml

3 another type of printing log
Insert picture description here

4 Extract the common part
Insert picture description here

Guess you like

Origin blog.csdn.net/Insist___/article/details/109100314