spring集合mybatis的入门demo
创建项目
-
这里使用idea创建项目,选择java8版本
-
导入对应maven坐标,由于我的mysql版本较高此处导入8.0.11版本,具体版本根据运行的mysql而定
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-spring适配器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.26</version>
</dependency>
</dependencies>
- 创建基础目录结构
由上图可看出使用了application.yml文件对项目进行配置,我将application.properties文件内的内容全部注释了 application.yml文件:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///test?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: 123456
mybatis:
typeAliasesPackage: com.hyx.springbootdemo.entity
mapperLocations: classpath:mapper/*Mapper.xml
- 项目执行类如下配置
@SpringBootApplication
@ComponentScan(basePackages = {"com.hyx.springbootdemo.controller","com.hyx.springbootdemo.service"})
@MapperScan(basePackages = {"com.hyx.springbootdemo.mapper"})
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
- controller文件编写
@RestController
public class DemoController {
@Autowired
private UserService userService;
@RequestMapping("/index")
public Map index() {
Map map = new HashMap();
map.put("name","大田");
map.put("type","程序猿");
map.put("sex","未知");
return map;
}
}
- 关于entity类与mapper,service类推荐根据自己的数据库表,与逻辑进行编写,我这里是进行了分页操作的demo,以下为示例
entity:
public class UserEntity implements Serializable {
private Integer id;
private String name;
private String password;
private String username;
}
mapper:
public interface UserMapper {
// @Select("select * from user where id=#{id}")
UserEntity selectUserById(Integer id);
// @Select("select * from user where name=#{name}")
List<UserEntity> selectUserByName(String name);
// 根据数组进行分页
// @Select("select * from user")
List<Map<String,Object>> selectUserByArray();
// 运用sql进行分页
List<Map<String,Object>> selectUserBySQL(Map<String, Object> map);
}
service实现类,service实现类需要注释@Service,引入的mapper也需要注释@Autowired:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
// 通过接收currPage参数表示显示第几页的数据,pageSize表示每页显示的数据条数。
@Override
public List<Map<String, Object>> selectUserByArray(int currPage, int pageSize) {
List<Map<String, Object>> maps = userMapper.selectUserByArray();
// 从第几条数据开始
int firstIndex = (currPage - 1) * pageSize;
// 到第几条数据结束
int lastIndex = currPage * pageSize;
return maps.subList(firstIndex, lastIndex);
}
@Override
public List<Map<String, Object>> selectUserBySQL(int currPage, int pageSize) {
Map<String, Object> data = new HashMap<String, Object>();
data.put("currIndex", (currPage - 1) * pageSize);
data.put("pageSize", pageSize);
return userMapper.selectUserBySQL(data);
}
}
- mapper文件对应的xml,该文件位置应该在yml配置文件所指定的目录下,用指定的命名
<?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.hyx.springbootdemo.mapper.UserMapper">
<resultMap id="userMap" type="java.util.HashMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<result property="username" column="username"/>
</resultMap>
<!-- 根据 id 查询 user 表中的数据
id:唯一标识符,此文件中的id值不能重复
resultType:返回值类型,一条数据库记录也就对应实体类的一个对象
parameterType:参数类型,也就是查询条件的类型
-->
<select id="selectUserById"
resultType="com.hyx.springbootdemo.entity.UserEntity" parameterType="int">
<!-- 这里和普通的sql 查询语句差不多,对于只有一个参数,后面的 #{id}表示占位符,里面不一定要写id,写啥都可以,但是不要空着,如果有多个参数则必须写pojo类里面的属性 -->
select * from user where id = #{id}
</select>
<select id="selectUserByArray" resultMap="userMap" >
select * from user
</select>
<select id="selectUserBySQL" resultMap="userMap" parameterType="map" >
select * from user limit #{currIndex} , #{pageSize}
</select>
<!-- 查询 user 表的所有数据
注意:因为是查询所有数据,所以返回的应该是一个集合,这个集合里面每个元素都是User类型
-->
<select id="selectUserByName" resultType="com.hyx.springbootdemo.entity.UserEntity" parameterType="String">
select * from user where `name`= #{name}
</select>
<!-- 模糊查询:根据 user 表的username字段
下面两种写法都可以,但是要注意
1、${value}里面必须要写value,不然会报错
2、${}表示拼接 sql 字符串,将接收到的参数不加任何修饰拼接在sql语句中
3、使用${}会造成 sql 注入
-->
<select id="selectLikeUserName" resultType="com.hyx.springbootdemo.entity.UserEntity" parameterType="String">
select * from user where username like '%${value}%'
<!-- select * from user where username like #{username} -->
</select>
<!-- 向 user 表插入一条数据 -->
<insert id="insertUser" parameterType="com.hyx.springbootdemo.entity.UserEntity">
insert into user(id,username,sex,birthday,address)
value(#{id},#{username},#{sex},#{birthday},#{address})
</insert>
<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.hyx.springbootdemo.entity.UserEntity">
update user set username=#{username} where id=#{id}
</update>
<!-- 根据 id 删除 user 表的数据 -->
<delete id="deleteUserById" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
SpringBoot常用注释
Bean相关
@Autowired 自动注入
自动导入对象到类中,被注入进的类同样要被 Spring 容器管理比如:Service 类注入到 Controller 类中。
@Component,@Repository,@Service, @Controller 注册为bean
@Component:通用的注解,可标注任意类为 Spring 组件。如果一个 Bean 不知道属于哪个层,可以使用@Component 注解标注。@Repository: 对应持久层即 Dao 层,主要用于数据库相关操作。@Service: 对应服务层,主要涉及一些复杂的逻辑,需要用到 Dao 层。@Controller: 对应 Spring MVC 控制层,主要用户接受用户请求并调用 Service 层返回数据给前端页面。
这几个注解功能一样,只是用于区别不同层
@RestController 控制器注解
@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器 bean
@Scope 设置bean的作用域默认为单例
四种常见的 Spring Bean 的作用域:
singleton: 唯一 bean 实例,Spring 中的 bean 默认都是单例的。(默认)prototype: 每次请求都会创建一个新的 bean 实例。request: 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。session: 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。
@Configuration
一般用来声明配置类,可以使用 @Component注解替代,不过使用Configuration注解声明配置类更加语义化。
前后台数据传输相关
http请求处理注解
@RequestMapping("/index")
@GetMapping
@PutMapping
@DeleteMapping
@PostMapping
@PatchMapping
- GET :请求从服务器获取特定资源。举个例子:GET /users(获取所有学生)
- POST :在服务器上创建一个新的资源。举个例子:POST /users(创建学生)
- PUT :更新服务器上的资源(客户端提供更新后的整个资源)。举个例子:PUT /users/12(更新编号为 12 的学生)
- DELETE :从服务器删除特定的资源。举个例子:DELETE /users/12(删除编号为 12 的学生)
- PATCH :更新服务器上的资源(客户端提供更改的属性,可以看做作是部分更新)
接收数据@PathVariable 和 @RequestParam
@PathVariable用于获取路径参数
@RequestMapping(value = "user/{username}")
public String userProfile(@PathVariable(value="username") String username) {
return username;
}
userProfile的参数username会自动设置为URL中对应变量username(同名赋值)的值。例如,当HTTP请求为/user/fpc,URL变量username的值fpc就会被赋值给函数参数username。
@RequestParam获取url后续拼接的数据,也就是get请求后的数据,key1=value1&key2=value2这样的参数列表。通过注解@RequestParam可以轻松地将URL中的参数绑定到处理函数方法的变量中:
@RequestMapping(value="/user")
public String getUserBlog(@RequestParam(value="id") int blogId) {
return "blogId="+blogId;
}
一旦我们在方法中定义了@RequestParam变量,如果访问的URL中不带有相应的参数,就会抛出异常——这是显然的,Spring尝试帮我们进行绑定,然而没有成功。但有的时候,参数确实不一定永远都存在,这时我们可以通过定义required属性:
@RequestParam(value = "id", required = false, defaultValue = "0")
defaultValue为默认值
@RequestBody 获取请求体内数据
用于读取 Request 请求(可能是 POST,PUT,DELETE,GET 请求)的 body 部分并且Content-Type 为 application/json 格式的数据,接收到数据之后会自动将数据绑定到 Java 对象上去。系统会使用HttpMessageConverter或者自定义的HttpMessageConverter将请求的 body 中的 json 字符串转换为 java 对象。 一个请求方法只可以有一个@RequestBody,但是可以有多个@RequestParam和@PathVariable。
@RequestMapping("/addReportDo")
@ResponseBody
public String addReportDo(String title,String note){
return "ok";
}
读取配置信息
@value
使用 @Value("${property}") 读取比较简单的配置信息:
@ConfigurationProperties
通过@ConfigurationProperties读取配置信息并与 bean 绑定。
PropertySource
@PropertySource读取指定 properties 文件 注:读取配置文件的优先级
参数校验
一些常用的字段验证的注解
- @NotEmpty 被注释的字符串的不能为 null 也不能为空
- @NotBlank 被注释的字符串非 null,并且必须包含一个非空白字符
- @Null 被注释的元素必须为 null
- @NotNull 被注释的元素必须不为 null
- @AssertTrue 被注释的元素必须为 true
- @AssertFalse 被注释的元素必须为 false
- @Pattern(regex=,flag=)被注释的元素必须符合指定的正则表达式
- @Email 被注释的元素必须是 Email 格式。
- @Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
- @Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值
- @DecimalMin(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
- @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
- @Size(max=, min=)被注释的元素的大小必须在指定的范围内
- @Digits (integer, fraction)被注释的元素必须是一个数字,其值必须在可接受的范围内
- @Past被注释的元素必须是一个过去的日期
- @Future 被注释的元素必须是一个将来的日期
验证请求体(RequestBody)
我们在需要验证的参数上加上了@Valid注解,如果失败,它将抛出MethodArgumentNotValidException
验证请求参数(Path Variables 和 Request Parameters)
一定一定不要忘记在类上加上 @Validated 注解了,这个参数可以告诉 Spring 去校验方法参数。
全局异常处理
@ControllerAdvice :注解定义全局异常处理类 @ExceptionHandler :注解声明异常处理方法
@ControllerAdvice
public class MyGlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ModelAndView customException(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("message", e.getMessage());
mv.setViewName("myerror");
return mv;
}
}
在该类中,可以定义多个方法,不同的方法处理不同的异常,例如专门处理空指针的方法、专门处理数组越界的方法...,也可以直接向上面代码一样,在一个方法中处理所有的异常信息。
@ExceptionHandler 注解用来指明异常的处理类型,即如果这里指定为 NullpointerException,则数组越界异常就不会进到这个方法中来。
JPA 相关
创建表
@Entity声明一个类对应一个数据库实体。
@Table 设置表名
创建主键
@Id :声明一个字段为主键。
使用@Id声明之后,我们还需要定义主键的生成策略。我们可以使用 @GeneratedValue 指定主键生成策略。
/**
* 使用一个特定的数据库表格来保存主键
* 持久化引擎通过关系数据库的一张特定的表格来生成主键,
*/
TABLE,
/**
*在某些数据库中,不支持主键自增长,比如Oracle、PostgreSQL其提供了一种叫做"序列(sequence)"的机制生成主键
*/
SEQUENCE,
/**
* 主键自增长
*/
IDENTITY,
/**
*把主键生成策略交给持久化引擎(persistence engine),
*持久化引擎会根据数据库在以上三种主键生成 策略中选择其中一种
*/
AUTO(默认)
设置字段类型
@Column 声明字段。
指定不持久化特定字段
@Transient :声明不需要与数据库映射的字段,在保存的时候不需要保存进数据库 。
声明大字段
@Lob:声明某个字段为大字段。
创建枚举类型的字段
可以使用枚举类型的字段,不过枚举字段要用@Enumerated注解修饰。
删除/修改数据
@Modifying 注解提示 JPA 该操作是修改操作,注意还要配合@Transactional注解使用。
关联关系
@OneToOne 声明一对一关系
@OneToMany 声明一对多关系
@ManyToOne声明多对一关系
@MangToMang声明多对多关系
事务 @Transactional
在要开启事务的方法上使用@Transactional注解即可! tips:事务失效的情况:
- @Transactional 应用在非 public 修饰的方法上
- @Transactional 注解属性 propagation 设置错误,若是错误的配置以下三种 propagation,事务将不会发生回滚。
- TransactionDefinition.PROPAGATION_SUPPORTS
- TransactionDefinition.PROPAGATION_NOT_SUPPORTED
- TransactionDefinition.PROPAGATION_NEVER
- @Transactional 注解属性 rollbackFor 设置错误Spring默认,未检查unchecked异常(继承自 RuntimeException 的异常)或者 Error才回滚事务
- 同一个类中方法调用,导致@Transactional失效
比如有一个类Test,它的A方法调用本类的方法B(不论方法B是用public还是private修饰),但方法A没有声明注解事务,而B方法有。则外部调用方法A之后,方法B的事务是不会起作用的。
- 异常被你的 catch“吃了”导致@Transactional失效
- 数据库引擎不支持事务(myisam)
过滤Json
@JsonIgnore注解用来忽略某些字段,可以用在变量或者Getter方法上,用在Setter方法时,和变量效果一样。这个注解一般用在我们要忽略的字段上。
@JsonIgnoreProperties(ignoreUnknown = true),将这个注解写在类上之后,就会忽略类中不存在的字段。这个注解还可以指定要忽略的字段,例如@JsonIgnoreProperties({ “password”, “secretKey” })
@JsonFormat可以帮我们完成格式转换。例如对于Date类型字段,如果不适用JsonFormat默认在rest返回的是long,如果我们使用@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm:ss”),就返回"2018-11-16 22:58:15"
@Data
@JsonIgnoreProperties(value = {"fullName", "comment"})
public class User {
private String id;
private String name;
private String fullName;
private String comment;
private String mail;
@JsonIgnore
private String address;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date regDate;
private Date reg2Date;
}
- @JsonIgnoreProperties 作用在类上用于过滤掉特定字段不返回或者不解析。
- @JsonIgnore一般用于类的属性上,作用和上面的@JsonIgnoreProperties 一样。
- 格式化 json 数据:@JsonFormat
- 扁平化对象:@JsonUnwrapped
测试相关
@ActiveProfiles一般作用于测试类上, 用于声明生效的 Spring 配置文件。@Test声明一个方法为测试方法@Transactional被声明的测试方法的数据会回滚,避免污染测试数据。@WithMockUserSpring Security 提供的,用来模拟一个真实用户,并且可以赋予权限。
属性注入
注解注入
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfiguration {
@Value("${jdbc.url}")
String url;
@Value("${jdbc.driverClassName}")
String driverClassName;
@Value("${jdbc.username}")
String username;
@Value("${jdbc.password}")
String password;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setDriverClassName(driverClassName);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
@Configuration:声明JdbcConfiguration是一个配置类。@PropertySource:指定属性文件的路径是:classpath:jdbc.properties- 通过
@Value为属性注入值。 - 通过
@Bean将 dataSource()方法声明为一个注册Bean的方法,Spring会自动调用该方法,将方法的返回值加入Spring容器中。相当于以前的bean标签 - 然后就可以在任意位置通过@Autowired注入DataSource了!
配置文件注入
- 配置文件信息
# 测试注入
jdbc:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql:///test?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: 123456
- 对应类
@ConfigurationProperties(prefix = "jdbc") //这里需要定义出在application文件中定义属性值得前缀信息
public class JdbcProperties {
private String url;
private String driverClassName;
private String username;
private String password;
//省略get,set方法
}
- 注入类
@EnableConfigurationProperties(JdbcProperties.class)
public class DemoController {
@Autowired
private JdbcProperties jdbcProperties;
}
构造函数注入
将上面例子中的注入类修改为
@EnableConfigurationProperties(JdbcProperties.class)
public class DemoController {
// @Autowired
private JdbcProperties jdbcProperties;
DemoController(JdbcProperties jdbcProperties){
this.jdbcProperties=jdbcProperties;
}
}
Bean注入
这里会把返回值注册为bean,同时jdbcProperties也会被注入到参数中提供给方法使用
@Bean
public UserEntity getjdbc(JdbcProperties jdbcProperties){
System.out.println(jdbcProperties.getUrl());
UserEntity userEntity = new UserEntity();
return userEntity;
}
无变量注解注入
直接将配置注解添加到方法上,这是因为DataSource内部也是有set方法.进行自动注入.但是,也是需要有前提的:必须保证注入的有set方法,并且set方法的名字和配制文件中的属性名需要是一样的.这里使用的是datasource中内部的set方法
springboot常用配置(不完全)
mvc
设定请求的超时时间
spring.mvc.async.request-timeout
设定请求的超时时间,以毫秒为单位,如果没有设置的话,以具体实现的超时时间为准,比如tomcat的servlet3的话是10秒.
设定日期的格式
spring.mvc.date-format
设定日期的格式,比如dd/MM/yyyy.
关闭springboot默认图标
- 默认的Spring Boot提供了一个默认的Favicon,每次访问应用的时候都能看到
- 关闭Favicon我们可以在application.properties中设置关闭Favicon,默认为开启。
spring.mvc.favicon.enabled=false - 设置自己的Favicon
若需要设置自己的Favicon,则只需将自己的favicon.ico(文件名不能变动)文件放置在类路径根目录、类路径META-INF/resources/下、类路径resources/下、类路径static/下或类路径public/下。这里将favicon.ico放置在src/main/resources/static下。
重定向时忽略默认model的内容
spring.mvc.ignore-default-model-on-redirect
在重定向时是否忽略默认model的内容,默认为true
指定使用的Locale.
spring.mvc.locale
指定使用的Locale.
指定message codes(讯息码)的格式化策略
spring.mvc.message-codes-resolver-format
指定message codes的格式化策略(PREFIX_ERROR_CODE,POSTFIX_ERROR_CODE).
指定mvc视图的前缀
spring.mvc.view.prefix
指定mvc视图的前缀.
指定mvc视图的后缀
spring.mvc.view.suffix
指定mvc视图的后缀.
messages
指定message的basename(包名)
spring.messages.basename
指定message的basename,多个以逗号分隔,如果不加包名的话,默认从classpath路径开始,默认: messages
设定加载的资源文件缓存失效时间
spring.messages.cache-seconds
设定加载的资源文件缓存失效时间,-1的话为永不过期,默认为-1
Message bundles的编码
spring.messages.encoding
设定Message bundles的编码,默认: UTF-8
mobile
fallback的解决方案是否开启
spring.mobile.devicedelegatingviewresolver.enable-fallback
是否支持fallback的解决方案,默认false
是否开始device view resolver(设备视图解析器)
spring.mobile.devicedelegatingviewresolver.enabled
是否开始device view resolver,默认为: false
设定mobile端视图的前缀
spring.mobile.devicedelegatingviewresolver.mobile-prefix
设定mobile端视图的前缀,默认为:mobile/
设定mobile视图的后缀
spring.mobile.devicedelegatingviewresolver.mobile-suffix
设定mobile视图的后缀
设定普通设备的视图前缀
spring.mobile.devicedelegatingviewresolver.normal-prefix
设定普通设备的视图前缀
设定普通设备视图的后缀
spring.mobile.devicedelegatingviewresolver.normal-suffix
设定普通设备视图的后缀
设定平板设备视图前缀
spring.mobile.devicedelegatingviewresolver.tablet-prefix
设定平板设备视图前缀,默认:tablet/
设定平板设备视图后缀
spring.mobile.devicedelegatingviewresolver.tablet-suffix
设定平板设备视图后缀.
是否启用SitePreferenceHandler(网站偏好处理程序)
spring.mobile.sitepreference.enabled
是否启用SitePreferenceHandler,默认为: true
view
设定mvc视图的前缀
spring.view.prefix
设定mvc视图的前缀.
设定mvc视图的后缀
spring.view.suffix
设定mvc视图的后缀.
resource
是否开启默认的资源处理
spring.resources.add-mappings
是否开启默认的资源处理,默认为true
设定资源的缓存时效
spring.resources.cache-period
设定资源的缓存时效,以秒为单位.
是否开启缓存
spring.resources.chain.cache
是否开启缓存,默认为: true
是否开启资源handling chain
spring.resources.chain.enabled
是否开启资源 handling chain,默认为false
是否开启h5应用的cache manifest重写
spring.resources.chain.html-application-cache
是否开启h5应用的cache manifest重写,默认为: false
是否开启内容版本策略
spring.resources.chain.strategy.content.enabled
是否开启内容版本策略,默认为false
指定要应用的版本的路径
spring.resources.chain.strategy.content.paths
指定要应用的版本的路径,多个以逗号分隔,默认为:[/**]
是否开启固定的版本策略
spring.resources.chain.strategy.fixed.enabled
是否开启固定的版本策略,默认为false
应用版本策略的路径
spring.resources.chain.strategy.fixed.paths
指定要应用版本策略的路径,多个以逗号分隔
版本策略使用的版本号
spring.resources.chain.strategy.fixed.version
指定版本策略使用的版本号
静态资源路径
spring.resources.static-locations
指定静态资源路径,默认为classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/
multipart
文件上传支持
multipart.enabled
是否开启文件上传支持,默认为true
文件写入磁盘的阈值
multipart.file-size-threshold
设定文件写入磁盘的阈值,单位为MB或KB,默认为0
文件上传路径
multipart.location
指定文件上传路径.
文件大小最大值
multipart.max-file-size
指定文件大小最大值,默认1MB
每次请求的最大值
multipart.max-request-size
指定每次请求的最大值,默认为10MB
freemarker
HttpServletRequest的属性是否可以覆盖model的同名项
spring.freemarker.allow-request-override
指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
HttpSession的属性是否可以覆盖model的同名项
spring.freemarker.allow-session-override
指定HttpSession的属性是否可以覆盖controller的model的同名项
是否开启template caching
spring.freemarker.cache
是否开启template caching.
设定Template的编码
spring.freemarker.charset
设定Template的编码.
检查templates路径
spring.freemarker.check-template-location
是否检查templates路径是否存在.
设定Content-Type
spring.freemarker.content-type
设定Content-Type.
是否允许mvc使用freemarker
spring.freemarker.enabled
是否允许mvc使用freemarker.
request的属性是否要都添加到model中
spring.freemarker.expose-request-attributes
设定所有request的属性在merge到模板的时候,是否要都添加到model中.
HttpSession的属性是否要都添加到model中
spring.freemarker.expose-session-attributes
设定所有HttpSession的属性在merge到模板的时候,是否要都添加到model中.
暴露RequestContext
spring.freemarker.expose-spring-macro-helpers暴露RequestContext
设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
是否优先从文件系统加载template
spring.freemarker.prefer-file-system-access
是否优先从文件系统加载template,以支持热加载,默认为true
freemarker模板的前缀.
spring.freemarker.prefix
设定freemarker模板的前缀.
RequestContext属性的名
spring.freemarker.request-context-attribute
指定RequestContext属性的名.
设定FreeMarker keys
spring.freemarker.settings
设定FreeMarker keys.
设定模板的后缀.
spring.freemarker.suffix
设定模板的后缀.
设定模板的加载路径
spring.freemarker.template-loader-path
设定模板的加载路径,多个以逗号分隔,默认: ["classpath:/templates/"]
指定使用模板的视图列表
spring.freemarker.view-names
指定使用模板的视图列表.
参考文章:
可参考该文章的常用springboot配置 blog.csdn.net/Ru_yin_hai/…