码迷,mamicode.com
首页 > 数据库 > 详细

SpringBoot整合JDBC模板

时间:2019-12-14 12:05:05      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:throws   tomcat端口   ann   inter   epo   java   ice   str   目录   

目录

技术图片

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模板

标签:throws   tomcat端口   ann   inter   epo   java   ice   str   目录   

原文地址:https://www.cnblogs.com/dabrk/p/12038337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!