spring-boot-tomcat-分页-部署到linux-nginx转发

1.demo结构:


图片.png

2.pom.xml



    4.0.0

    com.zy
    spring-boot01
    0.0.1-SNAPSHOT
    war

    spring-boot01
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.14.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            mysql
            mysql-connector-java
            5.1.45
        
        
            com.alibaba
            druid
            1.0.21
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
        
            com.github.pagehelper
            pagehelper
            5.1.2
        
        
            com.github.miemiedev
            mybatis-paginator
            1.2.15
        
        
        
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
            jstl
            jstl
            1.2
        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet
            jsp-api
            2.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3.application.yml

spring:
# 配置数据库连接
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/stu
    username: root
    password: 123456
# 配置jsp的解析
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp
# 配置HTML的解析
  thymeleaf:
    prefix: classpath:/templates/
logging:
  pattern:
    level: debug
  level:
    com.zy.controller: error
    com.zy.service: info
    com.zy.mapper: debug
  file: D:/springboot.log
# mybatis中,静态资源不发布:此处指的是resources文件夹下的文件
mybatis:
  mapper-locations: classpath:com/zy/mapper/*.xml
  config-location: classpath:sqlMappingConfig.xml
# 使用pageHelper进行分页
pagehelper:
    helperDialect: mysql
    reasonable: true
    support-methods-arguments: true
    params: count=countSql

4.启动类

package com.zy;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
/*配置事务的注解,同时也需要在service层方法上添加注解*/
@EnableTransactionManagement
/*整合mybatis*/
@MapperScan("com.zy.mapper")
/*@可配可不配ComponentScan(basePackages = {"com.zy.service","com.zy.controller"})*/
public class SpringBoot01Application {

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

5.config包下类

package com.zy.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class StaticResourceConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*配置js,css等*/
        registry.addResourceHandler("/js/**").addResourceLocations("WEB-INF/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("WEB-INF/css/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("WEB-INF/fonts/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        /*配置默认访问页:需要在浏览器中输入ip+端口号*/
        registry.addViewController("/").setViewName("forward:/index.jsp");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

6.util包下分页工具类

package com.zy.util;

import java.util.List;

public class StudentPageUtils {

    private long totalPages;
    private Integer pageSize;
    private Integer currentPage;
    private List rows;

    @Override
    public String toString() {
        return "StudentPageUtils{" +
                "totalPages=" + totalPages +
                ", pageSize=" + pageSize +
                ", currentPage=" + currentPage +
                ", rows=" + rows +
                '}';
    }

    public long getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(long totalPages) {
        this.totalPages = totalPages;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    public StudentPageUtils() {

    }

    public StudentPageUtils(long totalPages, Integer pageSize, Integer currentPage, List rows) {

        this.totalPages = totalPages;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.rows = rows;
    }
}

7.controller层

package com.zy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.model.Student;
import com.zy.service.StudentServiceI;
import com.zy.util.StudentPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("stu/")
public class StudentController {

    @Autowired
    private StudentServiceI studentServiceI;

    // 查询学生信息,包括多条件查询
    @RequestMapping("getStudentByCondition")
    public String getStudentByCondition(Map map, Student student, @RequestParam(defaultValue = "1") Integer pageNo) {
        PageHelper.startPage(pageNo, 2);
        List list = studentServiceI.getStudentByCondition(student);
        StudentPageUtils pageInfo = new StudentPageUtils();
        pageInfo.setRows(list);
        PageInfo pageInfoList = new PageInfo(list);
        pageInfo.setTotalPages(pageInfoList.getPages());
        pageInfo.setCurrentPage(pageNo);
        pageInfo.setPageSize(pageInfoList.getPageSize());
        map.put("pageInfo", pageInfo);
        map.put("stu",student);
        return "stu/stuList";
    }

    // 删除学生信息
    @RequestMapping("deleteStu")
    public String deleteStu(Integer id) {
        studentServiceI.deleteStu(id);
        return "redirect:/index.jsp";
    }

    // 增加学生信息
    @RequestMapping("addStu")
    public String addStu(Student student) {
        studentServiceI.addStu(student);
        return "redirect:/index.jsp";
    }

    // 更新学生信息
    @RequestMapping("updateStu")
    public String updateStu(Student student){
        studentServiceI.updateStu(student);
        return "redirect:/index.jsp";
    }

    // 根据id查询学生信息
    @RequestMapping("getStudentById")
    public String getStudentById(Integer id,Map map){
        Student student = new Student();
        student.setId(id);
        List list = studentServiceI.getStudentByCondition(student);
        StudentPageUtils pageInfo = new StudentPageUtils();
        pageInfo.setRows(list);
        map.put("pageInfo", pageInfo);
        return "stu/updateStu";
    }
}

8.service层实现类

package com.zy.service.impl;

import com.zy.mapper.StudentMapper;
import com.zy.model.Student;
import com.zy.service.StudentServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service(value="StudentServiceI")
/*配置事务注解*/
public class StudentServiceImpl implements StudentServiceI {

    // 查询学生信息,多条件查询
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public List getStudentByCondition(Student student) {
        return studentMapper.getStudentByCondition(student);
    }

    // 删除学生
    @Override
    public void deleteStu(Integer id) {
        studentMapper.deleteStu(id);
    }

    // 增加学生信息
    @Override
    public int addStu(Student student) {
        int id = studentMapper.addStu(student);
        return id;
    }

    // 修改学生信息
    @Override
    public void updateStu(Student student) {
        studentMapper.updateStu(student);
    }
}

9.mapper层接口

package com.zy.mapper;

import com.zy.model.Student;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface StudentMapper {

    // 查询学生信息,多条件查询
    List getStudentByCondition(Student student);

    // 删除学生
    void deleteStu(Integer id);

    // 增加学生信息
    int addStu(Student student);

    // 修改学生信息
    void updateStu(Student student);
}

10.model层实体类

package com.zy.model;

import java.io.Serializable;

public class Student implements Serializable {

    private Integer id;
    private String name;
    private Integer age;
    private String gender;
    private String addr;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", addr='" + addr + '\'' +
                '}';
    }

    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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public Student() {

    }

    public Student(Integer id, String name, Integer age, String gender, String addr) {

        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.addr = addr;
    }
}

11.resources下的mybatis全局配置文件sqlMappingConfig.xml




    
    
        
        
        
        
        
        
    
    
    
        
    

12.mybatis其他文件





    
    
        
        
        
        
        
    

    
    

    
    
        delete from stu
        where id = #{id}
    

    
    
        
            select last_insert_id()
        
        insert into stu values(null,#{name},#{age},#{gender},#{addr})
    

    
    
        update stu
        set name=#{name},age=#{age},gender=#{gender},addr=#{addr}
        where id=#{id}
    


13.webapp下的index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

14.WEB-INF下jsp文件


图片.png

14.1stuList.jsp

<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2018/6/26
  Time: 18:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


    
    
    
    
    
    
    学生信息
    



<%--
--%>
id: name: age: gender: addr:
学号 姓名 年龄 性别 地址 操作
${stu.id} ${stu.name} ${stu.age} ${stu.gender} ${stu.addr} 更新 删除

14.2updateStu.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2018/6/26
  Time: 20:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    Title
    
    
    
    
    


修改客户

1.15发布到linux服务器
1.15.1使用外部tomcat发布

        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        

执行打包指令(注意,这里有web页面,所以是war包)

maven clean install -DskipTests=true
图片.png

然后,将该war包上传到tomcat的webapps目录下-->到tomcat的bin目录下: ./startup.sh启动即可
访问方式:ip:port/项目名/路径名
如:此处为:l92.168.0.199:8080/spring-boot01-0.0.1-SNAPSHOT/index.jsp
1.16采用nginx代理,转发请求

    #将/hello请求转发给http://l92.168.0.199:8080/spring-boot01-0.0.1-SNAPSHOT处理(假设server的port是8081)
    location /hello{
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://l92.168.0.199:8080/spring-boot01-0.0.1-SNAPSHOT;
    }

如果用户在浏览器输入:

http://192.168.0.199:8081/hello/toJSP

那么实际的访问地址是:

http://l92.168.0.199:8081/spring-boot01-0.0.1-SNAPSHOT/toJSP

你可能感兴趣的:(spring-boot-tomcat-分页-部署到linux-nginx转发)