Spring Boot整合MyBatis

最近剛做完jpa的測(cè)試,因?yàn)閖pa跟hibernate關(guān)聯(lián)的比較緊密,mybatis由于其靈活的sql配置,深得人心,所以我研究了一下spring boot怎么和mybatis整合,現(xiàn)在寫(xiě)一篇文章來(lái)記錄所學(xué),不得不說(shuō),spring boot真是太強(qiáng)大了,整合起來(lái)也非常簡(jiǎn)單,talk is cheap, show me the code!!

1.引入mybatis

<?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.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xuzhang</groupId>
    <artifactId>spring-boot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.由于我用的是h2來(lái)進(jìn)行測(cè)試,所以我需要?jiǎng)?chuàng)建sql文件來(lái)初始化表

init.sql

CREATE TABLE IF NOT EXISTS `user` (
    `id`         INTEGER  PRIMARY KEY AUTO_INCREMENT,
     `name` VARCHAR(50) NOT NULL,
     `pwd`        varchar(50) not null
);

application.properties

初始化數(shù)據(jù)表用的
spring.datasource.schema=classpath:init.sql

3.在啟動(dòng)類添加注解(@MapperScan)指明你要掃描的包。

package com.xuzhang.springbootmybatis;

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

@SpringBootApplication
@MapperScan("com.xuzhang.springbootmybatis.mappper")
public class SpringBootMybatisApplication {

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

}

4.創(chuàng)建實(shí)體

package com.xuzhang.springbootmybatis.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}

5.創(chuàng)建mapper

package com.xuzhang.springbootmybatis.mappper;

import com.xuzhang.springbootmybatis.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from user")
    List<User> findAllUser();

    @Insert("insert into user(name,pwd) values(#{name},#{pwd})")
    void insertUser(@Param("name") String name, @Param("pwd") String pwd);

    @Delete("delete from user where id = #{id}")
    void deleteUser(@Param("id") int id);
}

6.創(chuàng)建controller來(lái)進(jìn)行測(cè)試

package com.xuzhang.springbootmybatis.controller;

import com.xuzhang.springbootmybatis.mappper.UserMapper;
import com.xuzhang.springbootmybatis.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("user/findAllUser")
    public List<User> findALlUser() {
        return userMapper.findAllUser();
    }

    @PostMapping("user/insertUser")
    public void insertUser(@RequestBody User user) {
        userMapper.insertUser(user.getName(), user.getPwd());
    }

    @PostMapping("user/deleteUser")
    public void deleteUser(@RequestParam(value = "id") int id) {
        userMapper.deleteUser(id);
    }
}

7.測(cè)試結(jié)果

  • 添加用戶


    addUser
  • 查找用戶


    findUser
  • 刪除用戶


    deleteUser

git:https://gitee.com/xuzhangRIchard/spring-boot-mybatis

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容