SpringBoot 1.5.9

注:所有示例参考我的github仓库

1. Controller

Controller
public class TestController {
//展现视图的两种写法
//1 返回值直接是ModelAndView(如下)
//2 返回值是String视图名,但是函数第一个参数是Model可追加属性
//如果使用jsp做显示的话,需要添加tomcat的jsp支持的依赖
//如果没有用到jstl则可以不配置
@RequestMapping("/hello")
public ModelAndView hello(@RequestParam(value="name", required=false, defaultValue="World") String name) {
HashMap<String,Object> map=new HashMap<>();
map.put("name",name);
map.put("age",11);
return new ModelAndView("hello",map);
}
@RequestMapping("/json")
@ResponseBody
public ResponseEntity<Student> json(){
Clz clz=new Clz();
clz.setClzid(101);
clz.setClazname("一班");
Student student=new Student();
student.setName("小明");
student.setClz(clz);
return new ResponseEntity<Student>(student,HttpStatus.OK);
}
}

2. Filter

两种生效方式:

Component
Order(1)//越小优先
public class TestFilter implements Filter {...}

3. Thymeleaf

默认支持的模板,放到resource/templates.html。例如:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Hello Thymeleaf!</title>
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
<p th:text="'Your age is ' + ${age} + '!'" />
</body>
</html>

4. Dao

使用SpringData JPA
entity:

@Entity
@Data//引入lombok
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class Student {
@Id
private int sid;
private String name;
//多对一需要以下俩注解
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="cid")
//如果不想显示外联json则加以下俩注解详见仓库
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "cid")
@JsonIdentityReference(alwaysAsId = true)
private Claz claz;
}

repository:

public interface StudentDao extends JpaRepository<Student,Integer> {
}

5. SpringSecurity

参考gist和仓库代码。以下直接将gits内容贴过来。