java introductory 016-spring boot 2 combined with mybatis, xml-free configuration

Posted by superstar on Wed, 24 Jul 2019 07:17:06 +0200

In the last section, we talked about how spring boot 2 combines mybatis to add, delete and check mysql data, but we need to use xml configuration. Once xml configuration is involved, it will be more troublesome. Today, let's talk about a new method, which does not need to set up xml files, and the code looks more concise.

First, introduce mybatis and database connection dependencies


Complete pom.xml is posted to you

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shitou</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--        mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>LATEST</version>
        </dependency>
        <!--        Database Link Dependence-->
        <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>
            </plugin>
        </plugins>
    </build>

</project>

Second, configure database connection


Here my database is mysql database, using test2 library.

3. Establishment of database and tables

Create test2 database

create database test2 default character set utf8 collate utf8_general_ci;

Create user2 tables

CREATE TABLE user2(
    id   int(32) auto_increment   NOT NULL PRIMARY KEY,
    name VARCHAR(32) comment 'Full name' NOT NULL,
    age  VARCHAR(32) comment 'Age' NOT NULL
) COMMENT 'User table' CHARACTER SET utf8 COLLATE utf8_general_ci;

Fourth, create entity classes corresponding to data tables

Fifth, create mapper of operation database


The code is posted to you.

@Mapper
public interface User2Mapper {
    @Insert("insert into user2(name, age) values(#{name}, #{age})")
    int add(@Param("name") String name, @Param("age") int age);

    @Update("update user2 set name = #{name}, age = #{age} where id = #{id}")
    int update(@Param("name") String name, @Param("age") int age, @Param("id") int id);

    @Delete("delete from user2 where id = #{id}")
    int delete(int id);

    @Select("select id, name as name, age as age from user2 where id = #{id}")
    User2 findOne(@Param("id") int id);

    @Select("select * from user2")
    List<User2> findAll();
}

Sixth, create service and controller

The service code is as follows

@Service
public class User2Service {

    @Resource
    private User2Mapper accountMapper;

    public int add(String name, int age) {
        return accountMapper.add(name, age);
    }

    public int update(String name, int age, int id) {
        return accountMapper.update(name, age, id);
    }

    public int delete(int id) {
        return accountMapper.delete(id);
    }

    public User2 findAccount(int id) {
        return accountMapper.findOne(id);
    }

    public List<User2> findAccountList() {
        return accountMapper.findAll();
    }
}

The controller code is as follows

@RestController
@RequestMapping("/mybatis")
public class User2Controller {

    @Resource
    User2Service accountService;

    @GetMapping("/list")
    public List<User2> getAccounts() {
        return accountService.findAccountList();
    }

    @GetMapping("/findone")
    public User2 getAccountById(@RequestParam("id") int id) {
        return accountService.findAccount(id);
    }

    @GetMapping("/update")
    public String updateAccount(@RequestParam("id") int id,
                                @RequestParam(value = "name") String name,
                                @RequestParam(value = "age") int age) {
        int t = accountService.update(name, age, id);
        if (t == 1) {
            return "success";
        } else {
            return "fail";
        }

    }

    @GetMapping("/delete")
    public String delete(@RequestParam(value = "id") int id) {
        int t = accountService.delete(id);
        if (t == 1) {
            return "success";
        } else {
            return "fail";
        }

    }

    @GetMapping("/add")
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "age") int age) {
        int t = accountService.add(name, age);
        if (t == 1) {
            return "success";
        } else {
            return "fail";
        }
    }
}

7. Start the project and verify it

Start the spring boot project

Add data. As shown below, we add two data by adding

Query all data through list

Update and delete data, we will not show you.

Source code:

https://github.com/qiushi123/springboot-demos

Video Interpretation

https://edu.csdn.net/course/detail/23443

Recent Review

Topics: Java Spring Mybatis Database