Integrated MyBatis of Spring-boot learning

Source: Internet
Author: User

I. About SPRING boot

1.spring boot in short is to make spring start easier, its motto is "Just run", most spring applications need only a few configurations, using spring-boot will greatly reduce the amount of code and XML configuration files that write spring-related

2. Normally spring-boot will look for application.properties or application.yml configuration files under Classpath, most of which will be configured in this configuration file.

Two Spring boot integrated MyBatis

Gradle configuration:

// gradle Configuration: compile ("Org.springframework.boot:spring-boot-starter-web:1.4.2.release") compile (' org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0 ') {        exclude group:' Org.springframework '     } compile ("mysql:mysql-connector-java:5.1.39")

APPLICATION.YML Configuration

Spring:    Profiles:      active:localmybatis:      configuration:       Log-impl: Org.apache.ibatis.logging.slf4j.Slf4jImpl       cache-enabled:true       interceptors:        - Com.github.pagehelper.PageHelper      Mapper-locations:classpath:mapper/*.xml

APPLICATION-LOCAL.YML Configuration

Spring:    DataSource:      username:root      Password:      driver-class-name:com.mysql.jdbc.driver      URL: Jdbc:mysql://localhost:3306/house?characterencoding=utf8server:  port:8082

Note that the configuration file is best in the root directory of Classpath

Three. Spring-boot Boot code:

 PackageCom.lyhs.machineparts.run;ImportCom.lyhs.machineparts.entity.User;ImportCom.lyhs.machineparts.mapper.UserMapper;ImportOrg.mybatis.spring.annotation.MapperScan;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.annotation.ComponentScan;Importjava.sql.SQLException;Importjava.util.List;/*** 1. Note that @mapperscan annotations must be labeled at Spring-boot to scan the Mapper interface and create its proxy object, which acts like Mapperscannerconfigure * 2. Spring-boot object pooling automatically creates Sqlsessionfactorybean and Sqlsessiontemplate objects based on configuration files * 3.spring-boot benefits omit most code and configuration files * Created by Niechen on 17/7/16. */@SpringBootApplication @componentscan ("Com.lyhs") @MapperScan (basepackages= "Com.lyhs.machineparts.mapper") Public classPortalprovider { Public Static voidMain (string[] args)throwsSQLException {applicationcontext ApplicationContext= Springapplication.run (Portalprovider.class, args); Usermapper Usermapper= Applicationcontext.getbean (usermapper.class); //System.out.println (housemapper.tostring ());list<user> users =Usermapper.selectall ();  for(User user:users) {System.out.println (User.getname ()); }    }}

Integrated MyBatis of Spring-boot learning

Related Article

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.