zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    C# 获取枚举 Enum 变量值的 Description 属性
    javascript获取网页URL地址及参数等
    也谈用反射实现Enum→String映射:一种重视性能的方法20090412 21:35一、问题的提出
    LinQ 多表查询
    Windows Service得到当前用户的名字和域
    ASP.NET 部署
    加密解密-C#
    Domino Internet邮件
    C#动态创建表
    读取Excel2000文件
  • 原文地址:https://www.cnblogs.com/lowerma/p/12038501.html
Copyright © 2011-2022 走看看