前提: 已生成自动Dao 、Bean、和 配置XML。
我们直接在服务层中写:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bihu.study.Service; import com.bihu.study.Bean.Ebook; import com.bihu.study.Bean.EbookExample; import com.bihu.study.Mapper.EbookMapper; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class EbookService { //Dao @Resource private EbookMapper ebookMapper; //模糊查询实现 public List<Ebook> selectAll(String name){ //new一个EbookExample EbookExample ebookExample = new EbookExample(); //new一个EbookExample.createCriteria() EbookExample.Criteria criteria = ebookExample.createCriteria(); //实施模糊查询数据填充 criteria.andNameLike("%" + name + "%"); //查询数据(Dao-selectByExample) 并 返回数据 ※※※ return ebookMapper.selectByExample(ebookExample); } }
然后控制层是这样的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bihu.study.controller; import com.bihu.study.Bean.Ebook; import com.bihu.study.Service.EbookService; import com.bihu.study.resp.CommonResp; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController public class EbookController { @Resource private EbookService ebookService; @GetMapping(value = {"/findEbook"}) public CommonResp findEbook(String name){ //新建一个CommonResp CommonResp<List<Ebook>> resp = new CommonResp<>(); //获取数据库数据 List<Ebook> list = ebookService.selectAll(name); //填充CommonResp 的内容 resp.setContent(list); //返回内容回去 return resp; } }
运行:
推荐另一种写法 就是Bean自动映射的 :
如果要查询其他数据 或 很多数据一起查询。。 岂不是要写很多个参数,这里封装一个类即可,[这是SpringMVC的基础!很简单不解释..]
首先创建一个请求类,一般放req文件夹.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bihu.study.req; public class EbookReq { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getSimpleName()); sb.append(" ["); sb.append("Hash = ").append(hashCode()); sb.append(", id=").append(id); sb.append(", name=").append(name); sb.append("]"); return sb.toString(); } }
只要你查询的名字 和 Bean 的字段名对应即可。【自动封装】
测试:
过滤信息输出写法:
我们查询出来的信息都是全部信息,什么都有,,,但是某些信息不希望被查询出来,那么就用这种写法:
1.新建一个 请求返回类 一般放在resp中,然后内容其实和Bean一样,可以比 Bean少 当然也可以比Bean多:
2.那么服务层 要返回的类型就是 list<请求返回类> 了:
服务层代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bihu.study.Service; import com.bihu.study.Bean.Ebook; import com.bihu.study.Bean.EbookExample; import com.bihu.study.Mapper.EbookMapper; import com.bihu.study.req.EbookReq; import com.bihu.study.resp.EbookResp; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; @Service public class EbookService { //Dao @Resource private EbookMapper ebookMapper; //模糊查询 public List<EbookResp> selectAll(EbookReq req){ EbookExample ebookExample = new EbookExample(); EbookExample.Criteria criteria = ebookExample.createCriteria(); criteria.andNameLike("%" + req.getName()+ "%"); List<Ebook> list = ebookMapper.selectByExample(ebookExample); //转换 List<EbookResp> ebookRespList = new ArrayList<>(); for (Ebook item : list){ EbookResp ebookResp = new EbookResp(); //ebookResp.setId(item.getId()); /*我们不用这种 效率太低 用下面的 BeanUtils*/ //ebookResp.setName(item.getName()); /*我们不用这种 效率太低 用下面的 BeanUtils*/ BeanUtils.copyProperties(item,ebookResp); //这个方法就是自动覆盖填充 ebookRespList.add(ebookResp); } return ebookRespList; } }
控制层代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bihu.study.controller; import com.bihu.study.Bean.Ebook; import com.bihu.study.Service.EbookService; import com.bihu.study.req.EbookReq; import com.bihu.study.resp.CommonResp; import com.bihu.study.resp.EbookResp; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController public class EbookController { @Resource private EbookService ebookService; @GetMapping(value = {"/findEbook"}) public CommonResp findEbook(EbookReq req){ CommonResp<List<EbookResp>> resp = new CommonResp<>(); List<EbookResp> list = ebookService.selectAll(req); resp.setContent(list); return resp; } }
EbookResp:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bihu.study.resp; public class EbookResp { private Long id; private String name; private String description; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getSimpleName()); sb.append(" ["); sb.append("Hash = ").append(hashCode()); sb.append(", id=").append(id); sb.append(", name=").append(name); sb.append("]"); return sb.toString(); } }
然后你试着模糊查询一下sql ,出来的数据就是刚用类过滤掉的了,你可能回想我直接语句 select xxx,xxx,xxx, 这样不就可以了 那么麻烦? 所以 他是用mybatis 的插件写的 ,
所以啊 这里就得是那么麻烦,代码少了很多。
上面的Bean转换如此麻烦,所以自己写一个即可:
新建工具包,然后新建BeanCopy类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bihu.study.Utils; import org.springframework.beans.BeanUtils; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; public class BeanCopy { /** * 单体复制 */ public static <T> T copy(Object source, Class<T> clazz) { if (source == null) { return null; } T obj = null; try { obj = clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); return null; } BeanUtils.copyProperties(source, obj); return obj; } /** * 列表复制 */ public static <T> List<T> copyList(List source, Class<T> clazz) { List<T> target = new ArrayList<>(); if (!CollectionUtils.isEmpty(source)) { for (Object c : source) { T obj = copy(c, clazz); target.add(obj); } } return target; } }
里面就一个单对象复制 和 列表复制 ,,,这个代码来源于 甲哇 讲师。
然后下面的转换 就可以直接一句带过:
那是真的方便啊...