Spring Boot mybatis-starter Introduction

1, mybatis-starter effect

Automatic detection project DataSource

Create and register instances SqlSessionFactory

Created and registered SqlSessionTemplate examples

Automatic scanning mappers

 

2, mybatis-starter using

1) introducing mybatis-spring-boot-starter

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.1</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.48</version>
		</dependency>

  

2) application.properties increase the database configuration file

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  

3) an increase in the pom.xml generated xml database plugins. Depends on mysql-connector-java the jar package

<build>
		<plugins>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.48</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

  

4) an increase in resources generatorConfig.xml file folder

<? xml Version = "1.0" encoding = "UTF-8"?> 
<DOCTYPE generatorConfiguration! 
        the PUBLIC "- // mybatis.org//DTD the Configuration MyBatis Generator 1.0 // EN" 
        "http://mybatis.org/dtd /mybatis-generator-config_1_0.dtd "> 

<generatorConfiguration> 
    <context ID =" testTables "targetRuntime =" MyBatis 3 "> 
        <commentGenerator> 
            <- true if removed automatically generated annotation:! is: to false: NO -> 
            < name = Property "suppressAllComments" value = "to true" /> 
        </ commentGenerator> 
        <- database connections:! driver class, connection address, user name, password -> 
        <JdbcConnection driverClass = "com.mysql.jdbc. Driver "
                        connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" userId="root"
                        password="123456">
        </jdbcConnection> 
        <-! default false, the JDBC DECIMAL and NUMERIC type resolution as Integer, when the true JDBC DECIMAL and 
            NUMERIC type resolution is java.math.BigDecimal -> 
        <javaTypeResolver> 
            <Property name = " forceBigDecimals "value =" to false "/> 
        </ javaTypeResolver> 

        <- targetProject:! PO class generating location -> 
        <javaModelGenerator targetPackage =" com.example.demo.bean " 
                            targetProject =" the src / main / Java "> 
            <-! enableSubPackages: whether to allow schema package as a suffix -> 
            <Property name = "enableSubPackages" = value "false" /> 
            <!- the value returned from the database spaces before and after being cleaned -> 
            <Property name = "trimStrings" value = "to true" />
        </ javaModelGenerator> 
        <-! specified database table ->
        <- targetProject:! Mapper mapping file generated location ->
        <sqlMapGenerator targetPackage = "Mapper" 
                         targetProject = "src / main / Resources"> 
            <- enableSubPackages:! whether to allow schema package as a suffix -> 
            <Property name = "enableSubPackages" = value "false" /> 
        </ sqlMapGenerator > 
        <- targetPackage:! Mapper interfaces generated position -> 
        <javaClientGenerator type = "XMLMAPPER" 
                             targetPackage = "com.example.demo.mapper" 
                             targetProject = "the src / main / Java"> 
            <- enableSubPackages:! Are let schema package as a suffix -> 
            <Property name = "enableSubPackages" = value "to false" />
        </javaClientGenerator>
        <table schema=""  tableName="test"></table>

    </context>
</generatorConfiguration>

 

5) Configure mapperScan

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class Sb2Application { 
}

  

6) arranged in an increase in application.properties

mybatis.mapper-locations=classpath:mapper/*.xml

  

7), mybatis.configuration.map-underscore-to-camel-case = true underscore converted camelCasing

mybatis.type-aliases-package=com.example.demo.bean
mybatis.configuration.map-underscore-to-camel-case=true

  

 8) increase the test class

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Sb2Application.class)
public class DemoApplicationTest  {

    @Autowired
    private TestMapper testMapper;


    @Test
    public void testInsert(){
        com.example.demo.bean.Test test = new com.example.demo.bean.Test();
        test.setId(100);
        test.setName("Nick Wang");
        testMapper.insert(test);
    }



}

  

9) run the test class. Results in success

 

 It has added a data table

 

10) condition updates.

The name was changed to Nick Wang Larry

    @Test
    public void testWhereUpdate(){
        com.example.demo.bean.Test test = new com.example.demo.bean.Test();
        test.setName("Larry");
        TestExample testExample = new TestExample();
        testExample.createCriteria().andNameEqualTo("Nick Wang");
        testMapper.updateByExampleSelective(test, testExample);
    } 

FIG updated following results

 

3, supplement

Apart from scanning with MapperScan, there is a way

 

 Mapper is TestMapper Add comment

 

Guess you like

Origin www.cnblogs.com/linlf03/p/12444764.html