JdbcTemplate 是Spring框架中帮助我们对数据库进行操作的工具类,它能简化我们对数据库的操作,降低代码耦合度,提高代码可维护性。 对本文操作不理解的话可以阅读我博客中前几篇入门教程 Java Spring框架入门第一个小例子教程
1.首先我们需要准备我们使用的jar包,并Add to BuildPath (下载链接:https://pan.baidu.com/s/1TWlq2SeLEmiOT6zHhvbIYA)

注:如果你的数据库不是mysql而是Oracle的话,需要将其中的mysql-connector-java-5.0.8-bin.jar 换成ojdbc6.jar(百度即可下载),并且下文的配置文件也要一并修改,具体操作请百度或私聊我。
项目目录如下:

2.首先我们配置我们本地配置文件DataSource.properties。(值得注意的是①端口要根据本地mysql的端口配置,默认使用端口为3306 ②3306后面斜线后面的 mytest 是你创建的 用于使用本项目中的数据库名 ③ 用户名和密码根据本地mysql配置而设置)
#DataSource.properities
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/my_test?characterEncoding=UTF-8&useOldAliasMetadataBehavior=true
username=root
password=123456
3.我们需要配置数据库,在my_test数据库下创建了一张表phone,字段属性如下,工具可使用SqlYog或Navicat

4.配置xml文件application.xml(①地址务必不要填错resource/DataSource.properties ②③的路径需要与你本地项目的路径相一致)
<?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.ly.demo"></context:component-scan>
<context:property-placeholder location="resource/DataSource.properties"></context:property-placeholder>
<!-- 创建DataSource对象 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 创建JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<bean id="phoneDaoImpl" class="com.ly.demo.PhoneDaoImpl"></bean>
<!-- 创建BookService对象 -->
<bean id="phoneService" class="com.ly.demo.PhoneService"></bean>
</beans>
5.根据数据库字段创建bean对象Phone。Phone中的所有字段需要和数据库的表中字段名相同
package com.ly.demo;
public class Phone {
private String name;
private String brand;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public Phone() {
super();
}
public Phone(String name, String brand, String price) {
super();
this.name = name;
this.brand = brand;
this.price = price;
}
@Override
public String toString() {
return "Phone [name=" + name + ", brand=" + brand + ", price=" + price + "]";
}
}
6.配置持久层Dao接口PhoneDao
package com.ly.demo;
import java.util.List;
public interface PhoneDao {
/**
* 添加手机
* @param name
* @param price
* @return int
*/
public int addPhone(String name , String brand,String price);
/**
* 返回所有Phone信息
* @return List
*/
public List<Phone> getAllPhone();
}
7.配置Dao对象实现类PhoneDaoImpl 注:此处及下文使用的@Resource为Spring框架的注解
package com.ly.demo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class PhoneDaoImpl implements PhoneDao{
// jdbc模板
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
public int addPhone(String name , String brand,String price) {
System.out.println("add!");
return jdbcTemplate.update("insert into Phone value(?,?,?)",name,brand,price);
}
public List<Phone> getAllPhone() {
return jdbcTemplate.query("select * from Phone", new PhoneRowMapper());
}
private class PhoneRowMapper implements RowMapper<Phone>{
@Override
public Phone mapRow(ResultSet re, int arg1) throws SQLException {
return new Phone(re.getString("name"),re.getString("brand"),re.getString("price"));
}
}
}
8.配置服务类PhoneService
package com.ly.demo;
import java.util.List;
import javax.annotation.Resource;
public class PhoneService {
@Resource(name="phoneDaoImpl")
private PhoneDaoImpl phoneDaoImpl;
public List<Phone> getAll() {
return phoneDaoImpl.getAllPhone();
}
public int add(String name , String brand,String price) {
return phoneDaoImpl.addPhone(name,brand, price);
}
}
9.配置Test测试类并运行
package com.ly.demo;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("resource/application.xml");
//检测数据源配置是否成功
DataSource ds = (DataSource) context.getBean("dataSource");
System.out.println(ds);
//检测JdbcTemplate配置是否成功
JdbcTemplate jt = (JdbcTemplate) context.getBean("jdbcTemplate");
System.out.println(jt);
//测试服务类运行
PhoneService phoneService = (PhoneService) context.getBean("phoneService");
phoneService.add("mate9","Huawei","4000" );
phoneService.add("hongmi","xiaomi","2000");
System.out.println(phoneService.getAll());
}
}
10.运行成功结果如下图

欢迎大家有任何问题在下方评论
本文介绍了如何在Spring框架中使用JdbcTemplate进行数据库操作,包括配置DataSource.properties、数据库连接、创建表,以及配置XML文件和相关Bean对象,提供了一个简单的入门教程。

3205

被折叠的 条评论
为什么被折叠?



