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

SpringBoot中JdbcTemplate

时间:2019-12-14 11:33:56      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:list   tin   driver   tar   where   throws   数据   tco   result   

步骤如下:

依赖:

<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

标签:list   tin   driver   tar   where   throws   数据   tco   result   

原文地址:https://www.cnblogs.com/lowerma/p/12038501.html

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