当前位置:   article > 正文

Spring_JdbcTemplate_注解版_jdbctemplate注解

jdbctemplate注解

在这里插入图片描述

配置类:

@ComponentScan("com.Zjy.jdbcTemplate")
@Configuration
public class jdbcConfig {
    //数据源
    @Bean
    public DataSource dataSource()throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("root");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql:///test2");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate()throws Exception{
        //在容器中找组件,特殊处理,不会重写创建
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());

        return jdbcTemplate;

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

javabean

@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
    private Integer id;
    private String username;
    private Integer age;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

dao:

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void test1(){
        String sql = "select count(*) from tbl_user ";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        System.out.println(integer);
    }

    public User test2(Integer id) {
        String sql = "select * from tbl_user where id = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }
    public List<User> test3(){
        String sql = "select * from tbl_user";
        List<User> list =  jdbcTemplate.query (sql, new BeanPropertyRowMapper<User>(User.class));
        return list;
    }
    public void test4(List<Object[]> batchArgs){
        String sql = "insert into tbl_user values(null, ?, ?)";
        List<Object> list = new ArrayList<>();
        list.add(new Object[]{"A", 33});
        list.add(new Object[]{"C", 32});
        jdbcTemplate.batchUpdate(sql,  batchArgs);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

service:

@Service
public class UserService {
    @Autowired
    UserDao userDao;
    public void test1(){
        userDao.test1();
    }

    public User test2(Integer id){
        User user = userDao.test2(id);
        return user;
    }

    public List<User> test3(){
        List<User> list = userDao.test3();
        return list;
    }
    public void test4(List<Object[]> batchArgs){
        List<Object> list = new ArrayList<>();
        list.add(new Object[]{"A", 33});
        list.add(new Object[]{"C", 32});
        userDao.test4(batchArgs);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = jdbcConfig.class)
public class test {
    @Autowired
    UserService userService;
    @Test
    public void test1(){
        userService.test1();

    }
    @Test
    public void test2(){
        User user = userService.test2(1);
        System.out.println(user);
    }

    @Test
    public void test3(){
        List<User> list = userService.test3();
        list.forEach(user -> System.out.println(user));
    }
    @Test
    public void test4(){
        List<Object[]> list = new ArrayList<>();
        list.add(new Object[]{"A", 33});
        list.add(new Object[]{"C", 32});
        userService.test4(list);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/576800
推荐阅读
相关标签
  

闽ICP备14008679号