分类版块功能实现
1.持久层接口
分类管理需要实现的接口有:查询所有分类getAllType()、根据名称查询getTypeByName()、根据id查询分类getType()、保存分类saveType()、修改编辑分类updateType()、删除分类deleteType()。
@Mapper
@Repository
public interface TypeDao {
//新增保存分类
int saveType(Type type);
//根据id查询分类
Type getType(Long id);
//查询所有分类
List<Type> getAllType();
//根据分类名称查询分类
Type getTypeByName(String name);
//编辑修改分类
int updateType(Type type);
//删除分类
void deleteType(Long id);
}
2.数据库mapper
mapper是按照dao接口来实现的
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.star.dao.TypeDao">
<!--新增保存分类-->
<insert id="saveType" parameterType="com.star.entity.Type">
insert into myblog.t_type values (#{id},#{name});
</insert>
<!--根据id查询分类-->
<select id="getType" resultType="com.star.entity.Type">
select id,name from myblog.t_type where id = #{id}
</select>
<!--查询所有分类-->
<select id="getAllType" resultType="com.star.entity.Type">
select * from myblog.t_type
</select>
<!--根据分类名称来查询-->
<select id="getTypeByName" resultType="com.star.entity.Type">
select * from myblog.t_type where name = #{name}
</select>
<!--编辑修改分类-->
<update id="updateType" parameterType="com.star.entity.Type">
update myblog.t_type set name = #{name} where id = #{id}
</update>
<!--删除分类-->
<delete id="deleteType" >
delete from myblog.t_type where id = #{id}
</delete>
</mapper>
3.分类管理Service业务层
Service层接口对照持久层接口,在Service包下创建TypeService接口,并写出实现类。
public interface TypeService {
//新增保存分类
int saveType(Type type);
//根据id查询分类
Type getType(Long id);
//查询所有分类
List<Type> getAllType();
//根据分类名称查询分类
Type getTypeByName(String name);
//编辑修改分类
int updateType(Type type);
//删除分类
void deleteType(Long id);
}
接口实现类,直接调用持久层接口
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeDao typeDao;
@Transactional
@Override
public int saveType(Type type) {
return typeDao.saveType(type);
}
@Transactional
@Override
public Type getType(Long id) {
return typeDao.getType(id);
}
@Transactional
@Override
public List<Type> getAllType() {
return typeDao.getAllType();
}
@Override
public Type getTypeByName(String name) {
return typeDao.getTypeByName(name);
}
@Transactional
@Override
public int updateType(Type type) {
return typeDao.updateType(type);
}
@Transactional
@Override
public void deleteType(Long id) {
typeDao.deleteType(id);
}
}
@Transactional注解:实现事务操作
@Autowired注解:表示被修饰的类需要注入对象,spring会扫描所有被@Autowired标注的类,然后根据类型在ioc容器中找到匹配的类注入
4.分类管理控制器 Controller
实现功能:
1.请求查询分页列表
2.请求返回“新增分类”页面
3.请求新增分类
4.请求跳转“修改分类”页面
5.请求修改分类
6.删除分类
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService;
// 分页查询分类列表
@GetMapping("/types")
public String list(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
//按照排序字段 倒序 排序
String orderBy = "id desc";
PageHelper.startPage(pageNum,10,orderBy);
List<Type> list = typeService.getAllType();
PageInfo<Type> pageInfo = new PageInfo<Type>(list);
model.addAttribute("pageInfo",pageInfo);
return "admin/types";
}
// 返回新增分类页面
@GetMapping("/types/input")
public String input(Model model){
model.addAttribute("type", new Type());
return "admin/types-input";
}
// 新增分类
@PostMapping("/types")
public String post(@Valid Type type, RedirectAttributes attributes) {
Type type1 = typeService.getTypeByName(type.getName());
if (type1 != null) {
attributes.addFlashAttribute("message", "不能添加重复的分类");
return "redirect:/admin/types/input";
}
int t = typeService.saveType(type);
if (t == 0) {
attributes.addFlashAttribute("message", "新增失败");
} else {
attributes.addFlashAttribute("message", "新增成功");
}
return "redirect:/admin/types";
}
// 跳转修改分类页面
@GetMapping("/types/{id}/input")
public String editInput(@PathVariable Long id, Model model) {
model.addAttribute("type", typeService.getType(id));
return "admin/types-input";
}
// 编辑修改分类
@PostMapping("/types/{id}")
public String editPost(@Valid Type type, RedirectAttributes attributes) {
Type type1 = typeService.getTypeByName(type.getName());
if (type1 != null) {
attributes.addFlashAttribute("message", "不能添加重复的分类");
return "redirect:/admin/types/input";
}
int t = typeService.updateType(type);
if (t == 0 ) {
attributes.addFlashAttribute("message", "编辑失败");
} else {
attributes.addFlashAttribute("message", "编辑成功");
}
return "redirect:/admin/types";
}
// 删除分类
@GetMapping("/types/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes) {
typeService.deleteType(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/types";
}
}
说明
@Controller注解:标注控制层组件@RequestMapping("/admin"):建立URL请求和相应处理方法的关系
@GetMapping注解:将HTTP get请求映射到特定处理程序的方法
@PostMapping注解:将HTTP Post请求映射到特定处理程序的方法
@Valid注解:请求数据校验,用来判断是否有重复的分类@PathVariable注解:获取URL中的数据attributes.addFlashAttribute:相当于重定向后,在URL后面拼接了参数,这样重定向之后的页面让控制器再去获取URL后面的参数就可以