需求分析:通过使用SpringBoot+SpringMVC+Mybatis整合实现对一个数据库中的users表的CRUD的操作
一:创建项目
1.修改pom文件
<!-- 修改jdc版本 -->
<properties>
<java.version>1.7</java.version>
<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 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.3.0</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>
2.application.properties全局配置文件
#配置数据库驱动配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_ssm
spring.datasource.username=root
spring.datasource.password=root
#数据数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#配置别名
mybatis.type-aliases-package=com.springboot.pojo
3.数据库表设计
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;
二:添加用户
1.创建实体类
public class Users implements Serializable{
private Integer id;
private String name;
private Integer age;
public int 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;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public Users() {
super();
}
}
2.创建mapper接口以及映射配置文件
接口配置:
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.springboot.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into
users(name,age) values(#{name},#{age})
</insert>
</mapper>
3.创建业务层
service层接口:
public interface UsersService {
void addUser(Users users);
}
service层实现类
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users users) {
}
}
3.创建Controller层
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 页面跳转
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page) {
return page;
}
/**
* 添加用户处理
*/
@RequestMapping("/addUser")
public String adddUser(Users users) {
usersService.addUser(users);
return "ok";
}
}
4.操作界面编写
input.html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</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>
<form 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>
5.编写启动类
@SpringBootApplication
@MapperScan("com.springboot.mapper") //用于扫描Mybaits的Mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
三:查询用户
1.配置mapper映射文件和接口
在UsersMapper接口中继续添加方法
List<Users> selectUsersAll();
在mapper映射文件中添加
<select id="selectUsersAll" parameterType="users">
select id,name,age from users
</select>
2.在业务层添加添加查询所有用户的代码
@Override
public List<Users> findUsersAll() {
return usersMapper.selectUsersAll();
}
3.在controller层添加查询所有的方法
/**
* 查询全部用户
*/
@RequestMapping("/findUserAll")
public String findUserAll(Model model) {
List<Users> list = usersService.findUsersAll();
model.addAttribute("list",list);
return "showUsers";
}
4.添加页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="300px;">
<tr>
<td>用户ID</td>
<td>用户NAME</td>
<td>用户AGE</td>
</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>
四:用户更新
1.更新用户之前要进行查询操作,并将数据在页面中回显
1.1修改mapper和映射文件
接口
Users selectUsersById(Integer id);
添加mapper中内容
<select id="selectUsersById" parameterType="Integer" resultType="user">
select id,name,age from users where id = #{id}
</select>
1.2.修改service层的接口和实现类
接口
Users findUsersById(Integer id);
实现类中添加的方法
@Override
public Users findUsersById(Integer id) {
usersMapper.selectUsersById(id);
return null;
}
1.3修改controller层和显示页面
controller层代码
/**
* 通过id查询用户
*/
@RequestMapping("/findUserById")
public String findUserById(Integer id,Model model) {
Users user = usersService.findUsersById(id);
model.addAttribute("user",user);
return "updateUser";
}
updateUser.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</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>
五:更新用户
1.修改mapper接口以及映射配置文件
mapper.xml配置
<select id="updateUsers" parameterType="users">
update users set name=#{name},age=#{age} where id=#{id}
</select>
接口方法:
void updateUsers(Users users);
2.修改业务层代码
@Override
public void updateUsers(Users users) {
usersMapper.updateUsers(users);
}
3.修改Controller页面
/**
* 更新用户
*/
@RequestMapping("/editUser")
public String updateUser(Users users) {
usersService.updateUsers(users);
return "ok";
}
六:删除用户
1.修改mapper.xml文件和接口
mapper.xml接口修改
<delete id="deleteUserById" parameterType="Integer">
delete from users where id=#{id}
</delete>
接口修改:
void deleteUserById(Integer id);
2.修改业务层代码
@Override
public void deleteUserById(Integer id) {
usersMapper.deleteUserById(id);
}
3.修改Controller层代码
/**
* 删除用户
*/
@RequestMapping("/deleteUser")
public String deleteUserById(Integer id) {
usersService.deleteUserById(id);
return "redirect:/users/findUserAll";
}