-
导入相关依赖
- 配置连接池,连接数据库
因为要写映射文件,所以要设置别名搜索和加载映射文件目录#连接池 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/*.xml
-
根据数据库创建实体类
利用@Data注解简化实体类的开发,不需要写get set方法(Lombok)
MyBatisPlus中如果类名和表名一致,是不需要加注解的
MyBatisPlus提供了一些注解供我们在实体类和表信息出现不对应的时候使用。
@TableName
实体类的类名和数据库表名不一致
@TableId
实体类的主键名称和表中主键名称不一致
默认主键是通过雪花算法生成全局唯一主键
主键自增策略@TableId(type = IdType.AUTO)
@TableField
实体类中的成员名称和表中字段名称不一致
如果字段与表没有关联(排除实体类中非表字段)
使用 @TableField(exist = false) 注解
因为查询的时候有开始时间与结束时间,我们要把时间作为条件传给账单实体类,所以我们需要在实体类中有两个瞬时属性
@Transient注解表示当前属性为瞬时属性,跟字段没有映射,上面实体类date1和date2只3作为查询条件存在,跟表中字段没有关联
最后还要显示类别名称,并不是显示类别id,所以还要有一个瞬时属性来记录类别名称
bill实体类
@Data @TableName(value = "bill_") public class Bill implements Serializable { @TableId(value = "id_",type = IdType.AUTO) private Long id; @TableField(value = "title_") private String title; @TableField(value = "bill_time_") private Date billTime; @TableField(value = "type_id_") private Long TypeId; @TableField(value = "price_") private Double price; @TableField(value = "explain_") private String explain; /** * 类别名称 */ @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
BillMapper
需要继承BaseMapper<>,继承BaseMapper后就可以使用mp内置的方法来实现增删改查
要在启动类中搜索接口所在的包来产生接口的代理对象
@MapperScan(“....mapper”)
测试的话要设置启动器(测试需要的注解)
@RunWith(SpringRunner.class)
@SpringBootTest@Repository public interface BillMapper extends BaseMapper<Bill> { List<Bill> select(Bill b); }
BillTypeMapper
@Repository public interface BillTypeMapper extends BaseMapper<BillType> { }
- Service
service调用dao内置方法实现增删改查
BillServicepublic interface BillService { //查询 List<Bill> list(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 List<Bill> list(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 { List<BillType> list(); }
BillTypeServiceImpl@Service public class BillTypeServiceImpl implements BillTypeService { @Autowired private BillTypeMapper billTypeMapper; @Override public List<BillType> list() { 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; /** * 查询账单类型 * Bill b接受页面的请求参数 * 把查询到的结果放到ModelA中 */ @RequestMapping("/list") public String list(Bill bill, Model model){ List<BillType> types = billTypeService.list(); //把查询到的types存储到Model Attribute中 model.addAttribute("types",types); //查询账单 Bill bill页面的查询条件 List<Bill> bills = billService.list(bill); //把查询到的bills也存储到Model Attribute中 model.addAttribute("bills",bills); return "bill/list"; } /** * 跳到添加页面 还需要再次查询账单类型 */ @RequestMapping("/toAdd") public String toAdd(Model model){ List<BillType> 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"; } /** * 删除 * 利用Rest风格的请求路径,点击删除 地址栏中占位符传参 */ @RequestMapping("/delete/{id}") public String delete(@PathVariable("id") Long id){ billService.delete(id); //删除以后也要重定向 到首页列表 return "redirect:/bill/list"; } /** * 修改 先回显数据 * 也是利用Rest风格的请求路径,点击删除 地址栏中占位符传参 */ @RequestMapping("/toUpdate/{id}") public String toUpdate(@PathVariable("id") Long id,Model model){ //通过id查询到的结果 存到Model中 并且也需要生成下拉框 List<BillType> 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下