Java基础、中级、高级、架构面试资料

spring boot mybatis 整合教程

JAVA herman 3466浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

Struts2 已被完全淘汰,连 Apache 自己都不在使用了。随着微服务的流行,Spring MVC 的项目也越来越少了,Spring Boot 逐渐成了主流的首先框架。在 ORM 框架方面,有 Hibernate 和 Mybatis 两个可选,它们近几年都没啥大的升级。但相对来说,市面上采用 Mybatis 的公司占据多少,因此本文借助 Spring boot + Mybatis 注解的形式,来教大家如何搭建一个新项目。

搭建的第一步,我们先使用 Spring 提供的初始化构建器创建一个 Spring boot web 项目。有不会的可以参考我的这篇文章:Spring Boot 入门之 helloworld 或 Spring Boot 入门之 helloworld

第二步,在 pom.xml 文件中配置 Mybatis,Mysql 等第三方 jar 包的依赖。

<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.1</version>
</dependency>
<!-- 业余草:www.xttblog.com -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>

有兴趣的朋友,可以在配置阿里的 Druid 数据库连接池。

第三步,在 application.yml 文件中配置数据库连接信息。如下所示:

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

第四步,创建数据库和数据表。

CREATE DATABASE xttblog;

USE xttblog;

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;

第五步,创建 t_user 表所对应的实体类。

package com.xttblog.domain;

/**
 * User实体映射类
 * Created by www.xttblog.com on 2018/06/30.
 */

public class User {

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

    //省略 get 和 set ...
}

第六步,创建 User 映射的操作 UserMapper,为了后续单元测试验证,实现插入和查询操作。

package com.xttblog.mapper;

import com.xttblog.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 www.xttblog.com on 2018/06/30.
 */
@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);

}

spring boot 的启动类,在创建项目的时候就跟着创建了,就不在提示了。代码如下:

package com.xttblog;

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);
    }
}

最后一步,也就是第7步,使用 @SpringBootTest 来测试我们的 Spring boot Mybatis 框架整合的是否有问题。代码如下:

package com.xttblog;

import com.xttblog.domain.User;
import com.xttblog.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("xttblog", "123456", "12345678910");
        User u = userMapper.findUserByPhone("12345678910");
        Assert.assertEquals("xttblog", u.getName());
    }

}

如果你认真按照我的步骤来,最终的结果是单元测试运行成功,绿色通过。

基于以上的代码,你是不是感觉 Spring boot 用起来非常的爽!赶快关注我的博客吧,一起来学习 Spring boot。

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » spring boot mybatis 整合教程