SpringBoot整合ssm案例中关于查询

这里我写查全部和根据条件查询

这里我们引用的依赖和ssm也有区别

"1.0" encoding="UTF-8"?>

"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">

   
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    
    4.0.0

    springboot-02
    war

    springboot-02 Maven Webapp
    
    http://www.example.com

    
    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-logging
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-devtools
            true
            runtime
        

        
        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
        
            com.alibaba
            druid
            1.0.11
        

        
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.3
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    

    
        springboot-02
    

首先从实体类开始

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 findAll();
    //根据条件查询
    public List 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 all = airService.findAll();
        model.addAttribute("allAir",all);
        return all;
    }

    @RequestMapping("/findBydistrict")
    @ResponseBody
    public Object findBydistrict(AirModel airModel){
        List all = airService.selectBydistrict(airModel);
        return all;
    }

在页面上(忽略删除,删除不在这篇博客上详细介绍)


"http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

    "UTF-8">
    Title
    
    


"70%" border="1" align="center" id="list">
    class="t_head">
        "list-content">
    

"height: 50px;line-height5:0px;border: 1px">空气质量检测信息库

按区域查询 <select name="district" id="ourSelect"> select> "button" οnclick="myselect()" value="查找"/> "/goAddPage">添加空气质量信息
序号 区域 检测时间 PM10数据 PM2.5数据局 监测站 操作

 这里在补充一点在resources下

SpringBoot整合ssm案例中关于查询_第1张图片

标红框的这两个目录分别是存放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

 

转载于:https://www.cnblogs.com/my-123/p/9219630.html

你可能感兴趣的:(java,数据库,javascript,ViewUI)