本篇文章仅仅提供一个crud上的思想,并且提供例子,具体该怎么做,还得靠你自己去想
1.new Project–>spring Initialzr出现:
选择项目需要的组件:
需要组件的如下所示:
首先是经典的mvc的目录创建方法。
java源代码目录
包含:
dao(持久层)
service(业务层)
controller (控制器)
pojo(实体类)
资源目录(resources)
mapper.xml配置文件位置,
springboot配置文件位置 (application.properties)
单元测试位置(test)
其他目录
pom.xml(maven依赖配置)
工具包
package com.cy.brand.pojo;
import lombok.*;
import java.util.Date;
//@Getter
//@Setter
//@ToString
@Data //注解替换上面三个重点
@NoArgsConstructor
@AllArgsConstructor
public class Brand {
private Integer id;
private String name;
private String logo;
private String remark;
private Date createdTime;
}
package com.cy.brand.dao;
import com.cy.brand.pojo.Brand;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface BrandDao {
/**基于id查询品牌信息*/
@Select("select * from tb_brand where id=#{id}")
Brand findById(Integer id);
@Update("update tb_brand set name=#{name},remark=#{remark} where id=#{id}")
int updateBrand(Brand brand);
@Insert("insert into tb_brand(name,remark,createdTime)value(#{name},#{remark},now())")
int insertBrand(Brand brand);
@Delete("delete from tb_brand where id=#{id}")
int deleteById(Integer id);
//复杂sql(例如嵌套了很多动态sql元素)不建议已注解方式进行定义
//@Select("")
List<Brand> findBrands(String name);
}
import com.cy.brand.pojo.Brand;
import java.util.List;
public interface BrandService {
Brand findById(Integer id);
int updateBrand(Brand brand);
int saveBrand(Brand brand);
int deleteById(Integer id);
List<Brand> findBrands(String name);
}
package com.cy.brand.Service.Impl;
import com.cy.brand.dao.BrandDao;
import com.cy.brand.pojo.Brand;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
public class BrandServiceImpl implements BrandService {
//@Slf4j注释描述类时,会在.java编译成.class,会自动创建一个对象
private static final Logger log=//这里的实现在springboot中默认选择的是logback
LoggerFactory.getLogger(BrandServiceImpl.class);
@Autowired
private BrandDao brandDao;
@Override
public Brand findById(Integer id) {
//.....
return brandDao.findById(id);
}
@Override
public int updateBrand(Brand brand) {
//.....
return brandDao.updateBrand(brand);
}
@Override
public int saveBrand(Brand brand) {
int rows=brandDao.insertBrand(brand);
return rows;
}
@Override
public int deleteById(Integer id) {
//1.参数校验
//2.执行删除逻辑
int rows=brandDao.deleteById(id);
//3.验证结果并返回
return rows;
}
@Override
public List<Brand> findBrands(String name) {
long t1=System.currentTimeMillis();
List<Brand> list= brandDao.findBrands(name);
long t2=System.currentTimeMillis();
log.info("time:{}",t2-t1);
return list;
}
}
package com.cy.brand.controller;
import com.cy.brand.pojo.Brand;
import com.cy.brand.Service.Impl.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class BrandController {
@Autowired
private BrandService brandService;
@GetMapping("/brand/doFindById/{id}")
public String doFindById(@PathVariable Integer id,Model model){
Brand brand=brandService.findById(id);
model.addAttribute("brand",brand);
return "/brand/brand-update";
}
@PostMapping("/brand/doUpdateBrand")
public String doUpdateBrand(Brand brand,Model model){
System.out.println("update.brand="+brand);
brandService.updateBrand(brand);
List<Brand> list=brandService.findBrands(null);
model.addAttribute("list",list);
return "brand/brand";
}
@PostMapping("/brand/doSaveBrand")
public String doSaveBrand(Brand brand,Model model){
System.out.println("save.brand="+brand);
brandService.saveBrand(brand);
List<Brand> list=brandService.findBrands(null);
model.addAttribute("list",list);
return "brand/brand";
}
@GetMapping("/brand/doAddUI")
public String doAddUI(){
return "brand/brand-add";
}
@GetMapping("/brand/doDeleteById/{id}")
public String doDeleteById(@PathVariable Integer id,Model model){
brandService.deleteById(id);//id值没有是什么原因
List<Brand> list=brandService.findBrands(null);
model.addAttribute("list",list);
return "brand/brand";
}
//http://localhost/brand/doFindBrands?name=tcl 传统方式url
//http://localhost/brand/doFindBrands/tcl rest风格url
//rest风格(一种软件架构编码风格)的url定义
//其语法为:a/b/{c}/d/{e},在url可以定义变量,这个变量需要使用{}括起来
//rest风格可以更好实现跨平台
//@PathVariable 注解用于修饰方法参数,目的是告诉spring mvc,参数的值来自url
@GetMapping(value={
"/brand/doFindBrands","/brand/doFindBrands/{name}"})
public String doFindBrands(@PathVariable(required = false) String name, Model model) {
List<Brand> list=brandService.findBrands(name);
model.addAttribute("list", list);
return "brand/brand";//第一个brand为目录,第二brand为view name
}//所有与数据相关的问题,一定要学会去跟踪你的数据?
//例如:
//1)客户端向服务端提交的数据,在服务端没有收到?(一定要看客户端提交数据的方式与服务端获取数据的方式)
//2)服务端向客户端响应数据时,假如客户端没有收到?(先检测服务端响应数据之前的数据是什么样子的)
}