1. 程式人生 > >spring boot 整合mybatis 通過官方mybatis-spring-boot-starter

spring boot 整合mybatis 通過官方mybatis-spring-boot-starter

pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cjwdemo.demoBySB</groupId>
<artifactId>demoBySB</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>demoBySB Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId
>
<artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
<version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring boot整合mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!--mysql jdbc驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> </dependencies> <build> <finalName>demoBySB</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

實體類

package com.cjwdemo.demoBySB.entity;

public class Userinfo {

    private Integer id;
    private String username;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

dao介面

package com.cjwdemo.demoBySB.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.cjwdemo.demoBySB.entity.Userinfo;

@Mapper
public interface UserinfoDao {

    @Select("SELECT id, username FROM userinfo WHERE username = #{username}")
    Userinfo findByUsername(@Param(value = "username") String username);

}

通過控制器呼叫

package com.cjwdemo.demoBySB.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjwdemo.demoBySB.dao.UserinfoDao;

@Controller
public class DemoController {

    @Autowired
    private UserinfoDao userinfoDao;

    // 測試路由器 http://localhost:9999/ 預設是8080,但我把他配置為9999了
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public String test() {
        return "hello spring boot";
    }

    // 測試路由器 http://localhost:9999/test2 預設是8080,但我把他配置為9999了
    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    @ResponseBody
    public String test2() {
        return userinfoDao.findByUsername("test").getUsername();
    }

}

測試準備的sql指令碼

CREATE DATABASE test CHARSET utf8mb4;
USE test;

CREATE TABLE userinfo (
id INT(11) AUTO_INCREMENT NOT NULL COMMENT '主鍵',
username VARCHAR(20) NOT NULL COMMENT '使用者名稱',
PRIMARY KEY (id)
)ENGINE=INNODB DEFAULT CHARSET utf8mb4 COMMENT '測試表';


INSERT INTO userinfo (username) VALUES('test');

官網教程

https://github.com/mybatis/spring-boot-starter/wiki/Quick-Start