SpringBoot整合ssm案例中关于查询
Posted 明渃筱曦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合ssm案例中关于查询相关的知识,希望对你有一定的参考价值。
这里我写查全部和根据条件查询
这里我们引用的依赖和ssm也有区别
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--parent替换为SpringBoot模块中的parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-02</artifactId> <packaging>war</packaging> <name>springboot-02 Maven Webapp</name> <!-- FIXME change it to the project\'s website --> <url>http://www.example.com</url> <!--同样是SpringBoot的--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- 核心依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 可以实现热部署,在IDEA上实现热部署还需一些额外的配置,请查阅资料 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>runtime</scope> </dependency> <!-- JDBC for mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> <!-- 分页插件 --> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <finalName>springboot-02</finalName> </build> </project>
首先从实体类开始
package cn.studio.entity; import cn.studio.util.JsonDateSerializer; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; /** * Created by mycom on 2018/6/23. */ public class AirModel { private Integer id; private String district; @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") @JsonSerialize(using = JsonDateSerializer.class) private Date monitorTime; private Integer pm10; private Integer pm25; private String monitoringStation; private Date createDate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public Date getMonitorTime() { return monitorTime; } public void setMonitorTime(Date monitorTime) { this.monitorTime = monitorTime; } public Integer getPm10() { return pm10; } public void setPm10(Integer pm10) { this.pm10 = pm10; } public Integer getPm25() { return pm25; } public void setPm25(Integer pm25) { this.pm25 = pm25; } public String getMonitoringStation() { return monitoringStation; } public void setMonitoringStation(String monitoringStation) { this.monitoringStation = monitoringStation; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } }
然后是DAO
import cn.studio.entity.AirModel; import java.util.List; /** * Created by mycom on 2018/6/23. */ public interface IAirDAO { //查询所有 public List<AirModel> findAll(); //根据条件查询 public List<AirModel> selectBydistrict(AirModel airModel);
上一篇博客写过这里对应的xml文件的配置位置有所变动
配置中
<!--查询所有--> <select id="findAll" resultType="AirModel"> select * from air_quality_index </select> <!--根据条件查询--> <select id="selectBydistrict" resultType="AirModel"> select * from air_quality_index <where> <if test=\'district!="0"\'> AND district=#{district} </if> </where> </select>
service层中和之前ssm的一样
在service的实现类中要注入dao并且要实现方法重写
在controller中
package cn.studio.controller; import cn.studio.entity.AirModel; import cn.studio.service.IAirService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.List; /** * Created by mycom on 2018/6/23. */ @Controller public class AirController { @Resource(name="airService") private IAirService airService; @RequestMapping("goHome") public String goHome(){ return "index"; } @RequestMapping("/findAll") @ResponseBody public Object findAll(Model model){ List<AirModel> all = airService.findAll(); model.addAttribute("allAir",all); return all; } @RequestMapping("/findBydistrict") @ResponseBody public Object findBydistrict(AirModel airModel){ List<AirModel> all = airService.selectBydistrict(airModel); return all; }
在页面上(忽略删除,删除不在这篇博客上详细介绍)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" th:src="@{/js/jquery-1.8.3.min.js}"></script> <script type="text/javascript"> //根据条件查询 function myselect() { var district=$("#ourSelect").val(); $.ajax({ url: "/findBydistrict", data:{ "district":district }, type:"POST", success: function (data) { //清空数据 $("#list-content").html(\'\'); //追加数据 $.each(data, function (i, dom) { //一个dom就是一个新闻对象 $("#list-content").append("<tr><td>"+ dom.id + "</td><td><a onclick=\'update("+dom.id+")\'>"+dom.district+"</a>" + "</td><td>" + dom.monitorTime + "</td><td>" + dom.pm10 + "</td><td >" + dom.pm25 + "</td><td>"+ dom.monitoringStation+"</td><td><a onclick=\'deleteair("+dom.id+")\'>删除</a></td></tr>"); }); $("tr:odd").css("background","pink"); } }) } function deleteair(id) { alert(id) $.ajax({ url: "/deleteAir", data:{ "id":id }, type:"POST", success: function (data) { $("[name=id]").val(data.id); $("[name=district]").val(data.district); $("[name=monitorTime]").val(data.monitorTime); $("[name=pm10]").val(data.pm10); $("[name=pm25]").val(data.pm25); $("[name=monitoringStation]").val(data.monitoringStation); } }) } load(); //默认初始化 /*点击查询的触发事件*/ function load() { $.ajax({ url: "/findAll", type: "post", success: function (data) { //清空数据 $("#list-content").html(\'\'); //追加数据 $.each(data, function (i, dom) { //一个dom就是一个新闻对象 $("#list-content").append("<tr><td>"+ dom.id + "</td><td><a onclick=\'update("+dom.id+")\'>"+dom.district+"</a>" + "</td><td>" + dom.monitorTime + "</td><td>" + dom.pm10 + "</td><td >" + dom.pm25 + "</td><td>"+ dom.monitoringStation+"</td><td><a onclick=\'deleteair("+dom.id+")\'>删除</a></td></tr>"); }); $("tr:odd").css("background","pink"); } }); }; </script> </head> <body> <table width="70%" border="1" align="center" id="list"> <caption><h1 style="height: 50px;line-height5:0px;border: 1px">空气质量检测信息库</h1> 按区域查询 <select name="district" id="ourSelect"> <option value="0">不限</option> <option value="西城区">西城区</option> <option value="东城区">东城区</option> <option value="海淀区">海淀区</option> <option value="丰台区">丰台区</option> </select> <input type="button" onclick="myselect()" value="查找"/> <a href="/goAddPage">添加空气质量信息</a> </caption> <thead> <tr class="t_head"> <th>序号</th> <th>区域</th> <th>检测时间</th> <th>PM10数据</th> <th>PM2.5数据局</th> <th>监测站</th> <th>操作</th> </tr> </thead> <tbody id="list-content"> </tbody> </table> </body> </html>
这里在补充一点在resources下
标红框的这两个目录分别是存放css,js和html文件的
这里还有一个application.yml文件
server: port: 8080 spring: thymeleaf: prefix: classpath:/templates/ mode: HTML5 cache: false datasource: name: test url: jdbc:mysql://localhost:3306/exam username: root password: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select \'x\' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 mybatis: //配置mapping下的xml文件路径 mapper-locations: classpath:mapping/*.xml //配置别名 type-aliases-package: cn.studio.entity
以上是关于SpringBoot整合ssm案例中关于查询的主要内容,如果未能解决你的问题,请参考以下文章