• Springboot与JdbcTemplate


    小案例

    目录结构

      

     依赖

    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
        </dependency>

    数据库

      

    entity

    package Springboot.entity;
    
    
    
    public class student {
        private Integer stuid;
        private String stuname;
        private Integer age;
    
        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 Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public student() {
        }
    
        public student(Integer stuid, String stuname, Integer age) {
            this.stuid = stuid;
            this.stuname = stuname;
            this.age = age;
        }
    }

    dao

    public interface Istudao {
        public int insertstu(student stu);
        public int updatestu(student stu);
        public int deletestu(Integer id);
        public List<student> findAll();
    }

    daoimpl

    @Repository
    public class Istudaoimpl implements Istudao {
    
        @Resource
        private JdbcTemplate jdbcTemplate;
    
    
        @Override
        public int insertstu(student stu) {
            return jdbcTemplate.update("insert into student (stuid,stuname,age) values(?,?,?)",stu.getStuid(),stu.getStuname(),stu.getAge());
        }
    
        @Override
        public int updatestu(student stu) {
            return jdbcTemplate.update("update student set stuname=? where stuid=?",stu.getStuname(),stu.getStuid());
        }
    
        @Override
        public int deletestu(Integer id) {
            return jdbcTemplate.update("delete from student where stuid=?",id);
        }
    
        @Override
        public List<student> findAll() {
            //封装行数据映射
            RowMapper<student> rowMapper=new RowMapper<student>() {
                @Override
                public student mapRow(ResultSet rs, int rowNum) throws SQLException {
                    student stu=new student(rs.getInt("stuid"),rs.getString("stuname"),rs.getInt("age"));
                    return stu;
                }
            };
            return jdbcTemplate.query("select * from student", rowMapper);
        }
    }

    service

    public interface Istuservice {
        public int insertstu(student stu);
        public int updatestu(student stu);
        public int deletestu(Integer id);
        public List<student> findAll();
    }

    serviceimpl

    @Service("istuservice")
    public class Istuserviceimpl implements Istuservice {
        @Resource
        private Istudao istudao;
    
    
        @Override
        public int insertstu(student stu) {
            return istudao.insertstu(stu);
        }
    
        @Override
        public int updatestu(student stu) {
            return istudao.updatestu(stu);
        }
    
        @Override
        public int deletestu(Integer id) {
            return istudao.deletestu(id);
        }
    
        @Override
        public List<student> findAll() {
            return istudao.findAll();
        }
    }

    Controller

    @RestController
    @RequestMapping("/stu")
    public class stuController {
        @Resource
        private Istuservice istuservice;
    
        @RequestMapping("/insertstu")
        public int insertstu(){
            System.out.println("insert");
            return istuservice.insertstu(new student(10,"S1",23));
        }
        @RequestMapping("/updatestu")
        public int updatestu(){
            System.out.println("update");
            return  istuservice.updatestu(new student(10,"S2",10012));
        }
        @RequestMapping("/deletestu")
        public int deletestu(){
            System.out.println("delete");
    
            return istuservice.deletestu(10);
        }
        @RequestMapping("/findAll")
        public List<student> findAll(){
            System.out.println("select");
            return istuservice.findAll();
        }
    }

    application

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql:///student
        username: root
        password: 123
    
    ##更改Tomcat端口
    server:
      port: 8081
      ##指定当前工程项目访问地址
      context-path: /jdbc

    主程序

    @SpringBootApplication
    public class Istu {
        public static void main(String[] args) {
            SpringApplication.run(Istu.class,args);
        }
    }

    启动

     

     其他省略

  • 相关阅读:
    mysql查找有某列但没有此列索引的表
    mysql找到所有索引
    mysq在某一刻同时获取主从库的位置点
    新书《深入应用C++11:代码优化与工程级应用》出版,感谢支持
    c++11实现一个简单的lexical_cast
    应该用bind+function取代虚函数吗?
    《深入应用C++11:代码优化与工程级应用》开始发售
    一个更好的C++序列化/反序列化库Kapok
    C++技术沙龙主要内容
    C++11模版元编程
  • 原文地址:https://www.cnblogs.com/whtt/p/12039047.html
Copyright © 2020-2023  润新知