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文件

  • 相关阅读:
    在ASP.NET 2.0中使用WebParts
    Asp.net生成静态页面原理
    提高ASP.Net应用程序性能的十大方法
    Web2.0之Tag标签原理实现浅析
    ASP.NET 2.0中的URL映射
    动态加载控件UserControl到页面上:视图状态问题
    C#自动登录网页浏览页面 抓取数据
    .NET Framework 类库提供的命名空间
    一个用于热部署的框架设想
    重构如何进行?
  • 原文地址:https://www.cnblogs.com/mayuan01/p/12039048.html
Copyright © 2011-2022 走看看