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

posted @ 2019-12-14 14:11  林染plus  阅读(2141)  评论(1编辑  收藏  举报