Springboot-JDBCTemplate

Posted

tags:

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

   引入依赖架包

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>

 

  yml配置文件:

##更改Tomcat端口
server:
  port: 8081

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///myschool
    password: root
    username: root

 

  创建项目(目录结构如下)

 

 

 

 

 

  实体类:

 

package com.wdy.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


public class Grade {

    private Integer gradeId;
    private String gradeName;

    public Grade() {

    }

    public Grade(String gradeName) {
        this.gradeName = gradeName;
    }

    public Grade(Integer gradeId, String gradeName) {
        this.gradeId = gradeId;
        this.gradeName = gradeName;
    }

    public Integer getGradeId() {
        return gradeId;
    }

    public void setGradeId(Integer gradeId) {
        this.gradeId = gradeId;
    }

    public String getGradeName() {
        return gradeName;
    }

    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }
}

 

  Dao层(增删改查方法):

 

package com.wdy.dao;

import com.wdy.entity.Grade;

import java.util.List;

public interface IGradeDao {
    public int insertGrade(Grade grade);
    public int updateGrade(Grade grade);
    public int deleteGrade(Integer id);
    public List<Grade> findAll();

}
package com.wdy.dao.impl;


import com.wdy.dao.IGradeDao;
import com.wdy.entity.Grade;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class IGradeDaoImpl implements IGradeDao {
    //导入JDBCTemplate模板
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insertGrade(Grade grade) {
        return  jdbcTemplate.update("insert into Grade(GradeName) values(?)",grade.getGradeName());
    }

    @Override
    public int updateGrade(Grade grade) {
        return jdbcTemplate.update("update Grade set GradeName=? where GradeId=?",grade.getGradeName(),grade.getGradeId());
    }

    @Override
    public int deleteGrade(Integer id) {
        return jdbcTemplate.update("delete from Grade where GradeId=?",id);
    }

    @Override
    public List<Grade> findAll() {
        //封装行数据映射
        RowMapper<Grade> rowMapper=new RowMapper<Grade>() {
            @Override
            public Grade mapRow(ResultSet rs, int rowNum) throws SQLException {
                Grade grade=new Grade(rs.getInt("GradeId"),rs.getString("GradeName"));
                return grade;
            }
        };
        return jdbcTemplate.query("select * from Grade", rowMapper);
    }
}

 

  Service:

package com.wdy.service;

import com.wdy.entity.Grade;

import java.util.List;

public interface IGradeService {
    public int insertGrade(Grade grade);
    public int updateGrade(Grade grade);
    public int deleteGrade(Integer id);
    public List<Grade> findAll();
}
package com.wdy.service.impl;

import com.wdy.dao.IGradeDao;
import com.wdy.entity.Grade;
import com.wdy.service.IGradeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("iGradeService")
public class IGradeServiceImpl implements IGradeService {
    @Resource
    private IGradeDao iGradeDao;


    @Override
    public int insertGrade(Grade grade) {
        return iGradeDao.insertGrade(grade);
    }

    @Override
    public int updateGrade(Grade grade) {
        return iGradeDao.updateGrade(grade);
    }

    @Override
    public int deleteGrade(Integer id) {
        return iGradeDao.deleteGrade(id);
    }

    @Override
    public List<Grade> findAll() {
        return iGradeDao.findAll();
    }
}

 

  控制器:

package com.wdy.controller;


import com.wdy.entity.Grade;
import com.wdy.service.IGradeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class JDBCTemplateController {
    @Resource
    private IGradeService iGradeService;


    @RequestMapping("/insertGrade")
    public int insertGrade(){
        return iGradeService.insertGrade(new Grade("S1"));
    }
    @RequestMapping("/updateGrade")
    public int updateGrade(){
        return  iGradeService.updateGrade(new Grade(10012,"S2"));
    }
    @RequestMapping("/deleteGrade")
    public int deleteGrade(){
        return iGradeService.deleteGrade(10012);
    }
    @RequestMapping("/findAll")
    public List<Grade> findAll(){
        return iGradeService.findAll();
    }
}

 

  调用:

package com.wdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartSpringBoot {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBoot.class,args);
    }
}

 

  启动项目进入浏览器输入查询地址就可以了!

以上是关于Springboot-JDBCTemplate的主要内容,如果未能解决你的问题,请参考以下文章

在区分源代码,对象代码,汇编代码和机器代码时,我有一个困惑

Java中普通代码块,构造代码块,静态代码块区别及代码示例

Java中普通代码块,构造代码块,静态代码块区别及代码示例2 构造代码块

Java中普通代码块,构造代码块,静态代码块区别及代码示例2 构造代码块

上古卷轴5代码

低代码究竟是啥?