spring jdbcTemplate简单使用

项目结构


1、使用spring配置jdbcTemplate,首先要配置applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@172.0.0.1:1521:orcl1</value></property>
        <property name="username"><value>xxxxxx</value></property>
        <property name="password"><value>xxxxxx</value></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource"><ref bean="dataSource" /></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource"><ref bean="dataSource" /></property>
    </bean>
	
	<bean id="ordersDao" class="dao.OrdersDao" >
	    <property name="jdbcTemplate"><ref bean="jdbcTemplate" /></property>
	</bean>
			
</beans>

2、随便写个实体类:

package entity;

public class Orders implements Serializable {
    private static final long serialVersionUID = -2811014017129339092L;
	
 	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

3、dao中新建一个dao类

package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import entity.Orders;

@Repository
public class OrdersDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	//setter方法注入
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	/**
	 * 查询
	 * @param status
	 * @return
	 */
	public List<Orders> getOrders(int id){
		return jdbcTemplate.query("select * from orders where id> ?",
				new Object[] { id }, new OrdersMapper());
	}

	/**
	 * 插入
	 * @param 
	 * @return
	 */
	public int insert(int id) {
		return jdbcTemplate.update(
			" insert into orders select * from orders_other where id=? ",
			new Object[] { id });
	}

	/**
	 * 更新
	 * @param or 
	 * @param 
	 * @return
	 */
	public int update(Orders or) {
		return jdbcTemplate.update(
			" update orders set name=? where id=? ",
			new Object[] { or.getName(), or.getId() });
	}
	/**
	 * 删除
	 * @param or  
	 * @return
	 */
	public int delete(Orders or) {
		return jdbcTemplate.update(" delete from orders where id = ? ",
				new Object[] { or.getId() });
	}
	private  class OrdersMapper implements RowMapper {
		public Orders mapRow(ResultSet rs, int rowNum) throws SQLException {
			Orders order = new Orders();
			order.setId(rs.getLong("ID"));
			//.......
			return order;
		}
	}

	
}

4、写个测试类

package test;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.OrdersDao;
import entity.Orders;

/**
 * @author Administrator
 *
 */
public class TestOrders {

	private static Logger log = Logger.getLogger(TestOrdersAdd.class);
	
	@Test
	public void test(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		OrdersDao ordersDao = (OrdersDao) ac.getBean("ordersDao");
		List<Orders> orders = new ArrayList<Orders>();
		
		orders = ordersDao.getOrders(2);
		log.info("总记录数==="+orders.size());
	}
	
	
	public static void main(String[] args) {

		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		OrdersDao ordersDao = (OrdersDao) ac.getBean("ordersDao");
		List<Orders> orders = new ArrayList<Orders>();
		
		orders = ordersDao.getOrders(2);
		log.info("总记录数==="+orders.size());

	}

}
5、可以增加日志功能,新建个 log4j.properties文件

log4j.rootCategory=WARN, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


log4j.logger.test.TestOrdersAdd=DEBUG, TestOrdersAdd
log4j.appender.TestOrdersAdd=org.apache.log4j.DailyRollingFileAppender
log4j.appender.TestOrdersAdd.File=C:/Users/Administrator/Desktop/log/TestOrdersAdd.log
log4j.appender.TestOrdersAdd.layout=org.apache.log4j.PatternLayout
log4j.appender.TestOrdersAdd.layout.ConversionPattern=%c{1}:%5L - %m%n





你可能感兴趣的:(spring jdbcTemplate简单使用)