一、Spring数据库编程主要使用Spring JDBC模块的core包和dataSource包。
core包是JDBC的核心功能包,包括常用的JdbcTemplate类;
dataSource包是访问数据源的工具类包。
使用Spring JDBC操作数据库,需要对其进行配置
二、配置数据源
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/wm"></property>
<property name="username" value="root"></property>
<property name="password" value="1"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="storeDao" class="org.zhx.Impl.StoreDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 通知式 事务-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txpoint" expression="execution(* org.zhx.Impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpoint"></aop:advisor>
</aop:config>
</beans>
三、JdbcTemplate的使用
首先要有bean,接口和实现类
package org.zhx.bean;
public class Store {
private int id;
private String username;
private String address;
private String manager;
public Store(int id, String username) {
this.id = id;
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public Store() {
}
public Store(int id, String username, String address, String manager) {
this.id = id;
this.username = username;
this.address = address;
this.manager = manager;
}
}
package org.zhx.dao;
import org.zhx.bean.Store;
import java.util.List;
public interface IStoreDao {
public List<Store> findStore();
public int updateStore(Store s);
public int addStore(int id);
public int deleteStore(int id);
}
package org.zhx.Impl;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.zhx.bean.Store;
import org.zhx.dao.IStoreDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class StoreDaoImpl implements IStoreDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public StoreDaoImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public StoreDaoImpl() {
}
@Override
public List<Store> findStore() {
return jdbcTemplate.query("select * from store ", new RowMapper<Store>() {
@Override
public Store mapRow(ResultSet rs, int i) throws SQLException {
Store s = new Store(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
return s;
}
});
}
@Override
public int updateStore(Store s) {
return jdbcTemplate.update("update store set username = ? where id = ?",s.getUsername(),s.getId());
}
@Override
public int addStore(int id) {
return jdbcTemplate.update("insert into store values (?,?,?,?)",null,"七号仓库","古巨基帮","杰哥");
}
@Override
public int deleteStore(int id) {
return jdbcTemplate.update("delete from store where id = ?",id);
}
}
package org.zhx.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zhx.bean.Store;
import org.zhx.dao.IStoreDao;
import java.util.List;
public class Test {
public static void main(String[] args) {
// test2();
// test3();
test4();
test1();
}
private static void test4() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
IStoreDao iStoreDao = ac.getBean("storeDao", IStoreDao.class);
iStoreDao.deleteStore(13);
System.out.println("删除成功了哦!");
}
private static void test3() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
IStoreDao iStoreDao = ac.getBean("storeDao", IStoreDao.class);
iStoreDao.addStore(12);
System.out.println("添加成功了哦!");
}
private static void test2() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
IStoreDao iStoreDao = ac.getBean("storeDao", IStoreDao.class);
iStoreDao.updateStore(new Store(11,"六号仓库"));
System.out.println("更改成功了哦!");
}
private static void test1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
IStoreDao iStoreDao = ac.getBean("storeDao", IStoreDao.class);
List<Store> list = iStoreDao.findStore();
for (Store s:list) {
System.out.println(s.getId()+"\t"+s.getUsername()+"\t"+s.getManager()+"\t"+s.getAddress());
}
}
}