SpringBoot2.x integration Mybatis3.x comment

First introduced jar

            <!-- 引入starter-->
                    <dependency>
                        <groupId>org.mybatis.spring.boot</groupId>
                        <artifactId>mybatis-spring-boot-starter</artifactId>
                        <version>1.3.2</version>
                        <scope>runtime</scope>                
                    </dependency>
                     
             <!-- MySQL的JDBC驱动包    -->    
                     <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <scope>runtime</scope>             <-! is introduced third party data sources ->        
                    </ dependency> 

                    <dependency>
                        <groupId>com.alibaba</groupId>
                        <artifactId>druid</artifactId>
                        <version>1.1.6</version>
                    </dependency>

Configuration file

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

Start class increase mapper scan
            @MapperScan ( "com.boot. *. Mapper ") // mapper Note Scan those mapper com.boot. *. Path mapper mapper for the folder

@SpringBootApplication
@MapperScan("com.boot.*.mapper") // mapper 注释扫描那些mapper
public class TestBootApplication {

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

}

mapper file comments

@Repository
public interface UserMapper {
    @Select("select *  from user where id = #{id}")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    public User getId(String id);

}

@Options (useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") and the entity class specifying field is a field in the database

Here I mapper annotation is used @Repository In fact, here you can also use @mapper

The two differ

@Repository scan address needs to be configured in the Spring, the Bean can then generate Dao Service layer are injected into the layer.

@Mapper not need to configure a scan address, the address through the interface inside the xml namespace inside, generated after injection into the Bean Service layer.

 

 

Let the console operator to print sql statement added in the configuration file

logging:
  level:
    com.boot.test.mapper: debug

com.boot.test.mappe this is the mapper file storage folder

 

Published 10 original articles · won praise 0 · Views 512

Guess you like

Origin blog.csdn.net/DNCCCC/article/details/105032466