SpringBoot整合SSM之查询

Posted 风骚少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合SSM之查询相关的知识,希望对你有一定的参考价值。

一.引入项目所需要用到的依赖

 <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </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>

        <!-- 分页插件 -->
        <!-- 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>

        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>

        <!-- JDBC for mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

 二.创建实体类

public class Air {
    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层:

这里只需要定义一个接口  然后写一个查询的方法就OK  实现类请接着往下看

public interface IAirDAO {
    ///查询全部
    public List<Air> findAll();
}

四.service层  和以前写的类似

在service层也需要注入dao

    public List<Air> findAll();
@Service("airservice")
public class IAirServiceImpl implements IAirService {

    @Resource(name = "IAirDAO")
    private IAirDAO dao;
    @Override
    public List<Air> findAll() {
        return dao.findAll();
    }
}

五.Controller

@Controller
public class FirstController {

    @Resource(name = "airservice")
    private IAirService iairService;

    @RequestMapping("/gohome")
    public String dohome(){
        return "main";
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public Object findAll(Model model){
        List<Air> all = iairService.findAll();
        return all;
    }
}

然后大家并没有看见我们的sql语句写在哪里  下边我们在resources下边开始书写  创建一个Mapping包 然后建一个和dao层下边的接口名字相同的xml文件 在这个里面写sql语句

项目也不需要用到jsp页面,那么页面放在哪里呢?当然也是放在resources下边

如下:

mapping下边放的是原本dao层放的xml文件 ,templates下边放的是前台页面

项目重点:application.yml文件  链接数据库的语句全部存放在这里边

内容如下:

server:
  port: 8080
spring:
    thymeleaf:
        prefix: classpath:/templates/
        mode: html5
        cache: false
    datasource:
        name: test
        url: jdbc:mysql:///table
        username: root
        password: 123456
        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:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: cn.leon.entity

 

以上是关于SpringBoot整合SSM之查询的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot完成SSM整合之SpringBoot整合junit

SpringBoot整合ssm案例中关于查询

SpringBoot整合SSM三大框架源码剖析之SpringBoot源码剖析

springboot --- 之SSM框架整合

SpringBoot整合SSM之日期类型转换

小白入门之 Springboot整合SSM