Spring操作JdbcTemplate數(shù)據(jù)庫的方法學(xué)習(xí)
Spring操作JdbcTemplate
spring 對 jdbc 做了封裝,就是 JdbcTemplate,可以讓操作數(shù)據(jù)庫更加方便。
一、準(zhǔn)備工作
1. 引入依賴
在之前的基礎(chǔ)上,再引入這些依賴。




2. 配置文件中配置數(shù)據(jù)庫連接池
外部文件 jdbc.properties:
prop.driverClass=com.mysql.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/userDb prop.username=root prop.password=123456
配置文件引入:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.pingguo.spring5"></context:component-scan>
<!--引入外部屬性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>3. 配置 JdbcTemplate 對象
注入 DataSource。
... ...
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入datasource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
... ...4. dao 中注入 JdbcTemplate 對象
創(chuàng)建 dao,在里面注入 JdbcTemplate 。
@Repository
public class BookDaoImpl implements BookDao {
// 注入 jdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
}創(chuàng)建 service 類,在里面注入 dao。
@Service
public class BookService {
// 注入dao
@Autowired
private BookDao bookDao;
}二、操作數(shù)據(jù)庫
以添加為例。
建一個很簡單的表,里面有 3 個字段。

1. 創(chuàng)建對應(yīng)實體類
創(chuàng)建數(shù)據(jù)表對應(yīng)的實體類,并且生成 3 個屬性的 get、set方法。
public class Book {
private String userId;
private String username;
private String userStatus;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
... ...2. 編寫service 和 dao
service
@Service
public class BookService {
// 注入dao
@Autowired
private BookDao bookDao;
public void addBook(Book book) {
bookDao.add(book);
}
}dao 的實現(xiàn)類。
@Repository
public class BookDaoImpl implements BookDao {
// 注入 jdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
String sql = "insert into t_book values (?, ?, ?)";
int result = jdbcTemplate.update(sql, book.getUserId(), book.getUsername(), book.getUserStatus());
System.out.println(result);
}
}使用 jdbcTemplate.update() 方法進(jìn)行添加,第一個參數(shù)是 sql,第二個不定長參數(shù),成功則返回 1。
3. 編寫測試
public class TestBook {
@Test
public void testJdbc() {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setUserId("1");
book.setUsername("ceshi");
book.setUserStatus("3");
bookService.addBook(book);
}
}運行結(jié)果:
八月 05, 2021 10:35:15 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
1
Process finished with exit code 0查看數(shù)據(jù)表

成功添加。
刪除跟修改操作跟上面類似了,不再演示。
以上就是Spring操作JdbcTemplate數(shù)據(jù)庫方法學(xué)習(xí)的詳細(xì)內(nèi)容,更多關(guān)于Spring操作JdbcTemplate的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot用JdbcTemplates操作Mysql實例代碼詳解
- Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作詳解
- SpringBoot整合JdbcTemplate的示例代碼
- Spring?Boot整合持久層之JdbcTemplate多數(shù)據(jù)源
- Spring Boot 整合持久層之JdbcTemplate
- Spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)參數(shù)
- Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理完全注解方式
- Spring JdbcTemplate實現(xiàn)添加與查詢方法詳解
相關(guān)文章
修改request的parameter的幾種方式總結(jié)
這篇文章主要介紹了修改request的parameter的幾種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
JPA @Basic單表查詢?nèi)绾螌崿F(xiàn)大字段懶加載
這篇文章主要介紹了JPA @Basic單表查詢?nèi)绾螌崿F(xiàn)大字段懶加載的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
IntelliJ?IDEA教程之clean或者install?Maven項目的操作方法
這篇文章主要介紹了IntelliJ?IDEA教程之clean或者install?Maven項目的操作方法,本文分步驟給大家介紹兩種方式講解如何調(diào)試出窗口,需要的朋友可以參考下2023-04-04

