Springboot2+thymeleaf+mybatis +SQLServer页面展示

展示的页面

 

 项目结构

 

 导包

<?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.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.neu</groupId>
    <artifactId>springbootdemo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo1</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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!-- sqlserver -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId><!--sqlserver依赖  驱动jar-->
            <scope>runtime</scope>
            <version>6.4.0.jre8</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
            <scope>test</scope>
        </dependency>
        <!-- driud -->
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.20</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 8081
# DataSource Config

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    # 下面为连接池的补充设置,应用到上面所有数据源中
    # 初始化大小,最小,最大
    druid:
      #JDBC配置
      driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
      url: jdbc:sqlserver://192.168.0.111:1433; DatabaseName=Data_Export_4Common_Query
      username: aaa
      password: aaaa111
      initialSize: 5
      minIdle: 5
      maxActive: 20
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      #filters: stat,wall,log4j
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      # 合并多个DruidDataSource的监控数据
      useGlobalDataSourceStat: true
      
      

#mybatis:
  #mapper-locations: classpath:mapper/course/*.xml,classpath:mapper/pointsMall/*.xml,classpath:mapper/TClock/*.xml
  #type-aliases-package: com.high.school.entity

POJO

package com.neu.springbootdemo1.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private String line_no;
    private String line_name;
    private String bus_no;
    @DateTimeFormat(pattern = "yyyy--MM-dd hh:mm:ss" )
    private Date riding_time;
    private String name;
    private String tel_no;
    private String card_category;        

}

server

package com.neu.springbootdemo1.service;

import com.github.pagehelper.PageInfo;
import com.neu.springbootdemo1.entity.Emp;

import java.util.List;

/**
 * @Author huibao
 * @Create 2021-04-20 15:45
 */
public interface EmpService {
    public int insert(Emp emp);
    public int update(Emp emp);
    public int delete(Integer empno);

    public Emp getById(Integer empno);
    public List<Emp> getAll();

    public PageInfo<Emp> getPaged(int pageNum,int pageSize);
}

server.inpl

package com.neu.springbootdemo1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.springbootdemo1.entity.Emp;
import com.neu.springbootdemo1.mapper.EmpMapper;
import com.neu.springbootdemo1.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author huibao
 * @Create 2021-04-20 15:47
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public int insert(Emp emp) {
        return empMapper.insert(emp);
    }

    @Override
    public int update(Emp emp) {
        return empMapper.update(emp);
    }

    @Override
    public int delete(Integer empno) {
        return empMapper.delete(empno);
    }

    @Override
    public Emp getById(Integer empno) {
        return empMapper.getById(empno);
    }

    @Override
    public List<Emp> getAll() {
        return empMapper.getAll();
    }

    @Override
    public PageInfo<Emp> getPaged(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<Emp> emps = empMapper.getAll();

        PageInfo<Emp> pageInfo = new PageInfo<>(emps);

        return pageInfo;
    }
}

Mapper

package com.neu.springbootdemo1.mapper;

import com.neu.springbootdemo1.entity.Emp;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author huibao
 * @Create 2021-04-20 14:54
 */
public interface EmpMapper {
    //@Select("Select * from tbl_card_trade_info_temp")
    @Insert({"insert into tbl_card_trade_info_temp(empno,ename,job,mgr,hiredate,sal,comm,deptno) ",
            "values(#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{dept.deptno})"})
    public int insert(Emp emp);

    @Update({"update tbl_card_trade_info_temp set ename = #{ename},job=#{job},mgr=#{mgr},hiredate=#{hiredate}",
            ",sal=#{sal},comm=#{comm},deptno=#{dept.deptno}",
            " where empno = #{empno}"})
    public int update(Emp emp);

    @Delete("delete from tbl_card_trade_info_temp where empno = #{empno}")
    public int delete(Integer empno);

    @Select("select * from tbl_card_trade_info_temp where empno = #{line_no}")
    @Results(id="basicMap",value = {@Result(
            property = "dept",
            column = "deptno",
            one = @One(select = "com.neu.springbootdemo1.mapper.DeptMapper.getById")
    )})
    public Emp getById(Integer empno);

    @Select("select * from tbl_card_trade_info_temp")
    @ResultMap("basicMap")
    public List<Emp> getAll();
}

Controller

package com.neu.springbootdemo1.controller;

import com.github.pagehelper.PageInfo;
import com.neu.springbootdemo1.entity.Dept;
import com.neu.springbootdemo1.entity.Emp;
import com.neu.springbootdemo1.service.DeptService;
import com.neu.springbootdemo1.service.EmpService;
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 org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author huibao
 * @Create 2021-04-20 16:20
 */
@Controller
//@RequestMapping("emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    private DeptService deptService;

    @RequestMapping({"/","/emp/getPaged"})
    public String getPaged(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "20") int pageSize, Model model){
        PageInfo<Emp> pageInfo = empService.getPaged(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);

        return "emp/paged";
    }

    @RequestMapping("emp/delete")
    public String delete(Integer empno){
        int n = empService.delete(empno);

        return "redirect:/";
    }

    @RequestMapping("emp/edit")
    public String edit(Integer empno,Model model){
        Emp emp = empService.getById(empno);

        model.addAttribute("emp",emp);
        List<Dept> deptList = deptService.getAll();
        model.addAttribute("deptList",deptList);
        return "emp/edit";
    }
    @RequestMapping("emp/update")
    public String update(Emp emp){
        empService.update(emp);
        return "redirect:/";
    }
    @RequestMapping("emp/add")
    public String add(Model model){
        List<Dept> deptList = deptService.getAll();
        model.addAttribute("deptList",deptList);
        return "emp/add";
    }
    @RequestMapping("emp/insert")
    public String insert(Emp emp){
        int n = empService.insert(emp);
        return "redirect:/";
    }
}

main

package com.neu.springbootdemo1;

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

@SpringBootApplication
@MapperScan("com.neu.springbootdemo1.mapper")
public class Springbootdemo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Springbootdemo1Application.class, args);
    }

}

HTML

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>员工信息</title>
    <style>
        .tr{
            background-color: antiquewhite;
        }
    </style>
</head>
<body>
   <!--  <a th:href="|${#request.contextPath}/emp/add|">添加员工</a> -->
   <a><h1>消费信息查询Time is always too short for those who need it.
愈是需要时间,愈感时光易逝。</h1></a>
    <table width="1200" border="1">
        <thead>
            <tr>
                <td>线路号</td>
                <td>线路名</td>
                <td>车辆号</td>
                <td>乘车日期</td>
                <td>乘客姓名</td>
                <td>电话</td>
                <td>刷卡类型</td>
            </tr>
        </thead>
        <tbody>
            <tr th:each="emp,status : ${pageInfo.list}"
                th:object="${emp}"
                th:classappend="${status.odd}?'tr'"
            >
                <td th:text="*{line_no}"></td>
                <td th:text="*{line_name}"></td>
                <td th:text="*{bus_no}"></td>
                <td th:text="*{#dates.format(riding_time,'yyyy-MM-dd hh:mm:ss')}"></td>
                <!-- <td th:text="*{#dates.format(hiredate,'yyyy-MM-dd')}"></td> -->
                <td th:text="*{name}"></td>
                <td th:text="*{tel_no}"></td>
                <td th:text="*{card_category}"></td>
                <!-- <td th:text="*{dept.dname}"></td>
                <td>
                    <a href="#" th:onclick="del([[*{line_no}]])">删除</a>
                    <a href="#" th:href="|${#request.contextPath}/emp/edit?empno=*{line_no}|">编辑</a>
                </td> -->
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="9" align="center">
                    <a th:if="${ not pageInfo.isFirstPage}" th:href="|${#request.contextPath}/emp/getPaged?pageNum=1&pageSize=${pageInfo.pageSize}|">第一页</a>
                    <a th:if="${ not pageInfo.isFirstPage}" th:href="|${#request.contextPath}/emp/getPaged?pageNum=${pageInfo.pageNum-1}&pageSize=${pageInfo.pageSize}|">上一页</a>
                    <a href="#"
                       th:each="pageNum : ${#numbers.sequence(1,pageInfo.pages)}"
                       th:text="|[${pageNum}]|"
                       th:href="|${#request.contextPath}/emp/getPaged?pageNum=${pageNum}&pageSize=${pageInfo.pageSize}|"
                    ></a>
                    <a th:if="${ not pageInfo.isLastPage}" th:href="|${#request.contextPath}/emp/getPaged?pageNum=${pageInfo.pageNum+1}&pageSize=${pageInfo.pageSize}|">下一页</a>
                    <a th:if="${ not pageInfo.isLastPage}" th:href="|${#request.contextPath}/emp/getPaged?pageNum=${pageInfo.pages}&pageSize=${pageInfo.pageSize}|">末页</a>
                    到:<input type="text" size="1" id="pageNum" onkeydown="goPage()"> <input type="button" value="go" onclick="jumpToPage()">
                    <select onchange="changePageSize(this.value)">
                        <option value="3" th:selected="${pageInfo.pageSize==3}?true:false">3</option>
                        <option value="5" th:selected="${pageInfo.pageSize==5}?true:false">5</option>
                        <option value="8" th:selected="${pageInfo.pageSize==8}?true:false">8</option>
                    </select>
                    总计: <span th:text="${pageInfo.total}"></span></td>
            </tr>
        </tfoot>
    </table>
</body>
</html>
<!-- <script th:inline="javascript">
    function jumpToPage() {
        let pageNum = document.getElementById("pageNum").value;

        location.href = [[|${#request.contextPath}/emp/getPaged?pageNum=|]]+pageNum+"&pageSize="+[[${pageInfo.pageSize}]];
    }

    function changePageSize(pageSize) {
        location.href = [[|${#request.contextPath}/emp/getPaged?pageNum=1|]]+"&pageSize="+pageSize;
    }

    function goPage() {
        if(event.keyCode === 13){
            jumpToPage();
        }
    }

    function del(empno) {
        if(confirm(`是否删除id为:${empno}的员工?`)){
            location.href = [[|${#request.contextPath}/emp/delete?empno=|]]+empno;
        }
    }
</script> -->

 

posted @ 2022-03-29 16:15  dafengchui  阅读(54)  评论(0)    收藏  举报