Spring Boot Tutorial-Spring boot integrated MyBatis (XML mode)

Source: Internet
Author: User
Tags spring boot tutorial

Project development inevitably needs to deal with the database, the author developed a project widely used MyBatis as an ORM framework.
This article explains how to integrate MyBatis in the Spring Boot project, and configure the annotation-based configuration in the next article based on the XML configuration. Development Environment JDK 1.8 Maven 3.3 Spring Boot 1.5.8.RELEASE Mybatis 3.4.4 code

First introduce spring-boot-starter-parent:

    <!--Inherit defaults from Spring Boot--
    <parent>
        <groupid>org.springframework.boot</ groupid>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8. Release</version>
    </parent>

MyBatis dependencies:

    <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis -<dependency> <groupId>org.mybatis.spring.boot</groupId> <artif Actid>mybatis-spring-boot-starter</artifactid> <version>1.3.1</version> </depe
            ndency> <!--db--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.version}</ver
            sion> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> & Lt;/dependency> &Lt;/dependencies> 

Application.yml

MyBatis:
    mapper-locations:classpath:mapper/*.xml
    Type-aliases-package:springboot.mybatis.model
    Type-handlers-package:springboot.mybatis.typehandler
    configuration:
        map-underscore-to-camel-case:true
        default-fetch-size:100

Spring:
    DataSource:
       url:jdbc:mysql://localhost:3306/demo?useunicode= True&characterencoding=utf-8&zerodatetimebehavior=converttonull
       username:root
       password:root
       Driver-class-name:com.mysql.jdbc.driver

The MyBatis mapper classes and mapper files involved are recommended for production through mybatis-generator, simple and efficient.

Launch class App

Package springboot.mybatis.xml;

Import Org.mybatis.spring.annotation.MapperScan;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * ${description}
 *
 * @author Ricky Fung
 *
/@SpringBootApplication @MapperScan ( Basepackages = "Com.mindflow.springboot.mybatis.mapper") public
class App {public

    static void Main (string[] args) {

        springapplication.run (app.class, args);
    }
}

Finally, just add @MapperScan annotations to the app class. Source Code

Portal: Spring-boot-mybatis3-xml-config View Source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.