雪一更--软件开发--数据--Spring JDBC

目的:使用Spring data Jdbc 构建应用程序数据领域核心,非databse


Spring JdbcTemplate NamedJdbcTemplate 简化了DAO 及常用  CRUD 操作。

基础: 从 Jdbc 开始,而后基于 JPA 规范和其实现 Hibernate 深入.

JdbcTemplate 是线程安全的,因此Config 配置一个单例。

dataSource 基于 HakariCP.HikariCPDataSource() . 替代默认的 DataSourceManager();


1.Spring boot 引入依赖

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

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

 

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/PRACTICE_SPRING_JDBC?userUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=PRACTICE_SPRING_JDBC
spring.datasource.password=admin

# cp hikari
spring.datasource.hikari.maximum-pool-size=50
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.leak-detection-threshold=20000
spring.datasource.hikari.connection-timeout=60000

批量更新

@Configuration
@ComponentScan(basePackages = {"org.tech.road.springjdbc"})
@EnableAutoConfiguration
public class Config {
  @Bean
  public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }
}
config
public interface VehicleDao {
    Vehicle findByVehicleNo(String vehicleNo);
    List<Vehicle> findAll();
    void insert(Vehicle vehicle);
    void insert(Collection<Vehicle> vehicles);
    void update(Vehicle vehicle);
    void delete(Vehicle vehicle);
}
Dao

 

@SpringBootApplication
public class SpringJdbcApplication {

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

  @Component
  class DBApp implements ApplicationRunner{

    @Autowired
    VehicleDao vehicleDao;

    @Override
    public void run(ApplicationArguments args) throws Exception {
      Vehicle vehicle1 = new Vehicle("TEM0022","Blue",4,4);
      Vehicle vehicle2 = new Vehicle("SEM0022","Red",4,6);
      Vehicle vehicle3 = new Vehicle("QEM0022","Yellow",4,6);
      vehicleDao.insert(List.of(vehicle1,vehicle2,vehicle3));
    }
  }
}
main

Query 

RowCallbackHandler 接口 processRow(ResultSet rs) throws SQLException 处理单行结果.

RowMapper<T> 将结果集映射到自定义对象上,可处理单行结果集和多行结果集. mapRow(ResultSet rs) throws SQLException 中需要构建出

代表一行的对象并将其作为方法返回值返回.

最好作为普通类,而不是内部类,便于重用。

可用Spring 默认的 RowMapper<T> 实现: BeanPropertyRowMapper<T>, 自动将一行映射到指定类的一个实例.

限定:该类必须是一个顶层类,有一个默认或无参构造方法. 属性名匹配规则:同名或是带有下划线的分开名。VEHICLE_NO.

单行结果集:JdbcTemplate.queryForObject( SQL_QUERY, RowMapper<T> impl, condition),

多行 JdbcTemplate.queryForList() 含有map 的 list, 每个 map 存储了结果集中的一行且使用列名作为键。 

查询单个值, queryForObject ( SQL_QUERY, java.lang.Class, [queryParam]);

main


具名参数

批更新,为参数值提供一个map,数组或是 SqlParameterSource 数组.


异常处理:

Spring 为数据访问模块提供了一种一致的数据访问异常处理机制。 抛出DataAccessException 子类. 无需强制捕捉.

Spring JDBC 是通过查找被捕获异常的 errorCode 与 SQLState 查找到具体的 exception.

查看相应 dataBase 的手册.

自定义异常要兼容于 DataAccessException


 

posted @ 2023-04-09 20:07  君子之行  阅读(16)  评论(0)    收藏  举报