SpringBoot整合持久层:SpringMVC+MyBatis
SpringBoot学习目录
创建项目
目录结构

修改pom.xml
<!-- 修改jdk版本 -->
<properties>
<java.version>1.8</java.version>
<!-- <thymeleaf.version>3.0.1.RELEASE</thymeleaf.version> -->
<springboot-thymeleaf.version>3.0.2.RELEASE</springboot-thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- web 启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>
创建配置文件
application.properties:
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
#mysql-connector-java使用6.0以上版本时,应该用com.mysql.cj.jdbc.Driver
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
#?serverTimezone=Asia/Shanghai→修改时区
#?useUnicode=true&characterEncoding=utf8→修改编码
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.znk.pojo
#配置实体类存放路径
创建实体类
Users.java:
public class Users {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
创建Mapper接口
UsersMapper.java:
public interface UsersMapper {
void insertUser(Users users);
List<Users> selectUserAll();
Users selectUserById(String id);
void updateUser(Users users);
void deleteUser(String id);
}
创建映射文件
UsersMapper.xml:
<?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.znk.mapper.UsersMapper"><!-- namespace的值为对应Mapper接口的路径 -->
<!-- 在application中配置了实体类路径,com.znk.pojo.users可简写为users -->
<insert id="insertUser" parameterType="users">
insert into users(name,age) values(#{name},#{age})
</insert>
<select id="selectUserAll" resultType="users">
select id,name,age from users
</select>
<select id="selectUserById" resultType="users">
select id,name,age from users where id=#{id}
</select>
<update id="updateUser" parameterType="users">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<delete id="deleteUser">
delete from users where id=#{id}
</delete>
</mapper>
创建Service层
UsersService.java:
public interface UsersService {
void addUser(Users users);
List<Users> selectUserAll();
Users selectUserById(String id);
void updateUser(Users users);
void deleteUser(String id);
}
UsersServiceImpl.java:
@Service
@Transactional
public class UsersServiceImpl implements UsersService{
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users users) {
usersMapper.insertUser(users);
}
@Override
public List<Users> selectUserAll() {
List<Users> li = usersMapper.selectUserAll();
return li;
}
@Override
public Users selectUserById(String id) {
Users users=usersMapper.selectUserById(id);
return users;
}
@Override
public void updateUser(Users users) {
usersMapper.updateUser(users);
}
@Override
public void deleteUser(String id) {
usersMapper.deleteUser(id);
}
}
创建Controller
UsersController.java:
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 页面跳转
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
/**
* 添加用戶
* @param users
* @return
*/
@RequestMapping("/addUser")
public String addUser(Users users){
System.err.println(users.toString());
System.err.println("/users/addUser---0");
usersService.addUser(users);
System.err.println("/users/addUser---1");
return "success";
}
/**
* 表格用户展示
* @param model
* @return
*/
@RequestMapping("/showUsers")
public String selectUserAll(Model model){
List<Users> list = usersService.selectUserAll();
model.addAttribute("list", list);
return "showUsers";
}
/**
* 通过id查user
* @param id
* @param model
* @return
*/
@RequestMapping("/selectUserById")
public String selectUserById(String id,Model model){
Users user = usersService.selectUserById(id);
model.addAttribute("user", user);
return "update";
}
/**
* 跳转showUsers页面
* @param users
* @return
*/
@RequestMapping("/updateUser")
public String updateUser(Users users){
usersService.updateUser(users);
System.err.println("/users/updateUser");
return "redirect:/users/showUsers";
}
/**
* 删除
* @param id
* @return
*/
@RequestMapping("/deleteUser")
public String deleteUser(String id){
usersService.deleteUser(id);
return "redirect:/users/showUsers";
}
}
创建启动类
Application.java:
@SpringBootApplication
@MapperScan("com.znk.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建页面
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
form{
text-align:center;
}
.box{
margin:100px auto;
}
</style>
</head>
<body>
<form th:action="@{/users/addUser}" method="post">
<div class="box">
姓名:<input type="text" name="name"/><br/>
年龄:<input type="text" name="age"/><br/>
<input type="submit" value="提交"/>
</div>
</form>
</body>
</html>
showUsers.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px" align="center" width="" cellspacing="0" cellpadding="10px">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th colspan="2"><a th:href="@{/users/index}">add</a></th>
</tr>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td><a th:href="@{/users/selectUserById(id=${user.id})}">update</a></td>
<td><a th:href="@{/users/deleteUser(id=${user.id})}">delete</a></td>
</tr>
</table>
</body>
</html>
update.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
form{
text-align:center;
}
.box{
margin:1 00px auto;
}
</style>
</head>
<body>
<form th:action="@{/users/updateUser}" method="post">
<div class="box">
<input type="hidden" name="id" th:field="${user.id}" />
姓名:<input type="text" name="name" th:field="${user.name}" /><br>
年龄:<input type="text" name="age" th:field="${user.age}" /><br>
<input type="submit" value="提交" />
</div>
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>success</p>
</body>
</html>
本文详细记录了SpringBoot项目中整合SpringMVC和MyBatis的过程,包括创建项目、配置文件、实体类、Mapper接口、映射文件、Service层、Controller及启动类的创建,最后介绍了页面的搭建。
638

被折叠的 条评论
为什么被折叠?



