Spring Boot Tutorial-Spring boot integrated mybatis (annotation configuration)

Source: Internet
Author: User
Tags spring boot tutorial

Previous article: Spring Boot Tutorial-Spring boot Integrated MyBatis (XML) describes the spring boot integration mybatis XML-based configuration, this article focuses on annotation mode configuration mybatis. Development Environment JDK 1.8 Maven 3.3 Spring Boot 1.5.8.RELEASE Mybatis 3.4.4 configuration

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>

Add MyBatis dependency:

    <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

Server:
  port:9000

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

Mybatisconfiguration

Package springboot.tutorials.mybatis.annotation.config;
Import Com.alibaba.druid.pool.DruidDataSource;
Import Org.apache.ibatis.session.SqlSessionFactory;
Import Org.mybatis.spring.SqlSessionFactoryBean;
Import Org.mybatis.spring.annotation.MapperScan;
Import Org.springframework.beans.factory.annotation.Qualifier;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.core.io.support.PathMatchingResourcePatternResolver;
Import Org.springframework.jdbc.datasource.DataSourceTransactionManager;

Import Javax.sql.DataSource; 
        /** * @author Ricky Fung */@Configuration @MapperScan (basepackages = "Springboot.tutorials.mybatis.annotation.mapper", Sqlsessionfactoryref = "Sqlsessionfactory") public class Mybatisconfiguration {static final String mapper_l

    ocation = "Classpath:mapper/*.xml"; @Value ("${spring.datasource.url}") PrivaTe String url;

    @Value ("${spring.datasource.username}") private String user;

    @Value ("${spring.datasource.password}") private String password;

    @Value ("${spring.datasource.driver-class-name}") Private String Driverclass; @Bean (name = "Sqlsessionfactory") public sqlsessionfactory sqlsessionfactory (@Qualifier ("DataSource") DataSource data Source) throws Exception {final Sqlsessionfactorybean Sqlsessionfactorybean = new Sqlsessionfactorybe
        An ();
        Sqlsessionfactorybean.setdatasource (DataSource); Sqlsessionfactorybean.setmapperlocations (New Pathmatchingresourcepatternresolver (). GetResources (MAPPER_LO
        cation));
    return Sqlsessionfactorybean.getobject (); } @Bean (name = "DataSource") public DataSource DataSource () {Druiddatasource DataSource = new Druiddatas
        Ource ();
        Datasource.setdriverclassname (Driverclass);
        Datasource.seturl (URL); Datasource.setusername (user);
        Datasource.setpassword (password);
    return dataSource; } @Bean (name = "TransactionManager") public Datasourcetransactionmanager TransactionManager () {return NE
    W Datasourcetransactionmanager (DataSource ());
 }
}

The MyBatis mapper classes and mapper files involved are recommended to be generated by Mybatis-generator from the start, simple and efficient.

Launch class App

Package springboot.tutorials.mybatis.annotation;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Ricky Fung *
 *
@SpringBootApplication public
class App {public

    static void main ( String[] args) {
        springapplication.run (app.class, args);
    }
}
Source Code

Portal: Spring-boot-mybatis3-annotation-config

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.