使用SpringBoot+Mybatis+sqlServer简单的增删改查+html

新手编写,大佬勿喷~~

在这里插入图片描述

首先application.properties的配置

server.port=8080
spring.datasource.url=jdbc:sqlserver://172.0.0.1:8080;databasename=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations= classpath:mapper/*.xml
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/

Pom.xml

<?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 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>2.2.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.house</groupId>
	<artifactId>housetest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>housetest</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
 		
 		<!--引入thymeleaf依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>sqljdbc4</artifactId>
			<version>4.2</version>
		</dependency>

		<dependency>
			<groupId>org.jdom</groupId>
			<artifactId>jdom</artifactId>
			<version>1.1.3</version>
		</dependency>

		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.23</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-core</artifactId>
			<version>3.1.2</version>
		</dependency>
	</dependencies>

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

</project>

Controller控制层

package com.house.housetest.controller;

import com.house.housetest.dao.FunUsers;
import com.house.housetest.service.IFunUsersService;
import com.house.housetest.service.impl.FunUsersImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;


@Controller
@RequestMapping("/funUser")
public class FunUserController {

    @Autowired
    private IFunUsersService iFunUsers;

    private final Logger logger = LoggerFactory.getLogger(FunUsersImpl.class);


    @GetMapping("/all")
    public String all(Model model) {
        List<FunUsers> users = this.iFunUsers.queryAll();
        model.addAttribute("users", users);
        return "user/allUser";
    }



    @GetMapping("/getOne")
    public String getOne(FunUsers user){
        FunUsers usr = iFunUsers.queryUserByID(user.getUserId());
        return usr.toString();
    }

    /**
     * @Description: 根据id 删除
     */

    @RequestMapping(value = "delete/{userId}")
    public ModelAndView delete(@PathVariable Integer userId) {
        iFunUsers.removeById(userId);
        ModelAndView mav = new ModelAndView("redirect:/funUser/all");
        return mav;
    }


    /**
     * 添加用户
     * @param funUsers
     * @return
     */
    @RequestMapping("addUser")
    public ModelAndView AddUser(FunUsers funUsers) {
        iFunUsers.addUser(funUsers);
        ModelAndView mav = new ModelAndView("redirect:/funUser/all");
        return mav;
    }


    @RequestMapping("add")
    public ModelAndView Add1() {
        return new ModelAndView("/user/add");
    }


    @RequestMapping("edit")
    public String edit(Integer userId ,Model model){
        FunUsers userInfo = iFunUsers.queryUserByID(userId);

        model.addAttribute("userInfo",userInfo);
        return "user/edit";
    }



    /**
     * 更新用户
     * @param
     * @return
     */
    @RequestMapping("updateUsers")
    public ModelAndView updateUser(FunUsers funUsers) {
        int i = iFunUsers.updateById(funUsers);
        ModelAndView mav = new ModelAndView("redirect:/funUser/all");
        return mav;
    }
}

DAO层(我这边可能字段有点多)

package com.house.housetest.dao;

import java.math.BigDecimal;
import java.util.Date;

public class FunUsers {
    private Integer userId;

    private Short cityId;

    private Integer compId;

    private Integer regId;

    private Integer deptId;

    private Integer grId;

    private Integer archiveId;

    private String userNo;

    private String userName;  
}

Mapper

package com.house.housetest.mapper;

import com.house.housetest.dao.FunUsers;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface FunUsersMapper {
    /*
   查所有
   return List<User>
    */
    List<FunUsers> getAll();

    /*
    根据ID查询
     */
    FunUsers getUserByID(Integer userId);

    /*
    删除FunUsers
     */
    void delete(Integer userId);

    /*
    更新FunUsers
     */
    int update (FunUsers users);

    /*
    新增FunUsers
     */
    int saveUser(FunUsers users);

}

Service

package com.house.housetest.service;

import com.house.housetest.dao.FunUsers;


import java.util.List;


public interface IFunUsersService {

    int addUser(FunUsers funUsers);
    int updateById(FunUsers funUsers);
    void removeById(Integer userId);
    FunUsers queryUserByID(Integer userId);
    List<FunUsers> queryAll();
}

Impl

package com.house.housetest.service.impl;

import com.house.housetest.dao.FunUsers;
import com.house.housetest.mapper.FunUsersMapper;
import com.house.housetest.service.IFunUsersService;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.FileOutputStream;
import java.sql.*;
import java.util.List;

@Service
public class FunUsersImpl implements IFunUsersService {


    @Autowired
    private FunUsersMapper funUsersMapper;

    @Override
    public int addUser(FunUsers funUsers) {
       return funUsersMapper.saveUser(funUsers);
    }

    @Override
    public int updateById(FunUsers funUsers) {
       return funUsersMapper.update(funUsers);
    }


    @Override
    public void removeById(Integer userId) {
        funUsersMapper.delete(userId);
    }

    @Override
    public FunUsers queryUserByID(Integer userId) {
        return funUsersMapper.getUserByID(userId);
    }

    @Override
    public List<FunUsers> queryAll() {
        return funUsersMapper.getAll();
    }
}

Mapper.xml

(这边有点多,自己用Mybatis生成器生成就行)

丑丑的前端页面

add.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <style type="text/css">
        table {border-collapse: collapse; font-size: 14px;
            width: 80%; margin: auto}
        table, th, td {border: 0px solid darkslategray;padding: 10px}
        .btgn{
            font-family: "Microsoft YaHei UI";
            font-size: 16px;
            color: #255e95;
            background-color: #99CCFF;
            text-align: center;
        }
        .ccc{
            font-family: "Microsoft YaHei UI";
            font-size: 16px;
            color: #255e95;
            background-color: #ff7c76;
            text-align: center;
        }

        tr:nth-child(2n){
            background-color:#FFCC99;
        }
        tr:nth-child(2n+1) {
            background-color: #99CC99;
        }

        .c{
            width: 200px;
        }

    </style>
</head>
<body>
<span class="span" style="color: darkslategray; font-size: 30px" >添加页面!</span>
<body>
<div style="margin:122px auto; width:392px">


    <form action="/funUser/addUser" method="post" class="c" th:object="${user}">
        <!-- <form action="AddUser" method="get">-->
        <tr><th class="btgn">用户ID:</th> <input name="userId" class="btn" th:value="${userId}"/><tr/><br/>
        <tr><th class="ccc">城市ID:</th> <input name="cityId"class="btn" th:value="${cityId}"/><tr/> <br/>
        <tr><th class="btgn">compId:</th> <input name="compId" class="btn"th:value="${compId}"/> </tr><br/>
        <tr><th class="ccc">regId:</th> <input name="regId"class="btn" th:value="${regId}"/> </tr><br/>
        <tr><th class="ccc">deptId:</th> <input name="deptId"class="btn" th:value="${deptId}"/> </tr><br/>
        <tr><th class="ccc">grId:</th> <input name="grId"class="btn" th:value="${grId}"/> </tr><br/>
        <tr><th class="ccc">archiveId:</th> <input name="archiveId"class="btn" th:value="${archiveId}"/> </tr><br/>
        <tr><th class="ccc">用户编号:</th> <input name="userNo"class="btn" th:value="${userNo}"/> </tr><br/>
        <tr><th class="ccc">用户名称:</th> <input name="userName"class="btn" th:value="${userName}"/> </tr><br/>

        <button type="submit">提交</button>

    </form>
</div>
</body>

</html>


allUser.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <style type="text/css">
        table {border-collapse: collapse; font-size: 14px;
            width: 80%; margin: auto}
        table, th, td {border: 0px solid darkslategray;padding: 10px}
        .btgn{
            font-family: "Microsoft YaHei UI";
            font-size: 16px;
            color: #255e95;
            background-color: #99CCFF;
            text-align: center;
        }

        tr:nth-child(2n){
            background-color: #fffdf5;
        }
        tr:nth-child(2n+1) {
            background-color: #f2fbfb;
        }

    </style>
</head>
<body>
<div style="text-align: center">
    <span class="span" style="color: darkslategray; font-size: 30px" >好房通用户查询</span>
    <hr/>
    <table class="list">

        <tr>
            <th class="btgn">id</th>
            <th class="btgn">城市</th>
            <th class="btgn">姓名</th>
            <th class="btgn">编号</th>
            <th class="btgn">删除操作</th>
            <th class="btgn">修改操作</th>

        </tr>

        <tr th:each="user : ${users}">
            <td th:text="${user.userId}"></td>
            <td th:text="${user.cityId}"></td>
            <td th:text="${user.userName}"></td>
            <td th:text="${user.userNo}"></td>

            <!--<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">1980-02-30</td>-->
            <!--<td th:text="${user.phone}">1</td>-->
            <td><a th:href="@{'/funUser/delete/'+${user.userId}}"><input type="submit" value="删除用户"></a></td>
            <td><a th:href="@{'/funUser/edit?userId='+${user.userId}}"><input type="submit" value="修改用户"></a></td>
        </tr>
        <a  href="add"><input type="submit" value="新增用户"></a>
    </table>
</div>
</body>
</html>

edit.html

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <style type="text/css">
        table {border-collapse: collapse; font-size: 14px;
            width: 80%; margin: auto}
        table, th, td {border: 0px solid darkslategray;padding: 10px}
        .btgn{
            font-family: "Microsoft YaHei UI";
            font-size: 16px;
            color: #255e95;
            background-color: #99CCFF;
            text-align: center;
        }
        .ccc{
            font-family: "Microsoft YaHei UI";
            font-size: 16px;
            color: #255e95;
            background-color: #ff7c76;
            text-align: center;
        }

        tr:nth-child(2n){
            background-color:#FFCC99;
        }
        tr:nth-child(2n+1) {
            background-color: #99CC99;
        }

        .c{
            width: 200px;
        }

    </style>
</head>
<body>
<span class="span" style="color: darkslategray; font-size: 30px" >修改页面!!!!</span>
<body>
<div style="margin:122px auto; width:392px">


    <form action="/funUser/updateUsers" method="post" class="c" th:value="userInfo">
        <!-- <form action="AddUser" method="get">-->

        <tr><th class="btgn"></th> <input type="hidden" name="userId" class="btn"  th:value="${userInfo.userId}"/><tr/><br/>
        <tr><th class="ccc">城市ID:</th> <input name="cityId"class="btn" th:value="${userInfo.cityId}"/><tr/> <br/>
        <tr><th class="btgn">compId:</th> <input name="compId" class="btn"th:value="${userInfo.compId}"/> </tr><br/>
        <tr><th class="ccc">regId:</th> <input name="regId"class="btn" th:value="${userInfo.regId}"/> </tr><br/>
        <tr><th class="ccc">deptId:</th> <input name="deptId"class="btn" th:value="${userInfo.deptId}"/> </tr><br/>
        <tr><th class="ccc">grId:</th> <input name="grId"class="btn" th:value="${userInfo.grId}"/> </tr><br/>
        <tr><th class="ccc">archiveId:</th> <input name="archiveId"class="btn" th:value="${userInfo.archiveId}"/> </tr><br/>
        <tr><th class="ccc">用户编号:</th> <input name="userNo"class="btn" th:value="${userInfo.userNo}"/> </tr><br/>
        <tr><th class="ccc">用户名称:</th> <input name="userName"class="btn" th:value="${userInfo.userName}"/> </tr><br/>
        <button type="submit">提交啊</button>
    </form>
</div>
</body>
</html>
原创文章 2 获赞 3 访问量 149

猜你喜欢

转载自blog.csdn.net/silence1120/article/details/105790483