标签:entity springmv注入 service controller 重定向
一【实体Entity:与数据库中的表对应
1、spring不会注入entity,使用时需要使用new方法创建实例。
二【repository:数据库操作接口
1、创建接口:
public interface StudentRepository extends
PagingAndSortingRepository<Entity, String>{}2、需要注入时:
@Resource(name = "studentRepository") //Resource中类名第一个字母小写。 private StudentRepository repository;
3、两种数据库操作方法:
1):
Page<Entity> findByALikeAndBLikeIgnoreCase(String A,String B, Pageable pageable);
2):
@Query("select student.name from Student student where student.class.id in (select class.id from Class class where class.name like ?1)")
//?1代表第一个参数
Page<Student> findByConditions(String className,Pageable pageable);说明:1、strdent表的class属性是class表的外键,多对一映射。class属性是实体类型。
在Student实体中申明
/** *学生所在班级。 */ @ManyToOne @JoinColumn(name = "class") private Class class;
2、搜索班级名类似className的班级的所有学生名。
三【service:服务接口
申明接口,并在service.impl中实现。
需要注入时:
@Resource(name = "studentservice") private StudentService studentService;
四【service.impl:服务实现
申明实现:
@Service("studentservice")实现service中的接口。
五【controller:控制器
申明:
@Controller
//申明控制器。
@RequestMapping("/a")
//匹配请求路径,第一个/表示根,如果匹配成功,请求由该控制器处理。
//控制器只有第一次调用时加载,全局属性用于所有请求。
@RequestMapping(method = RequestMethod.GET)
@RequestMapping(value = "/b", method = RequestMethod.GET)
//匹配方法,没有value表示匹配/a,有value表示匹配在/a的基础上加上value,即匹配/a/b
//method表示请求类型,如果没有申明,表示匹配所有请求方法,包括GET,POST。
方法参数:
@Valid Student student,BindingResult bindingResult
//如果请求参数中包括Student实体属性,会自动封装到对象student中。
//然后由bindingResult检测参数是否符合Student实体对属性的规定。
//如果不符合
//if (bindingResult.hasErrors()) {
// return "/error";
//}
@RequestParam(value = "result", required = false) String result,
//参数result,不是必须的。
@PathVariable String id,
//路径中的参数,@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@PageableDefault(page = 0, size = PAGE_SIZE) Pageable pageable,
//默认首页,页面大小为PAGE_SIZE。
Model model
//将处理结果数据封装到model,传给页面显示
//model.addAttribute("page", page);
//model.addAttribute("pageable", pageable);
返回页面:
return "/show" //显示根目录下的show.ftl页面 //页面可以获得model中的数据,组装成html页面,返回给客户端。 return "/a/b" //服务器端重定向到/a/b页面(客户端url不会改变)。 return "redirect:/a/b" //客户端重定向到/a/b页面(客户端url改变)。
本文出自 “暗夜” 博客,请务必保留此出处http://icyore.blog.51cto.com/8486958/1567070
标签:entity springmv注入 service controller 重定向
原文地址:http://icyore.blog.51cto.com/8486958/1567070