Small class D - Basics zero SpringBoot2.X to combat _ and integration Mybaties Affairs Section 8 of database operations explain _33, SpringBoot2.x real integration Mybatis3.x comment

notes


2, SpringBoot2.x integration Mybatis3.x Notes combat
    Description: SpringBoot2.x integration Mybatis3.x actual configuration notes

        1, starter, maven warehouse address: http: //mvnrepository.com/artifact/org.mybatis.spring.boot/ Spring-Boot--MyBatis Starter

        2, was added dependent (http://start.spring.io/ can download)
                    
            <! - introducing Starter ->
                    <dependency>
                        <the groupId> org.mybatis.spring.boot </ the groupId>
                        <the artifactId> MyBatis-Spring-Boot-Starter </ the artifactId>
                        <Version> 1.3.2 </ Version>
                        <scope> Runtime </ scope>                
                    </ dependency>
                     
             <-! JDBC driver package of the MySQL ->        
                     <dependency>
                        <the groupId> MySQL </ the groupId>
                        <the artifactId> MySQL-Connector-Java </ the artifactId>
                        <scope> Runtime </ scope>
                    </ dependency> 
            <-! is introduced third party data sources ->        
                    <dependency>
                        <the groupId> com.alibaba </ the groupId>
                        <the artifactId> Druid </ the artifactId>
                        <Version> 1.1.6 </ Version>
                    </ dependency>

        . 3, addition of the profile
            # mybatis.type-aliases-package = net.xdclass.base_project.domain
            # can automatically identify
            # spring.datasource.driver-class-name =com.mysql.jdbc.Driver

            = jdbc spring.datasource.url: MySQL: // localhost:? 3306 / Movie useUnicode to true = = UTF-8 & characterEncoding
            spring.datasource.username = root
            spring.datasource.password password =
            # If you do not use the default data source (com. zaxxer.hikari.HikariDataSource)
            spring.datasource.type = com.alibaba.druid.pool.DruidDataSource

        load configuration, etc. are injected into sqlSessionFactory springBoot help us to complete

        4 start class increase mapper scan
            @MapperScan ( "net.xdclass.base_project .mapper ")

             Tip: save the object, since access to the database by the above mentioned id 
             @Options (useGeneratedKeys = to true, the keyProperty =" the above mentioned id ", KeyColumn =" the above mentioned id ")

        4, developed mapper
            reference grammar http://www.mybatis.org/mybatis -3 / zh / java-api.HTML

        5, SQL script
            TABLE `user` the CREATE (
              ` id` int (. 11) unsigned the AUTO_INCREMENT the NOT NULL,
              `name` VARCHAR (128) the COMMENT the DEFAULT NULL 'name',
              ` phone` VARCHAR (16) the COMMENT the DEFAULT NULL 'mobile phone number',
              `the create_time `datetime DEFAULT NULL COMMENT 'creation time',
              ` age` int (4) the DEFAULT NULL the COMMENT 'Age',
              PRIMARY KEY ( `id`)
            ) ENGINE = InnoDB AUTO_INCREMENT = 18 the DEFAULT CHARSET = utf8;


        Related information:
        HTTP: // www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Configuration

        https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples

        integration set of questions:
            https://my.oschina.net/hxflar1314520/blog/1800035
            https://blog.csdn.net/tingxuetage/article/details/80179772

Start

This same general approach as relatively simple,

Mapper: database interface, database access interface layer that
service: business logic
util: Tools

user class

is automatically generated in the form of projects


generally do not have the latest version of the bug may be



the data source for general use Ali Baba druid data source. The following three dependent are added to the list


hot deployment also add to the mix the

added profile

default database connection pool. If the Druid Alibaba commented. Will use the default
here is connected to the user table in the movie database

Start scanning corresponding packet category plus

Scanning the packet mapper is equivalent dao layer mapper

complete package replication mapper classes that are






developed can refer to the official document mapper





primary key here is the self-energizing

get auto-incremented id

service is an interface, which defines methods

service the implementation class. getId is to increase access to post-primary key id

implementation class Service, remember the notes with @Service

controller


test

Start a program
code returned is 0. data is 45, data is the id of a primary key

and then save 46 is a

database 46 holds data

relating documents

Related information:
        http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Configuration

        https://github.com/mybatis/spring-boot-starter/tree/master/mybatis -spring-boot-samples

        integrated set of questions:
            https://my.oschina.net/hxflar1314520/blog/1800035
            https://blog.csdn.net/tingxuetage/article/details/80179772

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11425137.html