最后
作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料
<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>
讲解:
- parameterType属性:用于指定传入参数的类型,传入的是一个类的对象,所以写全类名
- resultType属性:用于指定结果集的类型
- #{ } 字符:代表占位符,类似 jdbc 中的 ?,用于执行语句时替换实际的数据
3.分类管理业务层
分类业务层接口
这里和持久层接口是一样的,就不做分析,在service包下创建TypeService接口,代码如下:
package com.star.service;
import com.star.entity.Type;
import java.util.List;
/**
- @Description: 分类业务层接口
- @Date: Created in 11:41 2020/6/2
- @Author: ONESTAR
- @QQ群: 530311074
- @URL: https://onestar.newstar.net.cn/
*/
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);
}
接口实现类
没啥好分析的,直接调用持久层接口,在Impl包下创建TypeServiceImpl类实现TypeService接口,代码如下:
package com.star.service.Impl;
import com.star.dao.TypeDao;
import com.star.entity.Type;
import com.star.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
- @Description: 分类业务层接口实现类
- @Date: Created in 13:32 2020/6/2
- @Author: ONESTAR
- @QQ群: 530311074
- @URL: https://onestar.newstar.net.cn/
*/
@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);
}
}
讲解:
- @Service注解:用于标注业务层组件
- @Autowired注解:@Autowired表示被修饰的类需要注入对象,spring会扫描所有被@Autowired标注的类,然后根据类型在ioc容器中找到匹配的类注入
- @Transactional注解:实现事务操作
4.分类管理控制器
分析:
问:分类管理控制器需要考虑哪些功能?
答:基本的增、删、改、查功能
问:增删改查够了吗?为了有良好的体验,前端页面显示的时候需要能够分页显示,操作成功后在前端有信息提示,并需要做重复判断,这些要如何实现呢?
答:分页显示使用PageHelper插件具体使用可以参考:SpringBoot引入Pagehelper分页插件 ,前端信息提示可以用model.addAttribute,重复添加使用@Valid注解:请求数据校验,再做一个逻辑判断就可以了
添加PageHelper分页插件,在pom.xml中添加:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
在admin包下创建TypeController控制器类,代码如下:
package com.star.controller.admin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.entity.Type;
import com.star.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;
/**
- @Description:
- @Date: Created in 14:56 2020/6/2
- @Author: ONESTAR
- @QQ群: 530311074
- @URL: https://onestar.newstar.net.cn/
*/
@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";
}
那么如何才能正确的掌握Redis呢?
为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题
-
并发编程面试题汇总
-
JVM面试题汇总
-
Netty常被问到的那些面试题汇总
-
Tomcat面试题整理汇总
-
Mysql面试题汇总
-
Spring源码深度解析
-
Mybatis常见面试题汇总
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
Mysql面试题汇总(一)
Mysql面试题汇总(二)
Redis常见面试题汇总(300+题)
试题整理汇总
-
Mysql面试题汇总
-
Spring源码深度解析
-
Mybatis常见面试题汇总
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
[外链图片转存中…(img-vPHvhvhR-1715097476320)]
Mysql面试题汇总(一)
[外链图片转存中…(img-41AKWnAg-1715097476320)]
Mysql面试题汇总(二)
[外链图片转存中…(img-ed7qwxqw-1715097476321)]
Redis常见面试题汇总(300+题)
[外链图片转存中…(img-MA2hn0FF-1715097476321)]