zoukankan      html  css  js  c++  java
  • springboot使用jdbcTemplate案例

    1 创建实体类

    public class Student {
        private Integer stuid;
        private String stuname;
    
        public Integer getStuid() {
            return stuid;
        }
    
        public void setStuid(Integer stuid) {
            this.stuid = stuid;
        }
    
        public String getStuname() {
            return stuname;
        }
    
        public void setStuname(String stuname) {
            this.stuname = stuname;
        }
    
        public Student(Integer stuid, String stuname) {
            this.stuid = stuid;
            this.stuname = stuname;
        }
        public Student(){}
    
        public Student(String stuname) {
            this.stuname = stuname;
        }
    
    }

    2 创建Dao层(Dao层接口和实现类合并)

    @Repository
    public class StudentDao {
    
        @Resource
        private JdbcTemplate jdbcTemplate;
    
        //查询所有学生信息
        public List<Student> getStudent(){
            RowMapper<Student> rowMapper=new RowMapper<Student>() {
                @Override
                public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                    Student student=new Student(resultSet.getInt("stuid"),resultSet.getString("stuname"));
                    return student;
                }
            };
            return jdbcTemplate.query("select * from student",rowMapper);
        }
    
        //删除学生信息
        public int delStudent(Integer stuid){
            return jdbcTemplate.update("delete from student where stuid=?",stuid);
        }
        //添加学生
        public int insertStudent(Student student){
            return jdbcTemplate.update("insert into student(stuname) values (?)",student.getStuname());
        }
        //修改学生信息
        public int updaStudent(Student student){
            return jdbcTemplate.update("update student set stuname=? where stuid=? ",student.getStuname(),student.getStuid());
        }
    }

    3 创建Service层(Service层 接口和实现类合并)

    @Service
    public class StudentService {
        @Resource
        private StudentDao studentDao;
        //添加学生信息
       public List<Student> getStudent(){
           return studentDao.getStudent();
       }
       //删除学生信息
        public int delStudent(Integer stuid){
           return studentDao.delStudent(stuid);
        }
        //添加学生信息
        public int insertStudent(Student student){
           return studentDao.insertStudent(student);
        }
        //修改学生信息
        public int updaStudent(Student student){
           return studentDao.updaStudent(student);
        }
    }

    4 创建application.yml文件 

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql:///springbootjpa
        username: root
        password: 123

    5 创建Controller层

    @RestController
    public class StudentController {
        @Resource
        private StudentService studentService;
    
        //查询所有学生信息
        @RequestMapping("/getStudent")
        public List<Student> getStudent(){
            return studentService.getStudent();
        }
        //删除学生信息
        @RequestMapping("/delStudent")
        public int delStudent(){
            return studentService.delStudent(8);
        }
        //添加学生信息
        @RequestMapping("/insertStudent")
        public int insertStudent(){
            return studentService.insertStudent(new Student("bb"));
        }
        //修改学生信息
        @RequestMapping("/updaStudent")
        public int updaStudent(){
            return studentService.updaStudent(new Student(3,"liuli"));
        }
    }

    6 启动程序

    @SpringBootApplication
    public class StartSpringBoot {
        public static void main(String[] args) {
            SpringApplication.run(StartSpringBoot.class,args);
        }
    }
  • 相关阅读:
    互斥锁和条件变量实现生产者消费者问题
    信号量实现生产者消费者问题
    IPC进程间通信---共享内存
    IPC进程间通信---消息队列
    图的遍历---广度优先遍历和深度优先遍历
    图的两种存储方式---邻接矩阵和邻接表
    内存分配---FF、BF、WF三种算法
    C++的前置++、后置++和前置--、后置--
    IPC进程间通信---信号量
    Linux进程间通信---管道和有名管道
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12038202.html
Copyright © 2011-2022 走看看