Spring boot Mybatis 整合(注解版)

Spring boot Mybatis 整合(注解版)更多干货


Spring Boot快速入门 

Spring Boot开发Web应用 

Spring Boot工程结构推荐 

Spring Boot构建RESTful API与单元测试 

Spring Boot中使用Swagger2构建强大的RESTful API文档 

Spring Boot中使用JdbcTemplate访问数据库 

Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅 

Spring Boot多数据源配置与使用 

Spring Boot日志管理 

Spring Boot中使用Redis数据库 

Spring Boot中使用MongoDB数据库 

Spring Boot中Web应用的统一异常处理 

Spring Boot属性配置文件详解 

Spring Boot中使用@Scheduled创建定时任务 

Spring Boot中使用@Async实现异步调用 

Spring boot Mybatis 整合(注解版) 

springboot事务管理详解 

springboot中使用Mybatis注解配置详解


正题

原创  2017年11月24日 10:39:57
  • 21023

之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦。接口定义和映射离散在不同的文件中,阅读起来不是很方便。于是,准备使用mybatis的注解方式实现映射。如果喜欢xml方式的可以看我之前的博文: Spring boot Mybatis 整合(完整版)

源码

请前往文章末端查看

开发环境:


  • 开发工具:Intellij IDEA 2017.1.3
  • JDK : 1.8.0_101
  • spring boot 版本 : 1.5.8.RELEASE
  • maven : 3.3.9

拓展:

  • springboot 整合 Mybatis 事务管理

开始

1.新建一个springboot项目:

这里写图片描述

这里写图片描述

添加依赖 
这里写图片描述

2.看一下项目结构

这里写图片描述

3.完整依赖
<?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.winterchen</groupId>
    <artifactId>springboot-mybatis-demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-mybatis-demo2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
4.配置文件

因为习惯性的喜欢使用yml作为配置文件,所以将application.properties替换为application.yml

spring:
  datasource:
     url: jdbc:mysql://127.0.0.1:3306/mytest
     username: root
     password: root
     driver-class-name: com.mysql.jdbc.Driver
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

简单且简洁的完成了基本配置,下面看看我们是如何在这个基础下轻松使用Mybatis访问数据库的

使用Mybatis


  • 在Mysql数据库中创建数据表:
CREATE DATABASE mytest;

USE mytest;

CREATE TABLE t_user(
  id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL ,
  password VARCHAR(255) NOT NULL ,
  phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 创建映射对象User
package com.winterchen.domain;

/**
 * User实体映射类
 * Created by Administrator on 2017/11/24.
 */

public class User {

    private Integer id;
    private String name;
    private String password;
    private String phone;

    //省略 get 和 set ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作
package com.winterchen.mapper;

import com.winterchen.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * User映射类
 * Created by Administrator on 2017/11/24.
 */
@Mapper
public interface UserMapper {

    @Select("SELECT * FROM T_USER WHERE PHONE = #{phone}")
    User findUserByPhone(@Param("phone") String phone);

    @Insert("INSERT INTO T_USER(NAME, PASSWORD, PHONE) VALUES(#{name}, #{password}, #{phone})")
    int insert(@Param("name") String name, @Param("password") String password, @Param("phone") String phone);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

如果想了解更多Mybatis注解的详细:springboot中使用Mybatis注解配置详解

  • 创建springboot 主类:
package com.winterchen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootMybatisDemo2Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisDemo2Application.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 创建测试单元: 
    • 测试逻辑:插入一条name为”weinterchen”的User,然后根据user的phone进行查询,并判断user的name是否为”winterchen”。
package com.winterchen;

import com.winterchen.domain.User;
import com.winterchen.mapper.UserMapper;
import org.junit.Assert;
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.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisDemo2ApplicationTests {


    @Autowired
    private UserMapper userMapper;

    @Test
    public void test(){

        userMapper.insert("winterchen", "123456", "12345678910");
        User u = userMapper.findUserByPhone("12345678910");
        Assert.assertEquals("winterchen", u.getName());
    }



}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 测试结果

这里写图片描述

说明已经成功了

事务管理(重要)


我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合。由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻辑并未正确的完成,之前成功操作数据的并不可靠,需要在这种情况下进行回退。

为了测试的成功,请把测试的内容进行替换,因为之前测试的时候已经将数据生成了,重复的数据会对测试的结果有影响

    @Test
    @Transactional
    public void test(){

        userMapper.insert("张三", "123456", "18600000000");
        int a = 1/0;
        userMapper.insert("李四", "123456", "13500000000");
        User u = userMapper.findUserByPhone("12345678910");
        Assert.assertEquals("winterchen", u.getName());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

只需要在需要事务管理的方法上添加 @Transactional 注解即可,然后我们启动测试,会发现异常之后,数据库中没有产生数据。

如果大家想对springboot事务管理有更加详细的了解,欢迎大家查看: springboot事务管理详解

源码:https://github.com/WinterChenS/springboot-mybatis-demo2/

https://blog.csdn.net/winter_chen001/article/details/77249029


  • 10
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值