SpringBoot中JdbcTemplate

Posted 流氓大队长

tags:

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

步骤如下:

依赖:

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

 

1.dao层

public interface IGradeDao {

    public int insertGrade(Grade grade);
    public int updateGrade(Grade grade);
    public int deleteGrade(Integer tid);
    public List<Grade> findAll();

}

 

2.dao中的impl

@Repository
public class IGradeDaoImpl implements IGradeDao {
    //导入jdbctemplate模板
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertGrade(Grade grade) {
        return jdbcTemplate.update("insert into teacher(tname) values(?)",grade.getTname());
    }
    @Override
    public int updateGrade(Grade grade) {
        return jdbcTemplate.update("update teacher set tname=? where tid=?",grade.getTname(),grade.getTid());
    }

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

    @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("tid"),rs.getString("tname"));
                return grade;
            }
        };
        return jdbcTemplate.query("select * from teacher", rowMapper);
    }
}

 

3.entity层

package com.wdksft.entity;

public class Grade {
    public Grade(Integer tid, String tname) {
        this.tid = tid;
        this.tname = tname;
    }
    public Grade(){

    }
    public Grade(String tname) {
        this.tname = tname;
    }
    private Integer tid;
    public Integer getTid() {
        return tid;
    }
    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    private String tname;

}

 

4.Service层

package com.wdksft.service;

import com.wdksft.entity.Grade;

import java.util.List;

public interface IGradeService {

    public int insertGrade(Grade grade);
    public int updateGrade(Grade grade);
    public int deleteGrade(Integer tid);
    public List<Grade> findAll();
}

 

5.Service层中的impl

package com.wdksft.service;

import com.wdksft.dao.IGradeDao;
import com.wdksft.entity.Grade;
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 tid) {
        return iGradeDao.deleteGrade(tid);
    }

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

 

6.controller层

package com.wdksft.controller;



import com.wdksft.entity.Grade;
import com.wdksft.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(""));
    }
    @RequestMapping("/updateGrade")
    public int updateGrade(){
        return  iGradeService.updateGrade(new Grade(1,""));
    }
    @RequestMapping("/deleteGrade")
    public int deleteGrade(){
        return iGradeService.deleteGrade(1);
    }
    @RequestMapping("/findAll")
    public List<Grade> findAll(){
        return iGradeService.findAll();
    }
}

 

7.application.yml

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

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

springboot之多数据源配置JdbcTemplate

springboot编写持久化接口之集成jdbctemplate

SpringBoot中JdbcTemplate

在springboot中使用jdbcTemplate

springboot入门_数据库访问_jdbcTemplate

SpringBoot使用JdbcTemplate