【文件属性】:
文件名称:springboot jpa mysql controller演示
文件大小:99KB
文件格式:RAR
更新时间:2021-08-21 11:52:14
springboot jpa controller mysql
springboot jpa mysql controller 完整demo演示
public class HelloBootController {
@Autowired
UserRepository userRepository;
/**
* jpatest
* @param modelMap
* @return
*/
@RequestMapping("/jpatest")
public String jpaTest(ModelMap modelMap){
userRepository.save(new User("python","123456","xxxxx@126.com"));
List users = userRepository.findAll();
modelMap.put("users", users);
return "jpatest";
}
/**
* 输出字符串测试
* http://localhost:8080/helloboot
* @return
*/
@RequestMapping("/hellotest")
public String helloTest(){
return "Hello!";
}
/**
* htmltest
* @return
*/
@RequestMapping("/htmltest")
public String test(){
return "login";
}
/**
* 测试 Model
* spring自动为Model创建实例,并作为controller的入参
* @param model
* @param name
* @return
*/
@RequestMapping("/modeltest")
public String testVelocity(Model model, String name){
if (name==null ||name.isEmpty()){
name ="您没有输入名字!";
}
model.addAttribute("name", name);
return "modeltest";
}
/**
* 测试 ModelAndView
* @param name
* @return
*/
@RequestMapping("modelandview")
public ModelAndView testModel(String name) {
if (name==null ||name.isEmpty()){
name ="您没有输入名字!";
}
//构建ModelAndView实例,并设置跳转地址
ModelAndView view = new ModelAndView("modelandviewtest");
//将数据放置到ModelAndView对象view中,第二个参数可以是任何java类型
view.addObject("name", name);
//返回ModelAndView对象view
return view;
}