发布时间:2024-05-10 13:01
SpringBoot对数据库操作有多种方式,这里主要介绍的是整合Mybatis并且是采用非注解的方法实现的对数据库的多条件查询的方式,在具体的功能实现时,遇到了查询数据为空的情况,在下文一并做出解答。
1.1 首先需要写好mybatis的配置文件
1.2 在SpringBoot的配置文件application.properties或者是application.yml文件中进行配置
mybatis: config-location: classpath:mybatis/mybatis-config.xml #这是mybatis的配置文件的路径 mapper-locations: classpath:mybatis/mapper/*.xml #这是mappper的路径,*.xml表示包含在该路径下的所有xml文件
2.1 数据库中表的设计
数据库的表名为equipment,具体的字段以及存储的信息如下
2.2 实体类的设计
package com.atguigu.springboot.entities; import org.springframework.boot.autoconfigure.domain.EntityScan; import java.util.Date; public class Equipment { private String eid; private String ename; private String estatic; private Integer espace; private String ecode; private Date etime; public String getEid() { return eid; } public void setEid(String eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getEstatic() { return estatic; } public void setEstatic(String estatic) { this.estatic = estatic; } public Integer getEspace() { return espace; } public void setEspace(Integer espace) { this.espace = espace; } public String getEcode() { return ecode; } public void setEcode(String ecode) { this.ecode = ecode; } public Date getEtime() { return etime; } public void setEtime(Date etime) { this.etime = etime; } public Equipment(String eid, String ename, String estatic, Integer espace, String ecode, Date etime) { this.eid = eid; this.ename = ename; this.estatic = estatic; this.espace = espace; this.ecode = ecode; this.etime = etime; } public Equipment(){ } @Override public String toString() { return \"Equipment{\" + \"eid=\'\" + eid + \'\\\'\' + \", ename=\'\" + ename + \'\\\'\' + \", estatic=\'\" + estatic + \'\\\'\' + \", espace=\" + espace + \", ecode=\'\" + ecode + \'\\\'\' + \", etime=\" + etime + \'}\'; } }
3.1 页面设计(equipment.html)
在设计的过程中使用了bootstrap框架,下面仅给出核心代码以及显示效果
//查找的表单设计 //显示设计
设备ID | 设备名称 | 状态 | 检测间隔(min) | 设备版本 | 上传时间 |
---|---|---|---|---|---|
[[${equip.ename}]] |
3.2 Controller设计
@PostMapping(\"/find_equipment_required\") public String find_equipment_required(@RequestParam(\"ename\") String ename, @RequestParam(\"eid\") String eid, @RequestParam(\"estatic\") String estatic, @RequestParam(\"ecode\") String ecode, Model model){ Equipment equip=new Equipment(); equip.setEname(ename); equip.setEid(eid); equip.setEstatic(estatic); equip.setEcode(ecode); Collectionequipment=equipmentDao.getRequired(equip); System.out.println(equipment); model.addAttribute(\"equipment\",equipment); return \"equipment/equipment\"; }
3.3 Dao层设计
package com.atguigu.springboot.dao; import com.atguigu.springboot.entities.Equipment; import com.atguigu.springboot.mapper.EquipmentMapper; import com.atguigu.springboot.service.EquipmentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @Repository public class EquipmentDao { @Autowired EquipmentService equipmentService; Mapmap=null; // 按照要求查询设备 public Map getRequiredmap(Equipment equipment){ map=new HashMap<>(); List equipments=equipmentService.findByRequired(equipment); System.out.println(equipments.size()); for(int i=0;i getRequired(Equipment equipment){ this.getRequiredmap(equipment); return map.values(); } }
3.4 Service层设计
package com.atguigu.springboot.service; import com.atguigu.springboot.entities.Equipment; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; public interface EquipmentService { // 按要求查找设备 public ListfindByRequired(Equipment equipment); } //实现类 package com.atguigu.springboot.service; import com.atguigu.springboot.entities.Equipment; import com.atguigu.springboot.mapper.EquipmentMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class EquipmentServiceImpl implements EquipmentService { @Autowired EquipmentMapper equipmentMapper; @Override public List findByRequired(Equipment equipment) { return equipmentMapper.findByRequired(equipment); } }
3.5 Mapper设计
//Mapper类 package com.atguigu.springboot.mapper; import com.atguigu.springboot.entities.Equipment; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; import java.util.Map; @Repository public interface EquipmentMapper { // 按要求查找设备 public ListfindByRequired(Equipment equipment); }
//对应的mapper的xml文件,实现多条件查询
4.1 问题
多条件查询时并未在数据库中得到数据
4.2 解决方法
在对数据库的设计时,vachar类型的是utf-8类型,在多条件查询时由于输入的大多数是中文,所以查询不到数据。在对数据库的配置时需要特别注意加上?useUnicode=true&characterEncoding=UTF-8
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost/jdbc?useUnicode=true&characterEncoding=UTF-8 driver-class-name: com.mysql.jdbc.Driver
输入页面
结果页面