avatar

SpringBoot+Mybatis+Thymeleaf_基础开发

创建项目

通过idea的Spring Initializr来创建项目

Group一般是你组织的名字,例如:域名.公司名.项目名

artifact一般写为:项目-xxx

SpringBoot + Mybatis 简单的测试

1、基础配置


首先我们对环境进行配置,pom.xml的dependencies配置如下

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
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>

经过测试mybatis为默认,mysql为8.0.17,springboot只需要用默认的即可,thymeleaf也是用默认的即可。

然后对application.properties进行配置,配置如下:

1
2
3
4
5
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=数据库登录账号
spring.datasource.password=数据库登录密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapping/*.xml #locations目录默认以resources为根目录

2、SpringBoot + Mybatis 的层


一般情况下我们分为四层

controller层:调用service层的接口,进行业务操作。

entity层:entity层和model层等价,存放的是实体类,属性值与数据库中的属性值保持一致。实现set和get方法(这一步不可以少,少一个get或者set都不行,只需要默认的get和set即可)。

mapper层:dao层和mapper层等价,对数据库进行持久化操作,它的方法是针对数据库操作的,基本用到的就是增删改查。他只是定义一个接口,然后在mapper.xml中实现接口和数据库的交互。

service层:存放业务逻辑处理,不直接对数据库进行操作,有接口和接口实现类,提供controller层调用方法。

在resources类中创建一个包,用于存放Mapper.xml

具体分布如下图所示

3、代码实现


controller层:UserController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/testBoot")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/getUsers",method = RequestMethod.GET)
public List<User> GetUsers(){
return userService.getUsers();
}
}

entity层:User

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
package com.example.demo.entity;
public class User {
private Integer id;
private String name;
private String account;
private String password;
private String email;
@Override
public String toString() {
return "id = " + getId();
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAccount(String account) {
this.account = account;
}
public String getAccount() {
return account;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}

mapper层:UserMapper

1
2
3
4
5
6
7
8
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
List<User> getUsers();
}

service层:UserService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class UserService {
@Autowired
UserMapper userMapper;
public List<User> getUsers() {
return userMapper.getUsers();
}
}

mapping.xml配置:UserMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="user" type="com.example.demo.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="account" jdbcType="VARCHAR" property="account" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="email" jdbcType="VARCHAR" property="email"/>
</resultMap>
<select id="getUsers" resultMap="user">
select * from user
</select>
</mapper>

运行效果:

注意事项:

1、如果service层@Autowired注解的变量出现了ERROR,但是运行时没有问题,请在Mapper层接口头部添加@Repository

2、请注意mapper.xml文件中的目录,若报错,请注意查看

带参数的输出某id的数据库数据

1、UserMapper.java需要加上getUser(Integer id)

1
2
3
4
5
6
7
8
9
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
List<User> getUsers();
User getUser(Integer id);
}

2、UserService.java需要定义getUser(Integer id)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class UserService {
@Autowired
UserMapper userMapper;
public List<User> getUsers() {
return userMapper.getUsers();
}
public User getUser(Integer id){
return userMapper.getUser(id);
}
}

3、UserController添加新的业务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/testBoot")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/getUsers",method = RequestMethod.GET)
public List<User> getUsers(){
return userService.getUsers();
}
@RequestMapping(value = "getUser",method = RequestMethod.GET)
public User getUser(@RequestParam(value="id",required = true) Integer id){
return userService.getUser(id);
}
}

4、UserMapper.xml需要添加新的sql语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="user" type="com.example.demo.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="account" jdbcType="VARCHAR" property="account" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="email" jdbcaType="VARCHAR" property="email"/>
</resultMap>
<select id="getUsers" resultMap="user">
select * from user
</select>
<select id="getUser" resultMap="user">
select * from user
<where>
id = #{id}
</where>
</select>
</mapper>

运行效果

通过thymeleaf显示数据库数据


1、首先我们再创建一个view层,java代码类似于UserController,但是不同的是把restController替换为Controller,这样就可以跳转到指定的网页了,如果函数参数中带有model就可以传入数据到网页,通过thymeleaf来渲染网页。

2、UserView代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.demo.view;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value = "user_view")
public class UserView {
@Autowired
private UserService userService;
@RequestMapping(value = "/test")
public String user_view(@RequestParam(value = "id",required = false) Integer id, Model model){
model.addAttribute("user_data",userService.getUser(id));
return "index";
}
}

3、接着我们开始写html,这里我们需要用到类似于EL语言的写法,代码如下

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${user_data.name}"></p>
</body>
</html>

运行效果如下:

文章作者: 咲夜南梦
文章链接: http://yoursite.com/2019/11/05/SpringBoot+Mybatis+Thymeleaf_%E5%9F%BA%E7%A1%80%E5%BC%80%E5%8F%91/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 咲夜南梦's 博客
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论