MyBatis test operation of the database # This section Video

Database operations using tk.mybatis

After a series of configuration before the chapter, we have met the necessary conditions for the use of MyBaits operation of the database, the following is an example of using tk.mybatis operation of the database.

We have to test the operation of the user table, for example (tb_user)

# Modify an entry class

Need to use  @MapperScan annotations to specify the path Mapper interface

PS: Note that the  @MapperScan annotations are  tk.mybatis.spring.annotation.MapperScan; packages under

package com.snake.hello.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan(basePackages = "com.funtl.hello.spring.boot.mapper")
public class HelloSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloSpringBootApplication.class, args);
    }
}

 

# Create a test class

package com.snake.hello.spring.boot;

import com.funtl.hello.spring.boot.entity.TbUser;
import com.funtl.hello.spring.boot.mapper.TbUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
importorg.springframework.transaction.annotation.Transactional;
 Import tk.mybatis.mapper.entity.Example; 

Import java.util.Date;
 Import java.util.List; 

@RunWith (. SpringRunner class ) 
@SpringBootTest (classes = HelloSpringBootApplication. class ) 
@Transactional 
@Rollback 
public  class MyBatisTests { 

    / ** 
     * injection data query interface 
     * / 
    @Autowired 
    Private TbUserMapper tbUserMapper; 

    / ** 
     * test insertion data 
     * / 
    @Test 
    public  void testInsert () {
         //A test data structure 
        TbUser tbUser = new new TbUser (); 
        tbUser.setUsername ( "Lusifer" ); 
        tbUser.setPassword ( "123456" ); 
        tbUser.setPhone ( "15,888,888,888" ); 
        tbUser.setEmail ( "[email protected]. COM " ); 
        tbUser.setCreated ( new new a Date ()); 
        tbUser.setUpdated ( new new a Date ()); 

        // insert data 
        tbUserMapper.insert (tbUser); 
    } 

    / ** 
     * delete test data 
     * / 
    @Test 
    public  void TestDelete () {
         //Structural conditions, equivalent to the WHERE username = tb_user from the DELETE 'Lusifer' 
        Example Example = new new Example (TbUser. Class ); 
        . Example.createCriteria () andEqualTo ( "username", "Lusifer" ); 

        // delete the data 
        tbUserMapper.deleteByExample ( Example); 
    } 

    / ** 
     * test modified data 
     * / 
    @Test 
    public  void testUpdate () {
         // structural conditions 
        Example Example = new new Example (TbUser. class ); 
        . example.createCriteria () andEqualTo ( "username", "Lusifer " ); 

        // construct a test data
        TbUser tbUser = new TbUser();
        tbUser.setUsername("LusiferNew");
        tbUser.setPassword("123456");
        tbUser.setPhone("15888888888");
        tbUser.setEmail("[email protected]");
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());

        // 修改数据
        tbUserMapper.updateByExample(tbUser, example);
    }

    /**
     * 测试查询集合
     */
    @Test
    public void testSelect() {
        List<TbUser> tbUsers = tbUserMapper.selectAll ();
         for (TbUser tbUser: tbUsers) { 
            System.out.println (tbUser.getUsername ()); 
        } 
    } 

    / ** 
     * Test paging query 
     * / 
    @Test 
    public  void TestPage () {
         // PageHelper very simple, just set the page number and page display items can 
        PageHelper.startPage (0, 2 ); 

        // set page query 
        Example Example = new new Example (TbUser. class ); 
        PageInfo <TbUser> = PageInfo new new PageInfo <> (tbUserMapper.selectByExample (Example)); 

        //Acquiring a query result 
        List <TbUser> tbUsers = pageInfo.getList ();
         for (TbUser tbUser: tbUsers) { 
            System.out.println (tbUser.getUsername ()); 
        } 
    } 
}

 

 Attachment: Complete POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.funtl</groupId>
    <artifactId>hello-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hello-spring-boot</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.funtl.hello.spring.boot.HelloSpringBootApplication</mainClass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.4</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

 

Guess you like

Origin www.cnblogs.com/snake107/p/11915003.html
Recommended