• mybatis的通用mapper小结


    import tk.mybatis.mapper.entity.Example; //此包是tk下的
    1.定义一个dao层接口不需要任何方法 需要继承Mapper<类型>

     

     2.在service中注入dao

    import com.alibaba.dubbo.config.annotation.Service;
    import com.github.pagehelper.Page;
    import com.github.pagehelper.PageHelper;import org.springframework.beans.factory.annotatio n.Autowired;
    import tk.mybatis.mapper.entity.Example;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * 实现类
     * BandService类  在接口包下
     * @date 2019/12/9 9:14
     */
    @Service //注意导入的是dubbo的包
    public class BrandServiceImpl implements BrandService {
    
        @Autowired
        private BrandMapper brandMapper; //依赖注入
    
        @Override//查询所有
        public List<Brand> findAll() {
            return brandMapper.selectAll();
        }
    
        @Override //分页查询品牌   page:页码  size:每页记录数
        public PageResult<Brand> findPage(int page, int size) {
            PageHelper.startPage(page,size);//分页 需要添加在查询的结果的上面
            Page<Brand> pageResult=(Page<Brand>) brandMapper.selectAll();
            return new PageResult<>(pageResult.getTotal(),pageResult.getResult());
        }
    
        @Override//条件查询
        public List<Brand> findList(Map<String, Object> searchMap) {
            Example example = createExample(searchMap);//把方法提取出来了
            return brandMapper.selectByExample(example);
        }
    
        @Override//品牌条件+分页查询
        public PageResult<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
            PageHelper.startPage(page,size);
            Example example = createExample(searchMap);
            Page<Brand> brands= (Page<Brand>) brandMapper.selectByExample(example);
            return new PageResult<>(brands.getTotal(),brands.getResult());
        }
    
        @Override  //根据id查询品牌
        public Brand findById(Integer id) {
           return brandMapper.selectByPrimaryKey(id);
    
        }
    
        @Override //插入数据
        public void add(Brand brand) {
            brandMapper.insert(brand);
        }
    
        @Override //更新 数据
        public void update(Brand brand) {
         brandMapper.updateByPrimaryKeySelective(brand);
        }
    
        @Override //删除
        public void delete(Integer id) {
            brandMapper.deleteByPrimaryKey(id);
        }
    
    
        //把方法提取出来了(创建一个example对象)
        private Example createExample(Map<String,Object> searchMap){
            Example example=new Example(Brand.class);
            // example是Mybatis数据层框架中的一个工具,可以帮我们完成sql语句中where条件句的书写,
            // 相当于where后面的部分,我们可以根据不同的条件来查询和操作数据库,简化书写sql的过程。
            Example.Criteria criteria = example.createCriteria();
            if (searchMap!=null){
                if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                    criteria.andLike("name","%"+(String)searchMap.get("name")+"%");//品牌名字的模糊查询
                }
                if(searchMap.get("letter")!=null&&!"".equals(searchMap.get("letter"))){//letter品牌的首字母
                    criteria.andEqualTo("letter",(String)searchMap.get("letter"));//品牌首字母精确查询
                }
            }
            return example;
        }
    }
  • 相关阅读:
    移动web性能优化从入门到进阶
    授权保存到相册
    授权通讯地址
    windows putty 链接到 linux 免密码
    my docker note
    docker run -i -t --rm
    Command Not Found
    firewall-cmd 笔记
    vim 插件 Tabularize
    vim :find
  • 原文地址:https://www.cnblogs.com/july7/p/12017376.html
Copyright © 2020-2023  润新知