持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情
Spring中的JdbcTemplate
JdbcTemplate简介
Spring提供的jdbcTemplate是一个小型的持久化层框架,可以简化jdbc代码。
JdbcTemplate使用步骤
- 导入jar包
//重要的是导入该jar包。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!--导入junit4.12-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--导入druid的jar包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--导入mysql的jar包,注意版本-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
-
编写配置文件
-
db.properties:设置连接数据库属性
-
applicationContext.xml:spring配置文件
- 加载外部属性文件【db.properties】
- 装配数据源【DruidDataSources】
- 装配持久化层框架【JdbcTemplate】
-
示例代码
db.properties
db.driver=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8 db.username=root db.password=rootapplicationContext_JdbcTemplate.xml
<!-- 装配外部属性文件--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 装配数据源--> <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${db.driver}"></property> <property name="url" value="${db.url}"></property> <property name="password" value="${db.password}"></property> <property name="username" value="${db.username}"></property> </bean> <!-- 装配持久化层框架--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="druidDataSource"></property> </bean>
装配自己内部的bean可以使用注解,但是第三方的bean要使用xml去装配,比如jdbcTemplate。因为装配类需要在类上面去写注解。
-
-
使用核心类库(API调用)
JdbcTemplate常用API
JdbcTemplate默认:自动提交事务,不像Mybatis一样手动提交事务。
-
jdbcTemplate.update(String sql,Object... args):通用的增删改方法
-
jdbcTemplate.batchUpdate(String sql,List<Object[]> args):通用批处理增删改方法
-
jdbcTemplate.queryForObject(String sql,Class clazz,Object... args):查询单个数值
- String sql = "select count(1) from tbl_xxx";
-
jdbcTemplate.queryForObject(String sql,RowMapper rm,Object... args):查询单个对象
- String sql = "select col1,col2... from tbl_xxx";
-
jdbcTemplate.query(String sql,RowMapper rm,Obejct... args):查询多个对象
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext_Jdbc.xml");
JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
String sql = "insert into tbl_employee(last_name,email,salary,dept_id) values(?,?,?,?)";
List<Object[]> list = new ArrayList<>();
list.add(new Object[]{"qq0","qq.com",123,123});
list.add(new Object[]{"qq1","qq.com",123,123});
list.add(new Object[]{"qq2","qq.com",123,123});
list.add(new Object[]{"qq3","qq.com",123,123});
//演示批量添加操作。
jdbcTemplate.batchUpdate(sql,list);
//查询单个数值
String sql = "select count(1) from tbl_employee";
Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println("integer = " + integer);
//查询单个对象
String sql = "select id,last_name,email,salary from tbl_employee where id = ?";
//创建RowMapper
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);
Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 1);
System.out.println("employee = " + employee);
//查询多个对象
String sql = "select id,last_name,email,salary from tbl_employee";
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);
List<Employee> employeeList = jdbcTemplate.query(sql, rowMapper);
使用JdbcTemplate搭建Service&Dao层
-
Service层依赖Dao层
-
Dao层依赖JdbcTemplate
示例代码:
//text: /** * @Author 不知名网友鑫 * @Date 2022/10/2 **/ @ContextConfiguration(locations = "classpath:applicationContext_Jdbc.xml") @RunWith(SpringJUnit4ClassRunner.class) public class TestDaoService { @Autowired DeptService deptService; @Test public void test(){ List<Dept> depts = deptService.selectDeptS(); System.out.println("depts = " + depts); } } //DeptService /** * @Author 不知名网友鑫 * @Date 2022/10/2 **/ @Service("deptService") public class DeptServiceImpl implements DeptService{ @Autowired DeptDao deptDao; @Override public List<Dept> selectDeptS() { List<Dept> depts = deptDao.selectDept(); return depts; } } //DeptDao /** * @Author 不知名网友鑫 * @Date 2022/10/2 **/ @Repository("deptDao") public class DeptDaoImpl implements DeptDao{ @Autowired JdbcTemplate jdbcTemplate; @Override public List<Dept> selectDept() { String sql = "select dept_id,dept_name from tbl_dept"; RowMapper<Dept> rowMapper = new BeanPropertyRowMapper<>(Dept.class); List<Dept> query = jdbcTemplate.query(sql, rowMapper); return query; } }\