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);
        }
    }
  • 相关阅读:
    notepad++ 编辑器链接地址可点击
    window的cmd窗口运行git
    php update for mac
    sublime打开文件时自动生成并打开.dump文件
    不能设置sublime text 2 为默认编辑器
    sublime text 2 配置文件
    Compass被墙后如何安装安装
    everything搜索工具小技巧
    Ubuntu安装已经下载好的文件包
    Flutter 异步Future与FutureBuilder实用技巧
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12038202.html
Copyright © 2011-2022 走看看