springbootj集成mybatis(含注解)

1.新建一个springboot项目,项目结构如下:

2.pom.xml

<?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.java</groupId>
    <artifactId>springbootmybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springbootmybatis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>
pom.xml

3.在application.yml中配置数据源,数据库自己建一个db_mybatis

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

 

4.建模型Account.java

/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: Account
 * Author:   adm
 * Date:     2018/7/5 22:31
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.java.model;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author adm
 * @create 2018/7/5
 * @since 1.0.0
 */
public class Account {
    private int id;
    private String name;
    private int money;

    public Account(int id, String name, int money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

    public Account() {
        super();
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
Account.java

5.写dao层映射类AccountMapper.java

package com.java.dao;

import com.java.model.Account;
import org.apache.ibatis.annotations.*;
import org.springframework.core.annotation.Order;

import java.util.List;

@Mapper
public interface AccountMapper {
    @Select("select id, name as name, money as money from account where id = #{id}")
    Account findAccountById(@Param("id") int id);

    @Insert("INSERT INTO account(id,name,money) "
            + "VALUES(#{id}, #{name}, #{money})")
    void addAccount(Account account);

    @Update("UPDATE account SET name=#{name} WHERE id=#{id}")
    void updateAccount(Account account);

    @Select("SELECT * FROM account")
    List<Account> queryAccounts();
}

6.这就写完了,现在来写测试类

package com.java;

import com.java.dao.AccountMapper;
import com.java.model.Account;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootmybatisApplicationTests {
    @Resource
    private AccountMapper accountMapper;

    @Test
    public void test0_QueryAccountById() {
        Account account = accountMapper.findAccountById(1);
        System.out.println(account);
    }
    @Test
    public void test1_Add() {
        Account account=new Account(5,"hahhah",23);
        accountMapper.addAccount(account);
    }
    @Test
    public void test2_Update(){
        Account resultaccount = accountMapper.findAccountById(1);
        resultaccount.setName("niubi");
        accountMapper.updateAccount(resultaccount);
    }

    @Test
    public void test3_QueryOrders() {
        Assert.assertEquals(4, accountMapper.queryAccounts().size());
        List<Account> accounts = accountMapper.queryAccounts();
        for (Account account:accounts
             ) {
            System.out.println(account);
        }
    }

}

先看一下我的数据库表

运行test0,结果如下

剩下的就不测试了,自己测吧,测试完了,就可以写controller了,这样就能实现前后端数据交互了,不想写了,挺简单的,看一下springmvc就知道了

posted @ 2018-07-06 17:06  blogcheng  阅读(382)  评论(0编辑  收藏  举报