Springboot原⽣JDBC操作数据库  Spring提供了spring-jdbc: 对jdbc的轻量级的封装, 提供⼀个⼀个JdbcTemplate类操作数据
1、导⼊jdbc启动器 starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springBoot⽗项⽬中, 设置⼀个Mysql的版本: <mysql.version>8.0.20</mysql.version>
<version>5.1.43</version>⾃定义版本  -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
2、springBoot的配置⽂件中配置数据库四⼤参数
#mysql数据库四⼤参数
spring:
datasource:
#driver-class-name: sql.jdbc.Driver  // 如果使⽤mysql 5.1.43
sql.cj.jdbc.Driver  //如果使⽤mysql 8.0.20 需要指定时区
url: jdbc:mysql://localhost:3306/house?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username:root
password:root
3、springBoot: 配置好 DataSource,  使⽤哪⼀个连接池的技术
  通过测试DataSource:  默认: Hikari的数据源
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootJdbcApplicationTests {
@Autowired
private DataSource ds;
// 测试jdbc数据源是否更换成功
@Test
public void test1() {
System.out.Class().getName());
}
打印默认数据源为:
com.zaxxer.hikari.HikariDataSource
SpringBoot 默认⽀持数据源:
//查看DataSourceAutoConfiguration中的⽅法
@Configuration
@Conditional(PooledDataSourceCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.Generic.class,
DataSource Jmx Configuration.class })
protected static class PooledDataSourceConfiguration {
}
更改SpringBoot使⽤某种数据源:  在默认⽀持的数据源选⼀个:
1) 第⼀种⽅式:  在pom⽂件, 排除Hikari的依赖, 添加其他(dbcp2)数据源, (不推荐)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<!-- 排除HikariCP依赖传递 -->
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
添加某个数据源:
<!-- 添加dbcp2的连接池 -->
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
2)第⼆种⽅式:  不在pom⽂件中排除hikari依赖, 在springboot配置⽂件中指定使⽤哪个数据源(DataSource) (推荐)#mysql数据库四⼤参数
spring:
datasource:
sql.jdbc.Driver  // 如果使⽤mysql 5.1.43
url:jdbc:mysql://localhost:3306/house?useUnicode=true&characterEncoding=UTF-8
username:root
password:root
  #指定使⽤哪个数据源 ***
type:org.apachemons.dbcp2.BasicDataSource
如果需要添加其他数据源⽐如(C3P0, druid)使⽤的数据源不是SpringBoot默认⽀持的
导⼊druid / C3P0 的依赖:
<!--添加的druid的连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
...
在springBoot配置⽂件中指定数据源
#mysql数据库四⼤参数
spring:
datasource:
sql.jdbc.Driver  // 如果使⽤mysql 5.1.43
url:jdbc:mysql://localhost:3306/house?useUnicode=true&characterEncoding=UTF-8
username:root
password:root
  #指定使⽤哪个数据源
#type: org.apachemons.dbcp2.BasicDataSource
type:com.alibaba.druid.pool.DruidDataSource  //或者添加C3p0的数据源
3)第三种⽅式: 编写⼀个DataSource的配置类, 覆盖SpringBoot的DataSourceAutoConfiguration
@Configuration
public class DruidConfig {
@Autowired
// springBoot的运⾏环境类, 获取到springBoot配置⽂件中内容
private Environment environment;
// 配置⼀个DataSource
@Bean
public DataSource createDataSource() { // 创建⼀个Druid的数据源 DruidDataSource
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.Property("spring.datasource.driver-class-name"));
druidDataSource.Property("spring.datasource.url"));
druidDataSource.Property("spring.datasource.username"));jdbctemplate查询一条数据
druidDataSource.Property("spring.datasource.password")); // 初始连接数
druidDataSource.setInitialSize(Integer.Property("spring.datasource.druid.initSize"))); // 最⼤连接数
druidDataSource.setMaxActive(Integer.Property("spring.datasource.druid.maxSize"))); // 最⼩连接数
druidDataSource.setMinIdle(Integer.Property("spring.datasource.druid.minSize")));
return druidDataSource;
}
可以配置连接参数
#mysql数据库四⼤参数
spring:
datasource:
#driver-class-name: sql.jdbc.Driver
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/house?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
#指定使⽤哪个数据源
#type: org.apachemons.dbcp2.BasicDataSource
#type: com.alibaba.druid.pool.DruidDataSource
druid:
initSize: 10
maxSize: 100
minSize: 10
profiles:
active: dev
使⽤Spring-jdbc提供的JdbcTemplate操作数据库:
Employee.java实体类:
@Data
public class Employee {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double comm;
private Double sal;
private Integer deptno;
}
EmployeeDao.java接⼝
public interface EmployeeDao {
 //根据id查询
public Employee  queryById(int empno) throws Exception;
  //查询指定页数据
public List<Employee>  queryByPage(int pageIndex, int pageSize) throws Exception;
  //添加员⼯
  public void insert(Employee emp) throws Exception;
}
EmployeeDaoImpl.java实现类
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
//注⼊JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Employee queryById(int empno) throws Exception {
String sql ="select * from emp where empno = ?";
// queryForObject( xxx.class) 使⽤的基本数据类型,要求查询的结果只能⼀列
/
/  BeanPropertyRowMapper()  把⾏记录转换为⼀个java对象
return jdbcTemplate.queryForObject(sql,new Object[] {empno},new BeanPropertyRowMapper<Employee>(Employee.class));    }
@Override
public List<Employee> queryByPage(int pageIndex, int pageSize) throws Exception {
String sql ="select * from emp limit ? , ? ";
return jdbcTemplate.query(sql,
new Object[] {(pageIndex-1)*pageSize,pageSize},
new BeanPropertyRowMapper<Employee>(Employee.class) );
}
@Override
public void insert(Employee emp) throws Exception {
String sql ="INSERT INTO `emp` (`empno`,`ename`,`job`,`mgr`,`hiredate`,`sal`,`comm`,`deptno`) VALUES (?,?,?,?,?,?,?,?)";
//update() 执⾏增删改sql
jdbcTemplate.update(sql, Empno(),
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。