一、创建Mean 项目 这个就不多说了
二、我的spring boot demo 结构 如下:
三、主要的配置文件(application.properties ;pom.xml ; log4j2.xml)
(1、)application.properties 文件
#server
server.port=8888
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8
#datasource
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/studentmanage?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=12
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=150
# 配置获取连接等待超时的时间
spring.datasource.maxWait=0
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#jap
spring.jpa.properties.hibernate.hbm2ddl.auto=update
#spring.jpa.show-sql=true
#上传大小配置
spring.http.multipart.max-file-size=30Mb
spring.http.multipart.max-request-size=30Mb
#FreeMarker FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=true
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.request-context-attribute=request
spring.freemarker.prefer-file-system-access=true
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.number_format=0.##########
spring.freemarker.settings.datetime_format=yyyy-MM-dd HH:mm:ss
spring.freemarker.settings.classic_compatible=true
spring.freemarker.settings.template_exception_handler=ignore
spring.freemarker.order=1
#log
logging.config=classpath:log4j2.xml
4.0.0
com.myspringboot
myspringboot
0.0.1-SNAPSHOT
war
myspringboot
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
1.0.23
1.2.18
1.1.1
3.2.1
2.5
0.4.8
3.14
1.7
UTF-8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-test
com.lmax
disruptor
3.3.4
mysql
mysql-connector-java
com.alibaba
druid
${alibaba.druid.version}
com.alibaba
fastjson
${alibaba.fastjson.version}
org.springframework.boot
spring-boot-starter-freemarker
org.apache.commons
commons-lang3
${apache.commons-lang.version}
commons-io
commons-io
${apache.commons-io.version}
org.apache.poi
poi
${apache.poi.version}
org.apache.poi
poi-ooxml
${apache.poi.version}
net.coobird
thumbnailator
${thumbnailator.version}
myspringboot
org.springframework.boot
spring-boot-maven-plugin
maven-compiler-plugin
${java.version}
${project.build.sourceEncoding}
/tmp/logs
/tmp/logs/7z
package com.myspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
* @ClassName: Application
* @Description:项目启动入口
*/
//@EnableCaching //缓存
//@EnableScheduling //定时任务
//@EnableAsync //异步任务
@SpringBootApplication // 项目启动
//@ComponentScan("com.myspringboot")
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.myspringboot.model.StudentBasis;
import com.myspringboot.service.StudentBasisService;
@Controller
@RequestMapping(value = "/")
@ResponseBody
public class StudentController {
@Autowired
private StudentBasisService studentService;
@RequestMapping(value = "Student/index.html", method = RequestMethod.GET)
public ModelAndView getStudentIndex(HttpServletRequest request) throws Exception {
ModelAndView mv = new ModelAndView("/student/index");
List list = new ArrayList();
Map whereMap = new HashMap();
list = studentService.getList(whereMap);
System.out.println("获取列表成功!");
mv.addObject("studentlist", list);
return mv;
}
}
(6、)前台页面
<#assign boot = basePath >
学生列表
学生学号
学生姓名
手机号码
电子邮件
性别
操作
<#if (studentlist?? && studentlist?size>0)>
<#list studentlist as data>
${data.studentcode}
${data.studentname}
${data.mobile}
${data.email}
修改
#list>
#if>
(7、)如果在前台页面 要想与jsp 那样获取
request.getContextPath() 的效果
需要重新定义 拦截器类和注册类:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 拦截器类
* @author Michael
*
*/
public class CommonInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o)
throws Exception {
String path = httpServletRequest.getContextPath();
String scheme = httpServletRequest.getScheme();
String serverName = httpServletRequest.getServerName();
int port = httpServletRequest.getServerPort();
String basePath = scheme + "://" + serverName + ":" + port + path;
httpServletRequest.setAttribute("basePath", basePath);
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o, Exception e) throws Exception {
}
}
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 注册类
* @author Michael
*
*/
@Configuration
public class CommonInterceptorConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**");
}
}
前台页面引用如下: