SpringBoot
Spring整合SpringMVC+Mybatis
需求分析:通过使用SpringBoot+SpringMVC+Mybatis整合实现一个对数据库中的users表的CRUD的操作
创建项目
finish
修改pom文件
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>12-spring-boot-springmvc-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<thymeleaf.version>3.0.2.RELEASE</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>
<!-- thymeleaf的启动器 -->
<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>
</project>
添加application.properties全局配置文件
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.bjsxt.pojo
数据库表设计
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加用户
创建实体类
package com.bjsxt.pojo;
public class Users {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
创建mapper接口以及映射文件
package com.bjsxt.mapper;
import com.bjsxt.pojo.Users;
public interface UsersMapper {
void insertUser(Users users);
}
<?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.bjsxt.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into users(name,age) values(#{name},#{age})
</insert>
</mapper>
创建业务层
package com.bjsxt.service;
import com.bjsxt.pojo.Users;
public interface UsersService {
void addUser(Users users);
}
package com.bjsxt.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
@Service
@Transactional
public class UsersServiceImpl implements UsersService{
@Autowired
private UsersMapper usermapper;
@Override
public void addUser(Users users) {
this.usermapper.insertUser(users);
}
}
创建Controller
package com.bjsxt.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 页面跳转
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page) {
return page;
}
/**
* 添加用户
*/
@RequestMapping("/addUser")
public String addUser(Users users) {
this.usersService.addUser(users);
return "ok";
}
}
编写页面
input.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/users/addUser}" method="post">
用户姓名:<input type="text" name="name"/><br/>
用户年龄:<input type="text" name="age"/><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>
</html>
ok.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
操作成功
</body>
</html>
启动类
package com.bjsxt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.bjsxt.mapper") //@MapperScan 用于告诉SpringBoot扫描Mybatis的Mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
查询用户
在mapper接口中以及映射文件添加查询方法
package com.bjsxt.mapper;
import java.util.List;
import com.bjsxt.pojo.Users;
public interface UsersMapper {
void insertUser(Users users);
/**
* 查询所有
* @return
*/
List<Users> selectUsersAll();
}
<?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.bjsxt.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into users(name,age) values(#{name},#{age})
</insert>
<select id="selectUsersAll" resultType="users">
select id,name,age from users
</select>
</mapper>
在业务层中添加查询方法
package com.bjsxt.service;
import java.util.List;
import com.bjsxt.pojo.Users;
public interface UsersService {
void addUser(Users users);
/**
* 查询全部
* @return
*/
List<Users> findUsersAll();
}
package com.bjsxt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
@Service
@Transactional
public class UsersServiceImpl implements UsersService{
@Autowired
private UsersMapper usermapper;
@Override
public void addUser(Users users) {
this.usermapper.insertUser(users);
}
@Override
public List<Users> findUsersAll() {
return this.usermapper.selectUsersAll();
}
}
在Controller中添加方法
package com.bjsxt.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 页面跳转
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page) {
return page;
}
/**
* 添加用户
*/
@RequestMapping("/addUser")
public String addUser(Users users) {
this.usersService.addUser(users);
return "ok";
}
/**
* 查询全部用户
*/
@RequestMapping("/findUserAll")
public String findUserAll(Model model) {
List<Users> list = this.usersService.findUsersAll();
model.addAttribute("list", list);
return "showUsers";
}
}
添加查询页面
showUsers.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:300px;">
<tr>
<th>用户ID</th>
<th>用户姓名</th>
<th>用户年龄</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>
</tr>
</table>
</body>
</html>
用户更新
更新用户之前的查询,并将数据在页面中回显
修改mapper接口以及映射配置文件
/**
* 根据id查询用户
* @param id
* @return
*/
Users selectUsersById(Integer id);
<select id="selectUsersById" resultType="users">
select id,name,age from users where id = #{value}
</select>
修改业务层代码
/**
* 根据id查询用户
*/
Users findUsersById(Integer id);
@Override
public Users findUsersById(Integer id) {
// TODO Auto-generated method stub
return this.usermapper.selectUsersById(id);
}
添加页面
updateUser.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/users/editUser}" method="post">
<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="确定"/><br/>
</form>
</body>
</html>
showUsers.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:300px;">
<tr>
<th>用户ID</th>
<th>用户姓名</th>
<th>用户年龄</th>
<th>操作</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/findUserById(id=${user.id})}">更新用户</a>
</td>
</tr>
</table>
</body>
</html>
更新用户
修改mapper接口以及映射配置文件
/**
* 更新用户
*/
void updateUser(Users users);
<update id="updateUser" parameterType="users">
update users set name=#{name} ,age=#{age} where id=#{id}
</update>
修改业务层代码
/**
* 更新用户
*/
void updateUser(Users users);
@Override
public void updateUser(Users users) {
this.usermapper.updateUser(users);
}
修改Controller
/**
* 更新用户
*/
@RequestMapping("/editUser")
public String editUser(Users users) {
this.usersService.updateUser(users);
return "ok";
}
删除用户
修改mapper接口以及映射配置文件
/**
* 删除用户
*/
void deleteUserById(Integer id);
<delete id="deleteUserById">
delete from users where id = #{value}
</delete>
修改业务层
/**
* 删除用户
*/
void deleteUserById(Integer id);
@Override
public void deleteUserById(Integer id) {
// TODO Auto-generated method stub
this.usermapper.deleteUserById(id);
}
修改Controller
/**
* 删除用户
*/
@RequestMapping("/delUser")
public String delUser(Integer id) {
this.usersService.deleteUserById(id);
return "redirect:/users/findUserAll";
}
修改页面
showUsers.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:500px;">
<tr>
<th>用户ID</th>
<th>用户姓名</th>
<th>用户年龄</th>
<th>操作</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/findUserById(id=${user.id})}">更新用户</a>
<a th:href="@{/users/delUser(id=${user.id})}">删除更新用户</a>
</td>
</tr>
</table>
</body>
</html>