Robinson 大战 Spring boot 之 Mybatis

最近在上 Oracle 数据库的课,老师从一个项目该做什么开始讲起,已经见识了很多工具(虽然还没去用过)。突然,上节课,老师说要开始学数据库了,大家先用 Spring boot 连数据库,然后花了一节课时间把 Spring boot 讲了一遍(这么快的吗)。虽然我国庆看了遍 Spring boot,但我那时候没去连数据库。那这次,趁这机会,来了解一下其他优秀的框架是如何开发的。还有,,,这次老师让我们用 Oracle 数据库来连,这篇我先用 Mysql 连,因为 Oracle 坑太多,所以到时候再开一篇,讲讲怎么连 Oracle。

准备工作

  1. IDEA 用这个开发比较舒服。
  2. 配置 JDK 和 Maven,并更换阿里云镜像。

项目结构

项目结构

新建项目

首先选择 Spring Intializr, 使用这个可以选择哪些组件应用到自己的项目中去。
初始化
这里就把自己的域名写上去,防止和其他包冲突
构建
选web,mybatis,mysql 就可以了
web
sql
接下来项目名自己取一下。

开始写程序

上面步骤都结束之后就可以开始真正的写程序了。

maven配置

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
58
59
60
61
62
63
64
65
66
<!-- /pom.xml -->
<?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>top.bbchen</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>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/>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>

实体类

先把 user 类写了。

1
2
3
4
5
6
7
8
// java/top/bbchen/demo2/entity/User.java
public class User {
private int id;
private String name;
private int age;
private String password;
// get 和 set 方法自己补全
}

数据库结构

自己在数据库里配置一下就行。表名 users 字段 id[int],name[varchar(255)],age[int],password[varchar(255)]
数据库结构

mapper

放在resources/mapper/UserMapper.xml下就行。
写sql语句,待会 DAO 层会来调用。
写了增删改查,记得 namespace 换成自己的 dao 层目录。
还有那些实体类的位置也记得换成自己的,因为我的跟你们不一样。

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
<!-- resources/mapper/UserMapper.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 记得换成自己的↓↓↓↓-->
<mapper namespace="top.bbchen.demo2.dao.UserMapper">

<select id="findUser" parameterType="int" resultType="top.bbchen.demo2.entity.User">
select ID , name as name, age as age, password from users where ID = #{id}
</select>

<insert id="add" parameterType="top.bbchen.demo2.entity.User">
insert into users(name,age,password) values (#{name}, #{age} , #{password})
</insert>

<update id="updateUser" parameterType="top.bbchen.demo2.entity.User">
update users set name = #{name} where id = #{id}
</update>

<delete id="deleteUser" parameterType="int">
delete from users where id = #{id}
</delete>

</mapper>

数据库配置

在application.properties中配置自己的数据库连接

1
2
3
4
5
6
// resources/application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

DAO层

这里写抽象的数据层接口,不具体实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Mapper
public interface UserMapper {
// java/top/bbchen/demo2/dao/UserMapper.java

// 下面注释掉的 @Insert和@Select 是因为一开始使用注解写sql 后来换成xml写sql,所以注释掉了。解开注释,就是使用注解写sql。
// @Insert("insert into users(name,age,password) values (#{name}, #{age} , #{password})")
public int add(User user);

// @Select("select id, name as name, age as age, password from users where id = #{id}")
public User findUser(int id);

public int updateUser(User user);

public int deleteUser(int id);
}

service层

这层用来实现上面的DAO层的。
用了自动注入,所以不用直接写实现某某接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// java/top/bbchen/demo2/service/UserService.java

@Service
public class UserService {
//自动注入
@Autowired
private UserMapper userMapper;

public int add(User user) {
return userMapper.add(user);
}
public User findUser(int id) {
return userMapper.findUser(id);
}
public int updateUser(User user) {
return userMapper.updateUser(user);
}
public int deleteUser(int id) {
return userMapper.deleteUser(id);
}
}

控制层

最后再写控制层就可以了

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
// java/top/bbchen/demo2/web/UserController.java
@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
UserService userService;

@RequestMapping("/{id}")
public User getUserById(@PathVariable("id") int id) {
System.out.println(id);
return userService.findUser(id);
}

@RequestMapping(value = "",method = RequestMethod.POST)
public String addUser(User user) {
int t = userService.add(user);
if (t==1)return "success";
else return "fail";
}

@RequestMapping(value = "/update",method = RequestMethod.POST)
public String updateUser(User user) {
int t = userService.updateUser(user);
if (t==1)return "success";
else return "fail";
}

@RequestMapping("/delete/{id}")
public String deleteUser(@PathVariable("id") int id) {
int t = userService.deleteUser(id);
if (t==1)return "success";
else return "fail";
}
}

运行

运行

下一篇

下一篇将讲一下怎么使用 Oracle 数据库。