springBoot+myBatis(mql)+thymeleaf实现基本增删查改

一.maven引入jar包

<?xml version="1.0" encoding="UTF-8"?>
<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.seecen</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <!--mabatis-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>4.1.6</version>
		</dependency>
		

		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>

		<!-- thymeleaf模板插件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!-- devtools插件,热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

二.配置application.properties配置文件

spring.datasource.url= jdbc:mysql://localhost:3306/peng?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


#spring.mvc.view.suffix=.jsp


###mybatis配置
mybatis.mapper-locations=classpath:mappers/*.xml


###文件上传下载大小设置
# Single file max size
multipart.maxFileSize=50Mb
# All files max size
multipart.maxRequestSize=50Mb

#配置thymeleaf的模板模式为HTML5
spring.thymeleaf.mode= HTML5
3spring.thymeleaf.prefix=classpath:/templates/*.html

三.java代码部分

3.1controller层

package com.seecen.controller;

import com.seecen.entity.Users;
import com.seecen.service.UserService;
import com.seecen.service.UsersServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class userController {

    @Autowired
    private UserService userService;

    @Autowired
    private UsersServices usersServices;

    @RequestMapping("/user")
    public String querrAllId(HttpServletRequest request, Model model ){
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println(id);
        Users user = userService.queryAllId(id);
        //model.addAttribute("user",user);//也能发送对象到前台
        request.setAttribute("user",user);
        return  "updateUser";
    }

    @RequestMapping(value = "/iris",produces = "text/plain;charset=UTF-8")
    public String queryUsers(HttpServletRequest request){
        List<Users> usersList = usersServices.queryUsers();
        request.setAttribute("usersList",usersList);

        return ("userList1");
    }

    @RequestMapping("/updateUsers")
    public String updateUsers(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        Boolean flag=usersServices.updateUsers(id,name);
        if (flag==true){
            System.out.println("修改成功");
            return "forward:/iris";
        }else {
            System.out.println("修改失败");
        }
        return "index";
    }

    //@RequestMapping("/deleteUsers/")
    @RequestMapping(value = "/deleteUsers")
    public String deleteUsers(HttpServletRequest request ){
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println(id);
        Boolean flag = usersServices.delteUsers(id);
        if (flag==true){
            System.out.println("删除成功!");
            return "forward:/iris";
        }else {
            System.out.println("删除失败");
        }
        return "index";
    }

    @RequestMapping("addUsers")
    public String addUsers(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        Users users =new  Users();
        users.setId(id);
        users.setName(name);
        users.setSex(sex);
        usersServices.addUsers(users);
        return "forward:/iris";
    }


    public static void main(String[] args) {
       String a="3";
       String b = new String("3");
       String c = "3";
       System.out.println(a==b);
       System.out.println(c==a);
    }

}

3.2 serviceImpl层

package com.seecen.service;

import com.seecen.dao.UsersDao;
import com.seecen.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UsersServicesImpl implements UsersServices {

    @Autowired
    UsersDao usersDao;

    @Override
    public List<Users> queryUsers() {
        return usersDao.queryUsers();
    }

    //修改
    @Override
    public Boolean updateUsers(int id,String name) {
        return usersDao.updateUsers(id,name);
    }

    @Override
    public Boolean delteUsers(int id) {
        return usersDao.deleteUsers(id);
    }

    @Override
    public void addUsers(Users users) {
         usersDao.addUsers(users);
    }
}

其中修改方法通过id查询某条记录用注解的方式实现(试试不同的方式)在mapper层

package com.seecen.mapper;

import com.seecen.entity.Users;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
public interface UserMapper {
     @Select("SELECT * FROM iris_users WHERE id = #{id}")
     Users selectUser(@Param("id") int id);


    }

3.3 service层接口

package com.seecen.service;

import com.seecen.entity.Users;

import java.util.List;

public interface UsersServices {
        //查询
        public List<Users> queryUsers();

        //修改
        public Boolean updateUsers(int id,String name);

        //删除
        public Boolean delteUsers(int id);

        //添加
        public void addUsers(Users users);

}

3.4 dao层接口

package com.seecen.dao;

import com.seecen.entity.Users;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UsersDao {

    //查询
    public List<Users> queryUsers();

    //修改
    public Boolean updateUsers(int id,String name);

    //删除
    public Boolean deleteUsers(int id);

    //添加
    public void addUsers(Users users);
}

3.5 entity基层

package com.seecen.entity;


public class Users {

    private Integer id;
    private String name;
    private String sex;

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

springBoot项目下有个resources包,resources包下有三个包分别是mappers和templates以及static
mappers放xml配置文件文件中是sql语句

<?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.seecen.dao.UsersDao">

    <resultMap id="userMap"  type="com.seecen.entity.Users">
        <id column="id" property="id"  />
        <result column="name" property="name" ></result>
        <result column="sex" property="sex"></result>
    </resultMap>

    <select id="queryUsers" resultMap="userMap">
        select id, name, sex from iris_users order by id
    </select>

    <update id="updateUsers">
        update iris_users set name =#{param2} where id=#{param1}
    </update>

    <delete id="deleteUsers">
        DELETE from iris_users where id=#{param1}
    </delete>

    <insert id="addUsers" parameterType="com.seecen.entity.Users">
        insert into iris_users(id, name, sex) values (#{id},#{name},#{sex})
    </insert>

</mapper>

templates里面放的是html页面显示的页面都放中templates包中 页面不能直接访问templates包下的页面
controller层跳转页面直接跳转会到templates包中找
比如controller的return"updateUser";会直接在templates找到updateUser.html页面找不到会报错
模板可能不存在,或者任何配置的模板解析器都无法访问模板模板可能不存在,或者任何配置的模板解析器都无法访问模板
static里面放静态资源如js文件

4.页面,页面数据显示使用thymeleaf模板规范遍历数据
4.1主页

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<body>
<table>
    <tr>
        <td><a href="iris_users.html">添加</a></td>
    </tr>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>性别</th>
        <th>操作</th>
    </tr>
    <tr th:each="list : ${usersList}">
        <td th:text="${list.id}">1</td>
        <td th:text="${list.name}">撒贝宁</td>
        <td th:text="${list.sex}">男</td>
        <td><a th:href="@{/deleteUsers(id=${list.id})}">删除</a></td>
        <td><a th:href="@{/user(id=${list.id})}">修改</a></td>
    </tr>
</table>
</body>
</body>
</html>

4.2修改保存页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>修改页面</title>
</head>
<body>
<table>
    <div>
        <h1>修改页面</h1>
      <form action="/updateUsers">
    <tr>
        编号:<input th:value="${user.id}" name="id" readonly="readonly" style="background-color: darkgray"><br>
        姓名:<input th:value="${user.name}" name="name"><br>
        性别:<input th:value="${user.sex}" name="sex"><br>
            <input type="submit" value="点击保存"><br>
    </tr>
      </form>
    </div>
</table>
</body>
</html>

4.3.添加页面

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<!-- 头部 -->
<div class="header">
    <div class="header-content">
        <a id="left" href="https://waimai.meituan.com/">
            <div class="logo-pic"></div>
            <div class="waimai">
                <div class="waimai-title"></div>
                <div class="waimai-slogan"></div>
            </div>
        </a>
    </div>
</div>

<!--中间部分-->
<div id="qishou_content">
    <div id="qushou_content1">
        <form  name="form_1" id="form1"  onsubmit="return checkForm(this)" action="/addUsers" method="post"enctype="multipart/form-data">
            <table id="table1">
                <caption style="font-size: 16px; color:green; margin: 10px 0px;">添加页面</caption>
                <tr>
                    <td class="td1">编号</td>
                    <td><input class="input1" type="text" name="id"/></td>
                    <td ><span id="phoneNumber_2"></span></td>
                </tr>
                <tr>
                    <td class="td1">姓名</td>
                    <td><input class="input1" type="text" name="name"/></td>
                    <td ><span id="phoneNumber_3"></span></td>
                </tr>
                <tr>
                    <td class="td1">性别</td>
                    <td><input class="input1" type="text" name="sex"/></td>
                    <td ><span id="phoneNumber_1"></span></td>
                </tr>
                <tr>
                    <td colspan="2"> <input type="submit" id="submit1" value="提交"/></td>
                </tr>
            </table>
        </form>
    </div>
</div>
</body>
</html>

5.0效果.浏览器直接输入http://localhost:8080/iris访问查询所有数据的方法
结果:在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值