- 导入相关依赖
- 配置连接池,连接数据库
因为要写映射文件,所以要设置别名搜索和加载映射文件目录#连接池 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/bill-manager?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false username: root password: 123456 #配置mybatis plus mybatis-plus: type-aliases-package: com.jwt.bill.entity mapper-locations: classpath:/mappers @Transient @TableField(exist = false) private String typeName; @Transient @TableField(exist = false) private Date date1; @Transient @TableField(exist = false) private Date date2; }billType实体类
@Data @TableName(value = "bill_type_") public class BillType implements Serializable { @TableId(value = "id_",type = IdType.AUTO) private Long id; @TableId(value = "name_") private String name; } - dao
需要继承baseMapper<>,继承baseMapper后就可以使用mp内置的方法来实现增删改查
要在启动类中搜索接口所在的包来产生接口的代理对象
@MapperScan(“....mapper”)
测试的话要设置启动器(测试需要的注解)
@RunWith(SpringRunner.class)
@SpringBootTest BillMapper@Repository public interface BillMapper extends baseMapper
{ List select(Bill b); } BillTypeMapper
@Repository public interface BillTypeMapper extends baseMapper
{ } - Service
service调用dao内置方法实现增删改查
BillServicepublic interface BillService { //查询 Listlist(Bill b); //添加 int add(Bill b); //根据id查询 Bill get(Long id); //更新 int update(Bill b); //删除 int delete(Long id); }
BillServiceImpl@Service public class BillServiceImpl implements BillService { @Autowired private BillMapper billMapper; @Override public Listlist(Bill b) { return billMapper.select(b); } @Override public int add(Bill b) { return billMapper.insert(b); } @Override public Bill get(Long id) { return billMapper.selectById(id); } @Override public int update(Bill b) { return billMapper.updateById(b); } @Override public int delete(Long id) { return billMapper.deleteById(id); } }
BillTypeServicepublic interface BillTypeService { Listlist(); }
BillTypeServiceImpl@Service public class BillTypeServiceImpl implements BillTypeService { @Autowired private BillTypeMapper billTypeMapper; @Override public Listlist() { return billTypeMapper.selectList(null); } } - Controller
在Controller中调用service中的方法来实现增删改查业务
看看查询的需求
查询账单类型:生成下拉列表框
开始时间,结束时间
删除:利用Rest风格的请求路径,点击删除 地址栏中占位符传参
修改:也是利用Rest风格的请求路径,点击修改 地址栏中占位符传参
package com.jwt.bill.Controller; import com.jwt.bill.entity.Bill; import com.jwt.bill.entity.BillType; import com.jwt.bill.service.BillService; import com.jwt.bill.service.BillTypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/bill") public class BillController { @Autowired private BillService billService; @Autowired private BillTypeService billTypeService; @RequestMapping("/list") public String list(Bill bill, Model model){ Listtypes = billTypeService.list(); //把查询到的types存储到Model Attribute中 model.addAttribute("types",types); //查询账单 Bill bill页面的查询条件 List bills = billService.list(bill); //把查询到的bills也存储到Model Attribute中 model.addAttribute("bills",bills); return "bill/list"; } @RequestMapping("/toAdd") public String toAdd(Model model){ List types = billTypeService.list(); //把查询到的types存储到Model Attribute中 model.addAttribute("types",types); return "bill/add"; } @RequestMapping("/add") public String add(Bill bill){ billService.add(bill); //添加以后需要重定向 到首页列表 return "redirect:/bill/list"; } @RequestMapping("/delete/{id}") public String delete(@PathVariable("id") Long id){ billService.delete(id); //删除以后也要重定向 到首页列表 return "redirect:/bill/list"; } @RequestMapping("/toUpdate/{id}") public String toUpdate(@PathVariable("id") Long id,Model model){ //通过id查询到的结果 存到Model中 并且也需要生成下拉框 List types = billTypeService.list(); //把查询到的types存储到Model Attribute中 model.addAttribute("types",types); //根据id查询出来回显数据 Bill bill = billService.get(id); model.addAttribute("bill",bill); return "/bill/update"; } @RequestMapping("/update") public String update(Bill bill){ billService.update(bill); //修改以后也要重定向 到首页列表 return "redirect:/bill/list"; } } - 实现项目页面部分
首先拷贝资源到resources-static目录下
模板也都需要放到resources-template下



