7-28日Java实训日记
- 7-28号,是java实训的第九天了,今天实训老师带我们使用springboot框架实现了新闻系统的分类模块,在今天的学习过程中让我对新的知识有了一个更加深刻的认识,包括:jpa、orm、Hibernate与ssm框架中mybatis的区别,在今天的学习中收获很大。
- 此外,在老师讲完课后,自己在中午通过亲自实现老师早上讲的功能,对老师所讲的知识有了一个更加深刻的学习和体会,同时看到自己使用新框架实现的东西自己很开心。
- 在完成老师上课所讲的项目之后,我们项目小组开始了对小组springboot项目开发的需求设计并将进行整理和分工。
springboot框架新闻标签分类实现流程
- 控制层(controller)
@Controller
@RequestMapping("/admin/tags")
public class TagController {
@Autowired
private TagService tagService;
@RequestMapping
public String list(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
Page<Tag> page=tagService.listTag(pageable);
model.addAttribute("page",page);
return "admin/tags";
}
@GetMapping("{id}/delete")
public String delete(@PathVariable Long id){
tagService.delete(id);
return "redirect:/admin/tags";
}
@GetMapping("input/{id}")
public String toinput(@PathVariable Long id,Model model){
if(id==-1){
Tag tag=new Tag();
model.addAttribute("tag",tag);
}else{
Tag tag=tagService.findTagById(id);
model.addAttribute("tag",tag);
}
return "admin/tags-input";
}
@RequestMapping("input")
public String input(Tag tag){
tagService.input(tag);
return "redirect:/admin/tags";
}
}
- dao层
package com.zr.dao;
import com.zr.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TypeDao extends JpaRepository<Type,Long> {
}
- Service层
接口:
package com.zr.dao;
import com.zr.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TypeDao extends JpaRepository<Type,Long> {
}
接口实现层
@Service
public class TagServiceImpl implements TagService {
@Autowired
private TagDao tagDao;
public Page<Tag> listTag(Pageable pageable) {
return tagDao.findAll(pageable);
}
public void delete(Long id) {
tagDao.deleteById(id);
}
public Tag findTagById(Long id) {
return tagDao.getOne(id);
}
public void input(Tag tag) {
tagDao.save(tag);
}
public List<Tag> listTag() {
return tagDao.findAll();
}
public List<Tag> findTagByTagId(String tagIds) {
//1,2,3
List<Long> ids=new ArrayList<>();
if(!StringUtils.isEmpty(tagIds)){
String[]strings = tagIds.split(",");
for(String s:strings){
if(!StringUtils.isEmpty(s)){
ids.add(new Long(s));
}
}
}
return tagDao.findAllById(ids);
}
public String getTagIds(List<Tag> tags) {
//1,2,3==>1,2,3 是第一个就不需要添加逗号
StringBuffer ids=new StringBuffer();
if(!tags.isEmpty()){
boolean flag=false;
for(Tag t:tags){
if(flag){
ids.append(",");
ids.append(t.getId());
}else{
ids.append(t.getId());
flag=true;
}
}
}
return ids.toString();
}
}
- Contorller层
package com.zr.controller;
import com.zr.po.User;
import com.zr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("admin")
public class LoginController {
@Autowired
private UserService userService;
@GetMapping
public String ToLogin(){
return "admin/login";
}
@PostMapping("login")
public String login(String username, String password, HttpSession session, RedirectAttributes redirectAttributes){
User user = userService.checkUser(username,password);
if (user!=null){
session.setAttribute("user",user);
return "admin/index";
}
else {
redirectAttributes.addFlashAttribute("message","用户名和密码错误1");
return "redirect:/admin";
}
}
@GetMapping("logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "admin/login";
}
}
Type:
package com.zr.controller;
import com.zr.po.Type;
import com.zr.service.TypeService;
import net.bytebuddy.TypeCache;
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.web.bind.annotation.RequestMapping;
import org.springframework.data.domain.Page;
//import sun.jvm.hotspot.debugger.Page;
//import java.awt.print.Pageable;
@Controller
@RequestMapping("/admin/types")
public class TypeController {
@Autowired
private TypeService typeService;
@RequestMapping
public String list(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model){
//Pageable中存放了一些查询条件
Page<Type> page = typeService.listType(pageable);
model.addAttribute("page",page);
return "admin/types";
}
}
- MD5加密
public class MD5Util {
/**
* MD5加密类
* @param str 要加密的字符串
* @return 加密后的字符串
*/
public static String code(String str){
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[]byteDigest = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < byteDigest.length; offset++) {
i = byteDigest[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
//32位加密
return buf.toString();
// 16位的加密
//return buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.println(code("123"));
}
}
springboot框架新闻标签分类实现效果