jdbcTemplate之jdbc模板技术

时间:2022-05-05 15:43:56

1:为什么要使用jdbcTemplate?

在实际开发中使用jdbc技术太过复杂,为了减少代码冗余,操作简单

步骤一:创建实体类

package beans;

public class Book {
@Override
public String toString() {
return "Book [bid=" + bookid + ", bname=" + bname + ", bprice=" + bprice
+ "]";
}
private int bookid;
private String bname;
private int bprice;
public int getBid() {
return bookid;
}
public String getBname() {
return bname;
}
public int getBprice() {
return bprice;
}
public void setBid(int bid) {
this.bookid = bid;
}
public void setBname(String bname) {
this.bname = bname;
}
public void setBprice(int bprice) {
this.bprice = bprice;
} }

步骤二:创建接口以及实现类

package dao;

import java.util.List;

import beans.Book;

public interface IBook {
public int addbook(Book book);
public List<Book> getall();
}
package dao.impl; import java.util.List;
import java.util.Map; import org.springframework.jdbc.core.support.JdbcDaoSupport; import util.MyRowMapped;
import beans.Book;
import dao.IBook; public class IBookimpl extends JdbcDaoSupport implements IBook { @Override
public int addbook(Book book) {
String sql="insert into book(bid,bname,bprice) values(?,?,?)";
int update = this.getJdbcTemplate().update(sql,book.getBid(),book.getBname(),book.getBprice());
return update;
} @Override
public List<Book> getall() {
String sql="select * from book";
List<Book> query = this.getJdbcTemplate().query(sql,new MyRowMapped());
return query;
} }

*如果要使用查询功能,需要调用jdbcTemplate的query方法,query方法的重载方法需要RowMapped类型的参数,不过RowMapped是接口类型。

所以要自己手动创建一个RowMapped接口的实现类。并重写其方法~!

package util;

import java.sql.ResultSet;
import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import beans.Book; public class MyRowMapped implements RowMapper<Book>{ //rs:單條記錄
@Override
public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
Book book=new Book();
book.setBid(rs.getInt("bid"));
book.setBname(rs.getString("bname"));
book.setBprice(rs.getInt("bprice"));
return book;
}
}

这里的resultSet对象代表单条记录,rowNum代表第几行的数据。所以不需要使用while循环来迭代。

步骤三:数据源的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<bean id="bookDao" class="dao.impl.IBookimpl">
<property name="dataSource" ref="DataSource"></property>
</bean>
<bean id="serviceDao" class="service.impl.IBookServiceimpl">
<property name="dao" ref="bookDao"></property>
</bean>
<!-- 配置數據源 -->
<!-- 1.spring內置 -->
<!-- <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="Y2162"></property>
<property name="password" value="1"></property>
</bean> -->
<!-- dbcp方式配置 需要导入dbcp的包和pool的包 -->
<!-- <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="Y2162"></property>
<property name="password" value="1"></property>
</bean> -->
<!-- c3p0方式配置 需要导入c3p0的包 -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 註冊jdbc屬性 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> <context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 註冊jdbctemplate -->
<bean id="myTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="DataSource"></property>
</bean>
</beans>

步骤四:测试类代码

package test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import service.IBookService;
import beans.Book; public class MyTest {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
/*IBookService bean = (IBookService)ctx.getBean("serviceDao");
Book book=new Book();
book.setBid(3);
book.setBname("深入spring");
book.setBprice(100);
System.out.println(bean.addbook(book));*/
IBookService bean = (IBookService)ctx.getBean("serviceDao");
List<Book> getall = bean.getall();
for (Book book : getall) {
System.out.println(book);
}
}
}