Spring Boot 整合 MyBatis 快速搭建 Web 开发环境

在 SSM 中,各种框架的整合要写繁琐的 XML 配置文件。但如果使用 Spring Boot ,很快就能够搭建一个三层架构的 Web 环境。简而言之,使用 Spring Boot 构建服务端,能够快速开发出 http 接口。

一、整合 Mybatis,搭建 Web 环境

1、创建数据库表

创建数据库 springboot-data,学生表 student

drop database if exists `springboot-data`;
create database `springboot-data`;
use `springboot-data`;

drop table if exists `student`;
create table `student`
(
   `id`                   int(10) not null auto_increment,
   `name`                 varchar(20) not null,
   `pwd`                  varchar(20) not null,
   primary key (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

insert into `student` (`name`,`pwd`)
values 
('小明','123456'),
('小华','234567'),
('小红','345678');

2、导入相关依赖

要整合 Mybatis,需要添加如下依赖:

  • spring-boot-starter-jdbc
  • mysql-connector-java
  • mybatis-spring-boot-starter

Maven 如下:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.1</version>
		</dependency>

3、创建项目的包和目录

在搭建 web 环境前,我们首先新建如下目录:

  • controller
  • mapper
  • pojo
  • service
  • resources / mapper
  • resources / application.yaml

项目结构如下:
在这里插入图片描述

4、配置数据库连接

无论持久层采用什么框架,既然要连接数据库,那么参数都是必须要配置的
application.yaml 中添加jdbcmybatis 的相关配置:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值