spring 整合JDBC进行增删改查

1.执行单条DML 语句

修改dao持久层代码

点击查看代码
public int insertUsers(Users users) {
        //创建sql语句
        String sql = "insert into users value(default,?,?)";
        Object[] params = {users.getUsername(), users.getUsersex() };

        return this.jdbcTemplate.update(sql,params);
    }

服务层代码

点击查看代码
public int addUsers(Users users) {
        return this.usersDao.insertUsers(users);
    }

测试代码

点击查看代码
 public static void main(String[] args) {
        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersServiceImpl usersService = applicationContext.getBean("usersService", UsersServiceImpl.class);
        Users users = new Users();
        users.setUsername("spring jdbc");
        users.setUsersex("nan");
        int i = usersService.addUsers(users);
        System.out.println(i);
    }

2.批量执行DML 语句

posted @ 2021-12-16 22:06  风的指引  阅读(60)  评论(0)    收藏  举报