自学内容网 自学内容网

SpringBoot+JdbcTempalte+SQLServer

SpringBoot+SqlServer+JdbcTempalte

前言

小项目或者做demo时可以使用jdbc+sql server解决即可,这篇就基于spring boot环境使用jdbc连接sql server数据库,和spring mvc系列保持一致。 在spring boot中使用jdbc 连接sql server数据只需要引入两个jar:spring-boot-starter-jdbc、spring-boot-starter-data-jpa

1. pom环境配置

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!--<exclusions>-->
            <!--<exclusion>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
            <!--</exclusion>-->
        <!--</exclusions>-->
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</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-data-jpa</artifactId>
        <version>1.5.4.RELEASE</version>
    </dependency>
   
</dependencies>

2. Controller

注意:我这里直接用Controller简单实现一下效果,可以完善为三层架构,dao层负责与数据库交互,service层负责业务处理,controller负责对业务模块流程的控制。

package com.zhwy.controller;

import com.zhwy.common.Result;
import com.zhwy.pojo.SurfChnMulMin;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author xjz_2002
 * @version 1.0
 */
@RestController
public class JdbcController {

    @Resource
    JdbcTemplate jdbcTemplate;

    @RequestMapping("/selectInfo")
    public Result selectInfo() {
        String sql = "select Station_Id_C, Station_Name, Datetime, PRE, RHU, TEM, WIN_D_Avg_2mi as winDAvg2mi, WIN_S_Avg_2mi as winSAvg2mi" +
                " from SURF_CHN_MUL_MIN where Station_Id_C='B1677'";
        List<SurfChnMulMin> scmmList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SurfChnMulMin.class));
        return Result.ok(scmmList);
    }

    @RequestMapping("/getNewDate")
    public Result getNewDate(){
        String sql = "SELECT Datetime FROM SURF_CHN_MUL_MIN " +
                "WHERE Station_Id_C = ? " +
                "AND Datetime = (SELECT MAX(Datetime) FROM SURF_CHN_MUL_MIN)";
        Timestamp timestamp = jdbcTemplate.queryForObject(sql, Timestamp.class, "B1677");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datatime = sdf.format(timestamp);

        return Result.ok(datatime);
    }


    @RequestMapping("/updateInfo")
    public String updateInfo(Double pre) {
        String sql = "UPDATE SURF_CHN_MUL_MIN SET PRE = ? WHERE PRE = 0.1";
        int affectedRow = jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement psmt) throws SQLException {
                psmt.setDouble(1,pre);
            }
        });
        System.out.println("受影响行数affectedRow=" + affectedRow);
        return "受影响行数affectedRow=" + affectedRow;
    }

    @RequestMapping("/insertInfo")
    public String insertInfo() {
        String sql = "INSERT INTO SURF_CHN_MUL_MIN " +
                " VALUES ('B1666', '七山滑雪场结束区', '2024-01-26 08:50:00.000', NULL, '-8.1', '34.0', NULL, '0.2')";
        int updateRow = jdbcTemplate.update(sql);
        System.out.println("插入行数updateRow=" + updateRow);
        return "插入行数updateRow=" + updateRow;
    }

    @RequestMapping("/deleteInfo")
    public String deleteInfo(String staId) {
        String sql = "DELETE FROM SURF_CHN_MUL_MIN WHERE Station_Id_C=?";
        int deleteRow = jdbcTemplate.update(sql, staId);
        System.out.println("删除行数deleteRow=" + deleteRow);
        return "删除行数deleteRow=" + deleteRow;
    }

}

详细文章请参考:spring boot(二): spring boot+jdbctemplate+sql server - 歪头儿在帝都 - 博客园 (cnblogs.com)


原文地址:https://blog.csdn.net/m0_53125903/article/details/135922550

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!