springboot整合mysql数据库_SpringBoot整合MySQL数据库和Mybatis

简介

SpringBoot 2.3.1.RELEASE版本,整合Mysql和Mybatis例子。

整合MySQL

pom中引入相关依赖包

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

runtime

复制代码

springboot的应用配置文件application.yml中添加数据库和数据库驱动配置

spring:

datasource:

url: jdbc:mysql://127.0.0.1:3306/spring_practice?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8

username: spring-practice

password: 123456

driver-class-name: com.mysql.cj.jdbc.Driver

复制代码

整合Mybatis

pom引入依赖相关依赖

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.3

复制代码

springboot的应用配置文件application.yml中配置mybaits “*Mapper.xml”文件(sql都写到这些文件中)的位置

mybatis:

mapper-locations: classpath:/mybatis/mapper/*.xml

复制代码

在任意一个被@Configuration(@SpringBootApplication被@Configuration注解)注解的类上添加@MapperScan,指定mybaits的dao类所在的package

@SpringBootApplication(scanBasePackages = {"com.huang.*"})

@MapperScan({"com.huang.spring.practice"})

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

复制代码

整合mybatis-generator

使用mybatis来操作数据库的时,我们还需要创建dto、dao、*Mapper.xml文件。如果手动创建上述文件的话,那将会非常的麻烦!!!好在人民的智慧是无穷的,我们可以通过mybatis-generator插件来帮我们自动生成指定数据库表的dto、dao、*Mapper.xml。整合和使用mybaits-generator的步骤如下:

pom文件元素的下添加一个新的元素,来配置mybatis-generator插件的相关配置:

org.mybatis.generator

mybatis-generator-maven-plugin

1.4.0

src/main/resources/mybatis/generatorConfig.xml

true

true

mysql

mysql-connector-java

8.0.20

复制代码

在应用配置的数据库(spring_practice)中创建一张名字叫user的表:

CREATE TABLE `user` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,

`name` varchar(255) NOT NULL,

`age` smallint(6) NOT NULL,

`city` varchar(255) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

复制代码

创建配置文件src/main/resources/mybatis/generatorConfig.xml

/p>

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

connectionURL="jdbc:mysql://127.0.0.1:3306/spring_practice?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8"

userId="spring-practice"

password="123456">

enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"

enableUpdateByExample="false" selectByExampleQueryId="false">

复制代码

在项目根目录下执行mybatis-generator插件,根据generatorConfig.xml中的配置生成文件

mvn mybatis-generator:generate

复制代码

执行成功

D:\JAVA\GIT\spring-practice>mvn mybatis-generator:generate

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building spring.practice 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- mybatis-generator-maven-plugin:1.4.0:generate (default-cli) @ spring.practice ---

[INFO] Connecting to the Database

[INFO] Introspecting table user

[INFO] Generating Record class for table user

[INFO] Generating Mapper Interface for table user

[INFO] Generating SQL Map for table user

[INFO] Saving file UserMapper.xml

[INFO] Saving file User.java

[INFO] Saving file UserMapper.java

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 3.010 s

[INFO] Finished at: 2020-07-12T12:06:26+08:00

[INFO] Final Memory: 21M/217M

[INFO] ------------------------------------------------------------------------

复制代码

在项目中成功生成dto、dao、mapper.xml

2563f83214c1cbfe891db428ffbc8eba.png

测试通过myabtis查询数据

创建一个userController

package com.huang.spring.practice.user.controller;

import com.huang.spring.practice.user.dao.UserMapper;

import com.huang.spring.practice.user.dto.User;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* User controller层

*/

@RestController

@RequestMapping("/api/user/")

public class UserController {

@Autowired

private UserMapper userMapper;

/**

* 根据用户id查询用户信息

*

* @param id

* @return

*/

@GetMapping("/getUserInfo/{id}")

public User getUserInfo(@PathVariable("id") Long id) {

return userMapper.selectByPrimaryKey(id);

}

}

复制代码

往user表中插入一条数据

insert into user (name, age, city) values ('小明', '18', '深圳');

mysql> select * from user;

+----+--------+-----+--------+

| id | name | age | city |

+----+--------+-----+--------+

| 1 | 小明 | 18 | 深圳 |

+----+--------+-----+--------+

1 row in set (0.00 sec)

复制代码

启动我们的SpringBoot应用,访问localhost:8080/api/user/getUserInfo/1

接口成功查询并返回我们之前插入的用户信息

2563f83214c1cbfe891db428ffbc8eba.png

项目代码地址

github.com/ambition080…

参考

MyBatis Generator Config 整体配置

MyBatis Generator Quick Start Guide

mybatis-generator自动生成代码插件使用详解

[基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis](segmentfault.com/a/119000001…)

b739ec46bb5c46d9c0aa4ce35ba1ea56.png

关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。

本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。

[SpringBoot整合MySQL数据库和Mybatis]http://www.zyiz.net/tech/detail-143403.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值