JdbcTemplate代码的部分使用

阅读更多
package com.cm.main.dao.impl;

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

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.cm.main.dao.AbstractDao;
import com.cm.main.dao.TestJdbcTemplateDao;
import com.cm.main.bo.User;

public class TestJdbcTemplateDaoImpl extends AbstractDao implements
		TestJdbcTemplateDao {

	public void updateDescription(int id, String description) {
		JdbcTemplate jt = new JdbcTemplate(getDataSource());
		jt.update("update oa_test_1 set description = ? where id = ?", new Object[] {
				description, new Integer(id) });
	}

	public int getCount() {
		JdbcTemplate jt = new JdbcTemplate(getDataSource());
		int count = jt.queryForInt("select count(*) from oa_test_1");
		return count;
	}

	public String getDescription() {
		JdbcTemplate jt = new JdbcTemplate(getDataSource());
		String name = (String) jt.queryForObject(
				"select description from oa_test_1 where id=1", String.class);
		return name;
	}

	public List getTestList(int id) {
		String sql = "select id, description from oa_test_1 where id = ?";
		RowMapper mapper = new RowMapper() {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				User user = new User();
				user.setUserid(new Integer(rs.getInt("id")));
				user.setUsername(rs.getString("description"));
				return user;
			}
		};
		JdbcTemplate jt = new JdbcTemplate(this.getDataSource());
		return jt.query(sql, new Object[] { new Integer(id) }, mapper);
	}
}



package com.cm.main.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import com.cm.main.bo.User;
import com.cm.main.dao.AbstractDao;
import com.cm.main.dao.UserDao;

public class UserDaoImpl extends AbstractDao implements UserDao,
		InitializingBean {

	private UserInsert insert;

	private UserUpdate update;

	private UserDelete delete;

	private UserMappingQuery query;
	
	public void afterPropertiesSet() {
		insert = new UserInsert(getDataSource());
		update = new UserUpdate(getDataSource());
		delete = new UserDelete(getDataSource());
		query = new UserMappingQuery(getDataSource());
	}
	private class UserMappingQuery extends MappingSqlQuery {

		public UserMappingQuery(DataSource ds) {
			super(ds,
					"select id,description from oa_test_1 where description=?");
			super.declareParameter(new SqlParameter("description",
					Types.VARCHAR));
			compile();
		}

		public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
			User user = new User();
			user.setUserid(new Integer(rs.getInt("id")));
			user.setUsername(rs.getString("description"));
			return user;
		}
	}
	public class UserInsert extends SqlUpdate {
		public UserInsert(DataSource ds) {
			setDataSource(ds);
			setSql("insert into oa_test_1(description) values(?)");
			declareParameter(new SqlParameter(Types.VARCHAR));
			compile();
		}

		public int insertUser(String description) {
			Object[] params = new Object[] { new String(description) };
			return update(params);
		}
	}

	public class UserUpdate extends SqlUpdate {
		public UserUpdate(DataSource ds) {
			setDataSource(ds);
			setSql("update oa_test_1 set description=? where id=?");
			declareParameter(new SqlParameter(Types.VARCHAR));
			declareParameter(new SqlParameter(Types.INTEGER));
			compile();
		}

		public int updateUser(int id, String description) {
			Object[] params = new Object[] { new String(description),
					new Integer(id) };
			return update(params);
		}
	}

	public class UserDelete extends SqlUpdate {
		public UserDelete(DataSource ds) {
			setDataSource(ds);
			setSql("delete from oa_test_1 where id=?");
			declareParameter(new SqlParameter(Types.INTEGER));
			compile();
		}

		public int deleteUser(int id) {
			Object[] params = new Object[] { new Integer(id) };
			return update(params);
		}
	}

	public void insertTest() {
		insert.insertUser("jdbc template");
	}

	public void updateTest() {
		update.updateUser(218, "updated by jdbc template");
	}

	public void deleteTest() {
		delete.deleteUser(217);
	}

	public List getUserList(String description) {
		Object[] parms = new Object[1];
		parms[0] = description;
		return query.execute(parms);
	}	
	
}

你可能感兴趣的:(SQL,Java,JDBC,DAO)