关于SpringBoot学习记录2.0
了解基础配置和Freemarker内容
1.了解application.properties基本设置和hibernate数据库配置
#端口配置,默认8080
server.port=8888
#项目请求,默认/
server.servlet.context-path=/Test
#自定义属性
HelloWorld=hello world!这是我第一个Spring Boot项目!
#自定义属性(有前缀,和proerties包下配合方便使用)
mysql.jdbcName=com.mysql.cj.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/test_itcast?serverTimezone=GMT
mysql.userName=root
mysql.passWord=123456
#数据库相关配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test_itcast?serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
2.了解Freemarker还有Controller
一、因为springboot不推荐jsp,所以本次学习不使用jsp相关知识。
二、这次学习Freemarker模板引擎,首先就是就是后缀.ftl,还有一些标签后面用到再说明。
三、@Controller和@RestController区别:用法上@Controller+@ResponseBody=@RestController,再者就是@RestController一般用于返回JSON。
持久化类和映射文件
1.了解application.properties基本设置(自动更新数据库)
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
2.Entity类(注解@Entity,@Table表,@Id主键,@GenertedValue自增)
@Entity
@Table(name="t_user")
public class User {
@Id
@GeneratedValue
private Integer id;
@Column(length = 45)
private String username;
@Column(length = 45)
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.Dao层
public interface UserDao extends JpaRepository<User,Integer> {
//自定义查询
@Query("select b from User b where b.username like %?1%")
public List<User> fineByUsername(String username);
@Query(value = "select * from t_user order by RAND() limit ?1",nativeQuery = true)
public List<User> randomList(Integer n);
}
Controller层
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserDao userDao;
@RequestMapping("/toadd")
public String toadd(){
return "add";
}
//显示列表
@RequestMapping("/userlist")
private ModelAndView userlist(){
ModelAndView mav=new ModelAndView();
mav.addObject("userlist",userDao.findAll());
mav.setViewName("userlist");
return mav;
}
//添加
@RequestMapping(value = "/add",method = RequestMethod.POST)
private String add(User user){
userDao.save(user);
return "redirect:/user/userlist";
}
//根据id查询
@RequestMapping("/preupdate/{id}")
public ModelAndView preupdate(@PathVariable("id") Integer id){
ModelAndView mav=new ModelAndView();
mav.addObject("user",userDao.getOne(id));
mav.setViewName("update");
return mav;
}
//修改
@RequestMapping(value = "/update",method = RequestMethod.POST)
private String update(User user){
userDao.save(user);
return "redirect:/user/userlist";
}
//删除
@RequestMapping(value = "/delete",method = RequestMethod.GET)
private String delete(Integer id){
userDao.deleteById(id);
return "redirect:/user/userlist";
}
关于栏截器的使用
1.了解SpringBoot拦截器配置
configuration用来配置拦截器栏截的请求和静态资源。
interceptor配置栏截器内容
2.基本代码格式
configuration包下(基本设置,主要介绍配置addInterceptors方法):
@Configuration
public class SessionConfiguration implements WebMvcConfigurer {
//注入拦截器LoginInterceptor
@Autowired
private LoginInterceptor loginInterceptor;
//配置静态资源的,html,js,css。。。等
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry){
//addPathPatterns("/**") 表示拦截所有请求,
//excludePathPatterns("/login") 表示除了/login请求之外
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
}
}
interceptor包下(这里主要用到PreHandle,即请求前栏截处理):
//添加Component注解,否则SessionConfiguration中无法注入
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session=request.getSession();
String user=(String) session.getAttribute("USER_SESSION");
if(user==null){
request.getRequestDispatcher("/login").forward(request, response);
return false;
}else {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
动态查询功能实现(初步学习内容,还需要进一步学习)
//动态查询
@RequestMapping("/list")
public ModelAndView list(User user){
ModelAndView mav=new ModelAndView();
List<User> userList=userDao.findAll(new Specification<User>(){
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate predicate=criteriaBuilder.conjunction();
if(user!=null){
if(user.getUsername()!=null && !"".equals(user.getUsername())){
predicate.getExpressions().add(criteriaBuilder.like(root.get("username"),"%"+user.getUsername()+"%"));
}
if(user.getPassword()!=null && !"".equals(user.getPassword())){
predicate.getExpressions().add(criteriaBuilder.like(root.get("password"),"%"+user.getPassword()+"%"));
}
}
return predicate;
}
});
mav.addObject("userlist",userList);
mav.setViewName("userlist");
return mav;
}
总结和个人建议
本次学习内容比较杂,不是非常系统和深入,最好是多多学习数据库的有关方面的使用。还有一些与前端插件配合使用的方法。