过滤器
1、实现 Filter 接口
package com.example.demo.filter; import org.springframework.stereotype.Component; import javax.servlet.*; import java.io.IOException; @Component public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("MyFilter init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("处理前1"); filterChain.doFilter(servletRequest, servletResponse); System.out.println("处理后1"); } @Override public void destroy() { System.out.println("MyFilter destroy"); } } package com.example.demo.filter; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @Component public class MyFilter2 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("MyFilter2 init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("处理前2"); filterChain.doFilter(servletRequest, servletResponse); System.out.println("处理后2"); } @Override public void destroy() { System.out.println("MyFilter2 destroy"); } }
2、配置中注册
package com.example.demo.config; import com.example.demo.filter.MyFilter; import com.example.demo.filter.MyFilter2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Arrays; // 注册自定义的过滤器 @Configuration public class MyFilterConfig { @Autowired MyFilter myFilter; @Autowired MyFilter2 myFilter2; @Bean public FilterRegistrationBean<MyFilter> filterRegistrationBean() { FilterRegistrationBean<MyFilter> filter = new FilterRegistrationBean<>(); filter.setFilter(this.myFilter); filter.setOrder(2); // 执行顺序 filter.setUrlPatterns(Arrays.asList("/api/*")); return filter; } @Bean public FilterRegistrationBean<MyFilter2> filterRegistrationBean2() { FilterRegistrationBean<MyFilter2> filter = new FilterRegistrationBean<>(); filter.setFilter(this.myFilter2); filter.setOrder(1); filter.setUrlPatterns(Arrays.asList("/api/*")); return filter; } }
拦截器 Interceptor
1、定义拦截器
package com.example.demo.interceptor; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor extends HandlerInterceptorAdapter{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); } }
2、注册拦截器
package com.example.demo.config; import com.example.demo.interceptor.MyInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // 可注册多个 registry.addInterceptor(new MyInterceptor()) .addPathPatterns("/api/*"); } }
MyBatis
项目结构
/pom.xml /src/main/ java/com/example/demo/ bean/ User.java controller/ UserController.java dao/ UserDao.java service/ UserService.java resources/ application.properties mapper/ UserMapper.xml
1、依赖 pom.xml
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
3、建表语句
CREATE TABLE `user` (
`id` int(13) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(33) DEFAULT NULL COMMENT '姓名',
`age` int(3) DEFAULT NULL COMMENT '年龄',
`money` double DEFAULT NULL COMMENT '账户余额',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
4、bean
package com.example.demo.bean;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
private Double money;
}
5、Dao
package com.example.demo.dao; import com.example.demo.bean.User; import org.apache.ibatis.annotations.*; import java.util.List; @Mapper public interface UserDao { /** * 通过名字查询用户信息, xml配置 */ User selectUserByName(String name); /** * 通过id查询用户信息 */ @Select("select * from user where id = #{id} limit 1") User selectUserById(Integer id); /** * 查询所有用户信息 */ @Select("select * from user") List<User> selectAllUser(); /** * 插入用户信息 */ @Insert("insert into user (name, age, money) values (#{name}, #{age}, #{money})") void insertUser(User user); /** * 根据 id 删除用户信息 */ @Delete("delete from user where id = #{id}") void deleteUser(Integer id); /** * 根据 id 更新用户信息 */ @Update("update user set name = #{name}, age = #{age}, money = #{money} where id = #{id}") void updateUser(User user); }
6、Service
package com.example.demo.service; import com.example.demo.bean.User; import com.example.demo.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service public class UserService { @Autowired UserDao userDao; public User selectUserByName(String name) { return userDao.selectUserByName(name); } public List<User> selectAllUser() { return userDao.selectAllUser(); } public void insertUser(User user) { userDao.insertUser(user); } public void deleteUser(Integer id) { userDao.deleteUser(id); } public void updateUser(User user) { userDao.updateUser(user); } /** * 模拟事务 */ @Transactional public void changeMoney(){ User user1 = userDao.selectUserById(1); User user2 = userDao.selectUserById(2); user1.setMoney(user1.getMoney() - 5); user2.setMoney(user2.getMoney() + 5); userDao.updateUser(user1); int temp = 1 / 0; // 异常 userDao.updateUser(user2); } }
7、Controller
package com.example.demo.controller; import com.example.demo.bean.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired UserService userService; @GetMapping("/selectUserByName") public User selectUserByName(String name){ return userService.selectUserByName(name); } @GetMapping("/selectAllUser") public List<User> selectAllUser(){ return userService.selectAllUser(); } @PostMapping("/insertUser") public List<User> insertUser(@RequestBody User user){ userService.insertUser(user); return userService.selectAllUser(); } @GetMapping("/deleteUser") public List<User> deleteUser(Integer id){ userService.deleteUser(id); return userService.selectAllUser(); } @PostMapping("/updateUser") public List<User> updateUser(@RequestBody User user){ userService.updateUser(user); return userService.selectAllUser(); } @GetMapping("/changeMoney") public List<User> changeMoney(){ userService.changeMoney(); return userService.selectAllUser(); } }