文章目录
在 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
中添加jdbc
与 mybatis
的相关配置:
spring:
datasource:
username: root
password: 123456
url: jdbc: