SpringBoot 整合 SpringMVC+MyBatis+Thymeleaf

需求:通过使用 SpringBoot+SpringMVC+MyBatis+Thymeleaf 整合完成对数据库的增删改查操作
项目总体搭建:在这里插入图片描述
一:项目创建
1.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.lidadaibiao</groupId>
  <artifactId>Springboot-SpringMvc-Mybatis-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.12.RELEASE</version>
    <relativePath/> 
  </parent>
     <!-- 修改jdk版本 -->
  <properties>
  	<java.version>1.8</java.version>
  	<mysql.version>5.1.45</mysql.version>
  </properties>
   <dependencies>
    <!-- springBoot的启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
        <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>

2.在src/main/resource下添加application.xml全局配置文件
application.xml文件配置

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

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

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

spring.datasource.type:快速的指定我们需要的数据源
mybatis.type-aliases-package:mapper.xml文件中resultMap的type或者parameterType会使用自定义的pojo。《看我UsersMapper.xml的配置》
3.映射文件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.lidadaibiao.mapper.UsersMapper">
	<insert id="insertUsers" parameterType="users">
	insert into users(username,password) values(#{username},#{password})
	</insert>
	<select id="findAllUsers" resultType="users">
	select id,username,password from users
	</select>
	<select id="findById" resultType="users">
	select id,username,password from users where id=#{value}
	</select>
	<update id="updateUsers" parameterType="users">
	update users set username=#{username},password=#{password} where id = #{id}
	</update>
	<delete id="deleteUsers">
	delete from users where id=#{value}
	</delete>
</mapper>

二.创建相关类
1.创建实体类

package com.lidadaibiao.pojo;

public class Users {
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Users() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Users(int id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}
	@Override
	public String toString() {
		return "Users [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
}

2.创建mapper接口

package com.lidadaibiao.mapper;

import java.util.List;

import com.lidadaibiao.pojo.Users;

public interface UsersMapper {
	/**
	 * 增加用户
	 * @param users
	 */
	void insertUsers(Users users);
	/**
	 * 删除用户
	 * @param id
	 */
	void deleteUsers(int id);
	/**
	 * 根据用户Id查询用户
	 * @param id
	 * @return
	 */
	Users findById(int id);
	/**
	 * 查询所有用户
	 * @return
	 */
	List<Users> findAllUsers();
	/**
	 * 更新用户
	 * @param users
	 */
	void updateUsers(Users users);
	
}

3.创建业务层接口和实现类

package com.lidadaibiao.service;

import java.util.List;

import com.lidadaibiao.pojo.Users;

public interface UsersService {
	void insertUsers(Users users);
	void deleteUsers(int id);
	Users findById(int id);
	List<Users> findAllUsers();
	void updateUsers(Users users);
}

package com.lidadaibiao.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.lidadaibiao.mapper.UsersMapper;
import com.lidadaibiao.pojo.Users;
import com.lidadaibiao.service.UsersService;
@Service
@Transactional
public class UsersServiceImpl implements UsersService{
	@Autowired
	private  UsersMapper usersMapper;
	@Override
	public void insertUsers(Users users) {
		// TODO Auto-generated method stub
		this.usersMapper.insertUsers(users);
	}

	@Override
	public void deleteUsers(int id) {
		// TODO Auto-generated method stub
		this.usersMapper.deleteUsers(id);
	}

	@Override
	public Users findById(int id) {
		// TODO Auto-generated method stub
		
		return this.usersMapper.findById(id);
	}

	@Override
	public List<Users> findAllUsers() {
		// TODO Auto-generated method stub
		return this.usersMapper.findAllUsers();
	}

	@Override
	public void updateUsers(Users users) {
		// TODO Auto-generated method stub
		this.usersMapper.updateUsers(users);
	}
	
}

4.创建Controller层

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

@Controller
@RequestMapping("/users")
public class UsersController {
	@Autowired
	private UsersService usersService;
	@RequestMapping("/{page}")
	public String showPage(@PathVariable String page){
		return page;
	}
	/**
	 * 增加用户
	 */
	@RequestMapping("/insertUsers")
	public String insertUsers(Users users){
		this.usersService.insertUsers(users);
		return "redirect:/users/findAllUsers";
	}
	/**
	 * 查询所有用户
	 */
	@RequestMapping("/findAllUsers")
	public String findAllUsers(Model model){
		List<Users> list = this.usersService.findAllUsers();
		model.addAttribute("list", list);
		return "showUsers";
	}
	/**
	 * 根据Id查询用户
	 */
	@RequestMapping("/findById")
	public String findById(int id,Model model){
		Users users = this.usersService.findById(id);
		System.out.println(users.getId());
		model.addAttribute("users", users);
		return "updateUsers";
	}
	@RequestMapping("/editUsers")
	public String editUsers(Users users){
		System.out.println(users.toString());
		this.usersService.updateUsers(users);
		return "redirect:/users/findAllUsers";
	}
	@RequestMapping("/deleteUsers")
	public String deleteUsers(int id){
		this.usersService.deleteUsers(id);
		return "redirect:/users/findAllUsers";
	}
}

三 Thymeleaf视图层设计
1.goon.html设置

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>主页面</title>
</head>
<body>
	<form th:action="@{/users/insertUsers}" method="post">
		UserName:<input type="text" name="username">
		<br>
		PassWord:<input type="text" name="password">
		<br>
		<input type="submit" value="确认">
		<br>
		<a th:href="@{/users/findAllUsers}">查询所有用户</a>
	</form>
</body>
</html>

2.finis.html设计

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Finish</title>
</head>
<body>
	Good,Finish!!!!
	<br>
	<a th:href="@{/users/goon}">返回</a>
</body>
</html>

3.showUsers.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询所有用户</title>
</head>
<body>
	<table border="1">
  <tr>
    <th>Id</th>
    <th>UserName</th>
    <th>PassWord</th>
    <th>相关操作</th>
  </tr>
  <tr th:each="user :${list}">
    <td th:text="${user.id}"></td>
  	<td th:text="${user.username}"></td>
  	<td th:text="${user.password}"></td>
  	<td><a th:href="@{/users/findById(id=${user.id})}">更新数据  </a>
  	<a th:href="@{/users/deleteUsers(id=${user.id})}"> 删除数据</a>
  	</td>
  </tr>
</table>
</body>
</html>

4.updateUsers.html设计

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form th:action="@{/users/editUsers}" method="post">
	<input type="hidden" name="id" th:field="${users.id}">
	UserName:<input type="text" name="username" th:field="${users.username}">
	<br>PassWord:<input type="text" name="password" th:field="${users.password}">
	<br><input type="submit" value="确定">
	</form>
</body>
</html>

注:templates文件夹必须要自己创建,必须要创建。
四:项目启动类

package com.lidadaibiao;

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

@SpringBootApplication
@MapperScan("com.lidadaibiao.mapper")//扫描Mapper
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

@MapperScan(“com.lidadaibiao.mapper”):用于扫描Mapper映射文件。

发布了20 篇原创文章 · 获赞 17 · 访问量 1793

猜你喜欢

转载自blog.csdn.net/qq_42534991/article/details/104213700
今日推荐