JdbcTemplate template
时间: 2023-10-06 15:12:59 浏览: 93
JdbcTemplate是Spring框架中的一个类,全限定命名为org.springframework.jdbc.core.JdbcTemplate。它是对JDBC进行封装,用于方便地进行数据库操作。为了使用JdbcTemplate,还需要引入相关的jar包,并且需要一个包含了事务和异常控制的环境。
JdbcTemplate提供了多个方法,其中包括execute方法,可以用来执行SQL语句。
相关问题
给下面代码添加注释 public class TestJdbcTemplate { JdbcTemplate template; @Before public void init() throws Exception { template= new JdbcTemplate(DruidUtils.getDataSource()); } @Test public void testInsert() throws Exception { template.update("insert into account(name,balance) values(?,?)","王麻子",8888); } @Test public void testUpdate() throws Exception { template.update("update account set balance=? where id=?",4444,4); } @Test public void testDelete() throws Exception { template.update("delete from account where id=?",4); } @Test public void testFindAll() throws Exception { List<Emp1> emps = template.query("select * from emp", new BeanPropertyRowMapper<Emp1>(Emp1.class)); for (Emp1 e :emps) { System.out.println(e); } } @Test public void testFindCount(){ Integer count = template.queryForObject("select count(1) from emp", Integer.class); System.out.println(count); } @Test public void testFindByPage() throws Exception { List<Emp1> emps = template.query("select * from emp limit ?,?", new BeanPropertyRowMapper<Emp1>(Emp1.class),10,5); for (Emp1 e :emps) { System.out.println(e); } } }
// 导入JdbcTemplate类
public class TestJdbcTemplate {
// 声明一个JdbcTemplate对象
JdbcTemplate template;
// 在执行每个测试方法前初始化JdbcTemplate对象
@Before
public void init() throws Exception {
// 获取数据源并创建JdbcTemplate对象
template= new JdbcTemplate(DruidUtils.getDataSource());
}
// 测试插入操作
@Test
public void testInsert() throws Exception {
// 使用JdbcTemplate的update方法插入数据
template.update("insert into account(name,balance) values(?,?)","王麻子",8888);
}
// 测试更新操作
@Test
public void testUpdate() throws Exception {
// 使用JdbcTemplate的update方法更新数据
template.update("update account set balance=? where id=?",4444,4);
}
// 测试删除操作
@Test
public void testDelete() throws Exception {
// 使用JdbcTemplate的update方法删除数据
template.update("delete from account where id=?",4);
}
// 测试查询所有数据操作
@Test
public void testFindAll() throws Exception {
// 使用JdbcTemplate的query方法查询数据,并将结果转换为Emp1类的List
List<Emp1> emps = template.query("select * from emp", new BeanPropertyRowMapper<Emp1>(Emp1.class));
// 遍历List并输出每个Emp1对象
for (Emp1 e :emps) {
System.out.println(e);
}
}
// 测试查询数据条数操作
@Test
public void testFindCount(){
// 使用JdbcTemplate的queryForObject方法查询数据条数
Integer count = template.queryForObject("select count(1) from emp", Integer.class);
// 输出查询结果
System.out.println(count);
}
// 测试分页查询操作
@Test
public void testFindByPage() throws Exception {
// 使用JdbcTemplate的query方法分页查询数据,并将结果转换为Emp1类的List
List<Emp1> emps = template.query("select * from emp limit ?,?", new BeanPropertyRowMapper<Emp1>(Emp1.class),10,5);
// 遍历List并输出每个Emp1对象
for (Emp1 e :emps) {
System.out.println(e);
}
}
}
jdbctemplate
Spring Boot 使用 JDBC Template 的方法如下:
1. 在 pom.xml 中添加 JDBC 的依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
```
2. 在应用程序的配置文件中配置数据源信息:
```
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: password
driver-class-name: com.mysql.jdbc.Driver
```
3. 在应用程序中创建 JDBC Template 对象:
```
@Autowired
private JdbcTemplate jdbcTemplate;
```
4. 使用 JDBC Template 进行数据库操作, 如执行查询:
```
List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM users");
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)