Practice Notes on 2: SpringBoot Integration MyBatis

This paper describes the initial integration process SpringBoot MyBatis and details that need attention.

  1. SpringBoot Profile

Official website https://spring.io/projects/spring-boot/

 

  1. Introduction to MyBatis

Official website: https://mybatis.org/mybatis-3/zh/index.html

MyBatis advantages:

  1. History development

    MyBatis is Ibatis evolved, Ibatis1.x and Ibatis2.x, are called Ibatis, in Ibatis3.x and later versions are called: MyBatis

    1. Position in the three-tier system software

    MyBatis located three Dao layer software architecture that MyBatis mainly interact with the database!

    MyBatis in the end is what?

    What is MyBatis?

    MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.

    MyBatis is a first class persistence framework, support for custom SQL , stored procedures and advanced mappings. MyBatis eliminates almost all JDBC manually set parameters and codes as well as result of the search. MyBatis can use simple XML or annotations configuration and primitives, mapping interface and Java POJOs (simple Java objects, is in fact general the JavaBeans ) mapped to the database record.

  1. MyBatis advantage [focus]

    MyBatis and native JDBC, Spring of JdbcTemplate compared to what advantage?

    1. MyBatis itself is a frame except for the additions and deletions to change search operation data table, also supported field mapping, caching mechanism.
    2. MyBatis supports dynamic SQL [according to different parameters, can be spliced ​​into different SQL]
    3. MyBatis supports java code [] to focus on business logic and SQL statements [focusing on] the separation process data
    4. MyBatis can also link directly mapped to relational tables relationship POJO objects.
    5. MyBatis is a semi-automatic (hand-written SQL) The ORM] [Object Relation Mapping Framework.

     

    1. Operating principle

      1. other

      On specific cases, SSM integration, reverse engineering, detailed write free articles ... not much go into details here.

       

      1. Integration steps

        1. Prepare the database and tables

      1. Import dependence

      Introducing mybatis, MySQL, maven dependent AlibabaDruid connection pool

      <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.0</version>
      </dependency>
      <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      </dependency>
      <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
      </dependency>

       

      1. Modify SpringBoot profile for application.yml

       

      #配置数据源
      spring:
      datasource:
      url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
      type: com.alibaba.druid.pool.DruidDataSource
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
      #springboot整合mybatis
      mybatis:
      mapper-locations: classpath:mapper/*.xml

      type-aliases-package: com.kjlw.entity

       

      1. Creating the Entity Classes

       

      1. Mapper interface and create a layer mapping file

      1. Create a Service layer interface and implementation class

      1. Creating Controller Layer Interface

      1. Add annotations scan on SpringBoot main startup class

       

      1. Start and test

      slightly

       

      1. Precautions

      1. Traditional ssm integration Mapper interface layer mapping file should be in the same package (usually Mapper package), but SpringBoot with MyBatis integration, the Mapper interface if the default mapping files are placed in the same package under, still error, this time there are two solutions:

        1) establish the same package name in the resources directory and map files in

        2) Add the following configuration in the pom file:

Guess you like

Origin www.cnblogs.com/hjwg8/p/11802540.html