栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot练手项目(springboot+mp)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot练手项目(springboot+mp)

  1. 导入相关依赖
  2. 配置连接池,连接数据库
    因为要写映射文件,所以要设置别名搜索和加载映射文件目录
    #连接池
    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;
    }
  3. 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 {
    }
    

  4. Service

    service调用dao内置方法实现增删改查

    BillService
    public interface BillService {
    
        //查询
        List 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 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);
        }
    }
    

    BillTypeService
    public interface BillTypeService {
        List list();
    }

    BillTypeServiceImpl
    @Service
    public class BillTypeServiceImpl implements BillTypeService {
        @Autowired
        private BillTypeMapper billTypeMapper;
    
        @Override
        public List list() {
            return billTypeMapper.selectList(null);
        }
    }
  5. 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){
            List types = 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";
        }
    }
    
  6. 实现项目页面部分
    首先拷贝资源到resources-static目录下

    模板也都需要放到resources-template下
     
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号