package cn.cnyasin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class RestfulController {
/* 方式一 */
@RequestMapping("/rest/test/{a}/{b}")
/* 方式二 */
// @RequestMapping(path = "/rest/test/{a}/{b}", method = RequestMethod.GET)
/* 方式三 */
// @RequestMapping(value = "/rest/test/{a}/{b}", method = RequestMethod.GET)
/* 方式四 */
// @GetMapping("/rest/test/{a}/{b}")
// @PostMapping("/rest/test/{a}/{b}")
public String test(@PathVariable int a, @PathVariable int b, Model model) {
int sum = a + b;
model.addAttribute("msg", sum);
return "restful";
}
}
重定向redirect
语法:return "redirect:/foo/bar";
// @PostMapping("/rest/test/{a}/{b}")
public String test(@PathVariable int a, @PathVariable int b, Model model) {
int sum = a + b;
model.addAttribute("msg", sum);
//return "redirect:/index.jsp";
return "redirect:/rest/test2";
}
@GetMapping("/rest/test2")
public String test2(Model model) {
model.addAttribute("msg", "redirect ok...");
return "restful";
}
获取参数
建议所有参数都加上@RequestParam()
package cn.cnyasin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class EncodingController {
@RequestMapping("/encoding/test1")
public String test1(@RequestParam("data") String data, Model model) {
model.addAttribute("msg", data);
return "encoding";
}
}
package cn.cnyasin.pojo;
public class Book {
private int id;
private String name;
private int status;
private long createdAt;
// getter,setter...
}
cn.cnyasin.dao.BookMapper.java
package cn.cnyasin.dao;
import cn.cnyasin.pojo.Book;
import java.util.List;
public interface BookMapper {
// 增加
int insertOne(Book book);
// 修改
int updateOne(Book book);
// 删除
int deleteOne(int id);
// 一个
Book getOne(int id);
// 所有
List<Book> getAll();
}
cn.cnyasin.dao.BookMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cnyasin.dao.BookMapper">
<!--增加-->
<insert id="insertOne" parameterType="book">
insert into book (`name`, `status`, `created_at`)
values (#{name}, #{status}, #{createdAt})
</insert>
<!--修改-->
<update id="updateOne" parameterType="book">
update book set `name` = #{name} where id = #{id}
</update>
<!--删除-->
<delete id="deleteOne" parameterType="int">
delete from book where id = #{id}
</delete>
<!--一个-->
<select id="getOne" resultType="book">
select * from book where id = #{id}
</select>
<!--所有-->
<select id="getAll" resultMap="bookList">
select * from book
</select>
<resultMap id="bookList" type="book">
<result property="createdAt" column="created_at"/>
</resultMap>
</mapper>
cn.cnyasin.service.BookService.java
package cn.cnyasin.service;
import cn.cnyasin.pojo.Book;
import java.util.List;
public interface BookService {
// 增加
int insertOne(Book book);
// 修改
int updateOne(Book book);
// 删除
int deleteOne(int id);
// 一个
Book getOne(int id);
// 所有
List<Book> getAll();
// 事务测试:插入&删除&查全部
List<Book> insertDeleteGetAll(Book book);
}
cn.cnyasin.service.BookServiceImpl.java
package cn.cnyasin.service;
import cn.cnyasin.dao.BookMapper;
import cn.cnyasin.pojo.Book;
import java.util.List;
public class BookServiceImpl implements BookService {
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int insertOne(Book book) {
return bookMapper.insertOne(book);
}
public int updateOne(Book book) {
return bookMapper.updateOne(book);
}
public int deleteOne(int id) {
return bookMapper.deleteOne(id);
}
public Book getOne(int id) {
return bookMapper.getOne(id);
}
public List<Book> getAll() {
return bookMapper.getAll();
}
public List<Book> insertDeleteGetAll(Book book) {
bookMapper.insertOne(book);
int i = 10 / 0;
bookMapper.deleteOne(15);
return bookMapper.getAll();
}
}
cn.cnyasin.controller.BookController.java
package cn.cnyasin.controller;
import cn.cnyasin.pojo.Book;
import cn.cnyasin.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class BookController {
@Autowired
@Qualifier("bookServiceImpl")
private BookService bookService;
@RequestMapping("/book/list")
public String getAllBook(Model model) {
List<Book> list = bookService.getAll();
model.addAttribute("list", list);
return "bookList";
}
@RequestMapping("/book/insert")
public String insertDeleteGetAll(Model model) {
Book book = new Book();
book.setName("哈喽啊");
book.setStatus(1);
book.setCreatedAt(new Date().getTime() / 1000);
bookService.insertDeleteGetAll(book);
return "redirect:/book/list";
}
}