SpringBoot(四)整合Mybatis——《3》CRUD

1、项目结构

2、pom.xml

<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>com.myron</groupId>
  <artifactId>11-springboot-ssm</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  
   <!-- 修改jdk版本 -->
  <properties>
  	<java.version>1.8</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>
	<!-- 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>
    <!-- SpringBoot热部署插件 -->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
	</dependency>
    
  </dependencies>
</project>

3、Controller层

3.1、UserController

package com.myron.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.myron.pojo.Users;
import com.myron.service.UsersService;

@RequestMapping("/users")
@Controller
public class UserController {
	
	@Autowired
	private UsersService userService;
	
	//页面跳转
	@RequestMapping("/{page}")
	public String showPage(@PathVariable String page) {
		return page;
	}
	
	//添加用户
	@RequestMapping("/addUser")
	public String addUser(Users users ) {
		userService.addUser(users);
		return "ok";
	}
	
	//查询全部用户
	@RequestMapping("/findUserAll")
	public String getUser(Model model) {
		List<Users> list = userService.findUserAll();
		model.addAttribute("list", list);
		return "showUsers";
	}
	
	//根据id查询用户,更新用户之前的查询,并将数据在页面中回显
	@RequestMapping("findUserById")
	public String findUserById(Integer id,Model model) {
		Users user = userService.findUserById(id);
		model.addAttribute("user", user);
		return "updateUser";
	}
	
	//更新用户
	@RequestMapping("editUser")
	public String editUser(Users users) {
		userService.updataUser(users);
		return "ok";
	}
	
	//删除用户
	@RequestMapping("delUser")
	public String delUser(Integer id) {
		userService.deleteUserById(id);
		return "redirect:/users/findUserAll";
	}

}

4、业务层

4.1、UserServiceImpl

package com.myron.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.myron.mapper.UserMapper;
import com.myron.pojo.Users;
import com.myron.service.UsersService;

@Service
@Transactional
public class UserServiceImpl implements UsersService{

	@Autowired
	private UserMapper userMapper;
	
	//添加用户
	@Override
	public void addUser(Users users) {
		userMapper.insertUser(users);
	}

	//查询所有用户
	@Override
	public List<Users> findUserAll() {
		return userMapper.selectUsersAll();
	}

	//根据id查询用户
	@Override
	public Users findUserById(Integer id) {
		return userMapper.selectUsersById(id);
	}

	//更新用户
	@Override
	public void updataUser(Users users) {
		userMapper.updateUser(users);
	}

	//删除用户
	@Override
	public void deleteUserById(Integer id) {
		userMapper.deleteUserById(id);
	}
	
}

4.2、UserService

package com.myron.service;

import java.util.List;

import com.myron.pojo.Users;

public interface UsersService {
	
	void addUser(Users users);

	List<Users> findUserAll();

	Users findUserById(Integer id);

	void updataUser(Users users);

	void deleteUserById(Integer id);
}

5、mapper接口以及映射配置文件

5.1、UserMapper:

package com.myron.mapper;

import java.util.List;

import com.myron.pojo.Users;

public interface UserMapper {
	void insertUser(Users users);

	List<Users> selectUsersAll();

	Users selectUsersById(Integer id);

	void updateUser(Users users);

	void deleteUserById(Integer id);
}

5.2、UserMapper.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.myron.mapper.UserMapper">
	<!-- 添加用户 -->
	<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>
	<!-- 根据id查询用户 -->
	<select id="selectUsersById" 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="deleteUserById" >
		delete from users where id = #{id}
	</delete>
	
</mapper>

6、application.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_ssm?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

mybatis.type-aliases-package=com.myron.pojo

7、静态页面

7.1、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>

7.2、ok.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
	操作成功!!!
</body>
</html>

7.3、showUsers.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
	<table border="1" style="width:400px;">
		<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>

7.4、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>

8、编写启动类

package com.myron;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBoot 启动类: 
 *
 */
@SpringBootApplication
@MapperScan("com.myron.mapper") //用户扫描mybatis的mapper接口
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

9、测试

1、添加用户:

2、查询所有用户

3、修改用户

点击更新用户按钮:

1)更新前回显用户信息:

2)更新用户信息:

4、删除用户

直接点击删除用户14,16:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值