SpringBoot整合JDBC模板

Posted 慕容子月

tags:

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

目录

Grade实体类

public class Grade {
    private Integer gradeId;
    private String gradeName;

    public Grade(){

    }

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

    public Grade(String gradeName) {
        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;
    }
}

IGradeDao

import com.boot.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();
}

IGradeDaoImpl

import com.boot.dao.IGradeDao;
import com.boot.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);
    }
}

IGradeService

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

IGradeServiceImpl

@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();
    }
}

JDBCTemplateController

@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();
    }
}

application.yml

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

##更改Tomcat端口
server:
  port: 8081
  ##指定当前工程项目访问地址
  context-path: /jdbc

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

Java 微服务 day02 源代码 SpringBoot 实战开发 整合JDBC和事务(数据库连接池)

Java 微服务 乐优网络商城 day02 源代码 SpringBoot 实战开发 整合JDBC和事务(数据库连接池)

SpringBoot整合使用JdbcTemplate

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段

SpringBoot--整合JDBC

idea springboot中整合jdbc连接MySQL中,Schemas中无mybatis怎么解决?