1. 示例代码

CustomerDao.java  ,dao接口

public interface CustomerDao {
	public void insertCustomer(Customer c);
	public void updateCustomer(Customer c);
	public List<Customer> findCustomerByName(String name);
}


CustomerDaoImpl.java 接口实现

/**
 * CustomerDaoImpl
 */
public class CustomerDaoImpl implements CustomerDao {
	private JdbcTemplate jt ;
	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}

	public List<Customer> findCustomerByName(String name) {
		String sql="select id,name,age from customers where name = ?";
		return jt.query(sql, new Object[]{name}, new RowMapper(){
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Customer c = new Customer();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setAge(rs.getInt("age"));
				return c;
			}});
	}

	/**
	 * 插入
	 */
	public void insertCustomer(Customer c) {
		String sql = "insert into customers(name,age) values(?,?)";
		jt.update(sql, new Object[]{c.getName(),c.getAge()});
	}

	public void updateCustomer(Customer c) {
		String sql = "update customers set name = ?,age = ? where id = ?";
		jt.update(sql,new Object[]{c.getName(),c.getAge(),c.getId()});
	}
}


jdbc.properties 分散配置

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root

c3p0.pool.size.max=10
c3p0.pool.size.min=2
c3p0.pool.size.ini=3
c3p0.pool.size.increment=2


dao.xml 配置文件


App.java 测试代码

public class App {

	public static void main(String[] args) throws SQLException {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"cn/itcast/spring/dao/dao.xml");
		CustomerDao dao = (CustomerDao) ac.getBean("customerDao");
		Customer c = new Customer();
		c.setName("tom");
		c.setAge(23);
		dao.insertCustomer(c);
		//
		c = new Customer();
		c.setId(1);
		c.setName("jerry");
		c.setAge(23);
		dao.updateCustomer(c);
		
		dao.findCustomerByName("tom");
	}

}

CustomerDaoSuportImpl.java 另外一种配置方法: 省略模板的配置

/**
 * CustomerDaoImpl,省略模板的配置
 */
public class CustomerDaoSuportImpl extends JdbcDaoSupport implements CustomerDao {

	public List<Customer> findCustomerByName(String name) {
		String sql="select id,name,age from customers where name = ?";
		return getJdbcTemplate().query(sql, new Object[]{name}, new RowMapper(){
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Customer c = new Customer();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setAge(rs.getInt("age"));
				return c;
			}});
	}

	/**
	 * 插入
	 */
	public void insertCustomer(Customer c) {
		String sql = "insert into customers(name,age) values(?,?)";
		getJdbcTemplate().update(sql, new Object[]{c.getName(),c.getAge()});
	}

	public void updateCustomer(Customer c) {
		String sql = "update customers set name = ?,age = ? where id = ?";
		getJdbcTemplate().update(sql,new Object[]{c.getName(),c.getAge(),c.getId()});
	}
}


AppDaoSupport.java 省略模板配置  测试代码

public class AppDaoSupport {

	public static void main(String[] args) throws SQLException {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"cn/itcast/spring/dao/dao.xml");
		CustomerDao dao = (CustomerDao) ac.getBean("customerDaoSuport");
		Customer c = new Customer();
		c.setName("tom");
		c.setAge(23);
		dao.insertCustomer(c);
	}

}



 

相关文章:

  • 2021-04-12
  • 2022-01-12
  • 2021-08-25
  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案