zoukankan      html  css  js  c++  java
  • SpringBoot使用JdbcTemplate

    1.导入依赖

     <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
    
            <groupId>org.springframework.boot</groupId>
    
            <artifactId>spring-boot-starter-jdbc</artifactId>
    
        </dependency>
    
    
            <dependency>
    
            <groupId>mysql</groupId>
    
            <artifactId>mysql-connector-java</artifactId>
    
            <version>5.1.32</version>
    
        </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    
            <dependency>
    
            <groupId>org.springframework.boot</groupId>
    
            <artifactId>spring-boot-starter-test</artifactId>
    
            <scope>test</scope>
    
    
            <exclusions>
    
    
            <exclusion>
    
            <groupId>org.junit.vintage</groupId>
    
            <artifactId>junit-vintage-engine</artifactId>
    
        </exclusion>
    
        </exclusions>
    
        </dependency>
    依赖

    2.Entity实体类

    3.创建Dao层

    4.创建Dao层实现类Impl

    package com.my.dao.impl;
    
    import com.my.dao.IStudentDao;
    import com.my.entity.Student;
    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 StudentDaoImpl implements IStudentDao {
        //导入JDBCTemplate模板
        @Resource
        private JdbcTemplate jdbcTemplate;
    
        @Override
        public int insertGrade(Student grade) {
            return  jdbcTemplate.update("insert into studentinfo(stu_name) values(?)",grade.getStu_name());
        }
        @Override
        public int updateGrade(Student grade) {
            return jdbcTemplate.update("update studentinfo set stu_name=? where stu_id=?",grade.getStu_name(),grade.getStu_id());
        }
        @Override
        public int deleteGrade(Integer id) {
            return jdbcTemplate.update("delete from studentinfo where stu_id=?",id);
        }
        @Override
        public List<Student> findAll() {
            //封装行数据映射
            RowMapper<Student> rowMapper=new RowMapper<Student>() {
                @Override
                public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                    Student student=new Student(resultSet.getInt("stu_id"),resultSet.getString("stu_name"));
                    return student;
                }
    
        };
            return jdbcTemplate.query("select * from studentinfo", rowMapper);
        }
    }
    View Code

    5.Service层

    6.Service实现类Impl

    package com.my.service.impl;
    
    
    import com.my.dao.IStudentDao;
    import com.my.entity.Student;
    import com.my.service.StudentService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @Service("iGradeService")
    public class StudentServiceImpl implements StudentService {
        @Resource
        private IStudentDao iGradeDao;
    
    
        @Override
        public int insertGrade(Student grade) {
            return iGradeDao.insertGrade(grade);
        }
    
        @Override
        public int updateGrade(Student grade) {
            return iGradeDao.updateGrade(grade);
        }
    
        @Override
        public int deleteGrade(Integer id) {
            return iGradeDao.deleteGrade(id);
        }
    
        @Override
        public List<Student> findAll() {
            return iGradeDao.findAll();
        }
    }
    View Code

    7.Controller层

    8.resources文件

      application.properties文件

  • 相关阅读:
    python3 numpy基本用法归纳总结
    MySQL 中的三中循环 while loop repeat 的基本用法
    什么是网关及网关作用
    网络扫描工具nmap
    nmap基本使用方法
    nmap脚本使用总结
    用Delphi将数据导入到Excel并控制Excel
    delphi Form属性设置 设置可实现窗体无最大化,并且不能拖大拖小(写一个WM_EXITSIZEMOVE的空函数)
    Delphi 数据类型列表
    一个队列类的实现(比delphi自带的速度快70倍)(线程安全版本)
  • 原文地址:https://www.cnblogs.com/mayuan01/p/12039048.html
Copyright © 2011-2022 走看看