目录的结构:
1、建立实体类,是用的是JPA依赖,在运行的过程会根据注释的相关信息在数据库中建立与name对应的表
运行后可以发现数据库中建立了t_user、t_type表。
2、用户登录使用到user表,DAO层:
package com.daqi.daqi.dao;
import com.daqi.daqi.po.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User,Long> {
User findByUsernameAndPassword(String username,String password);
}
解析:写的DAO层接口继承了JpaRepository<T,T>接口,里面包装了一些对数据库的常规操作,通过User中的属性名来生成符合格式的方法名进行映射,就是比较常规的findxxx,deletexxx,addxxx,updatexxx等增删査改操作。
3、service层,
package com.daqi.daqi.service.iml;
import com.daqi.daqi.dao.UserRepository;
import com.daqi.daqi.po.User;
import com.daqi.daqi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public User checkUsers(String username, String password) {
return userRepository.findByUsernameAndPassword(username,password);
}
}
4、Controller层:
@Controller
@RequestMapping("/admin")
public class LoginController {
@Autowired
private UserService userService;
@GetMapping
public String loginPage(){
return "admin/login";
}
@PostMapping("/login")
public String login(@RequestParam String username, @RequestParam String password,
HttpSession session, RedirectAttributes attributes){
User user=userService.checkUsers(username,password);
if (user!=null){
user.setPassword(null);
session.setAttribute("user",user);
return "admin/index";
}else{
attributes.addFlashAttribute("message","用户或密码错误");
return "redirect:/admin";
}
}
}
再就是关于分页的功能:
DAO层:
package com.daqi.daqi.dao;
import com.daqi.daqi.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TypeRepository extends JpaRepository<Type,Long> {
Type findByName(String name);
}
Service层:
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
TypeRepository typeRepository;
@Override
public Page<Type> listType(Pageable pageable){
return typeRepository.findAll(pageable);
}
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
@Override
public void delete(Long id) {
typeRepository.deleteById(id);
}
@Override
public Type getType(Long id) {
return typeRepository.findById(id).orElse(null);
}
@Override
public Type updateType(Long id, Type type) {
Type type1=typeRepository.findById(id).orElse(null);
if (type1!=null){
System.out.println("未获得更新对象");
return null;
}
BeanUtils.copyProperties(type,type1);
return typeRepository.save(type1);
}
}
Controller层:
import com.daqi.daqi.po.Type;
import com.daqi.daqi.service.iml.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeServiceImpl typeService;
@RequestMapping("/types")
public String type(@PageableDefault(size=3,sort={"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
model.addAttribute("page",typeService.listType(pageable));
return "admin/types";
}
@RequestMapping("/types/input")
public String input(Model model){
model.addAttribute("type",new Type());
return "admin/types-input";
}
@PostMapping("/types/add")
public String add(@Valid Type type, BindingResult result, RedirectAttributes attributes){
Type type1=typeService.getTypeByName(type.getName());
if (type1!=null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if (result.hasErrors()){
return "admin/types-input";
}
Type type2=typeService.saveType(type);
if (type2==null){
attributes.addFlashAttribute("message","新增失败");
}else {
attributes.addFlashAttribute("message","新增成功");
}
return "redirect:/admin/types";
}
@RequestMapping("/types/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes){
typeService.delete(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
@RequestMapping("/types/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
model.addAttribute("type",typeService.getType(id));
return "admin/types-input";
}
@RequestMapping("/types/{id}/update")
public String update(@Valid Type type,BindingResult result,@PathVariable Long id,RedirectAttributes attributes){
Type type1=typeService.getTypeByName(type.getName());
if(type1!=null){
result.rejectValue("name","nameError","不能添加重复的类");
}
if(result.hasErrors()){
return "admin/type-input";
}
Type type2=typeService.updateType(id,type);
if (type2!=null){
attributes.addFlashAttribute("message","更新成功");
}else {
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/types";
}
}
附上JPA常用注释