首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

springTEMPELE 保藏的

2013-04-21 
springTEMPELE 收藏的本文收藏自 http://my.oschina.net/jolphin/blog/61693package com.team.daoimport

springTEMPELE 收藏的

本文收藏自 http://my.oschina.net/jolphin/blog/61693

package com.team.dao;import java.util.List;import java.util.Map;import org.springframework.jdbc.core.JdbcTemplate;import com.team.util.PageHelper;public interface Jdbc {// JdbcTemplatepublic JdbcTemplate getJdbcTemplate();// 更新public int update(String sql, Object... args);// 统计public long stat(String sql, Object... args);// 查找public <T> T find(String sql, Class<T> clazz, Object... args);// 查询public <T> List<T> query(String sql, Class<T> clazz, Object... args);// 分页public <T> PageHelper<T> query(String sql, long count, Class<T> clazz, Object... args);// 命名更新public <T> int updateNamed(String namedSql, T bean);// 批量命名更新public <T> int[] updateNamed(String namedSql, List<T> beans);// 命名更新public int updateNamedMap(String namedSql, Map<String, Object> paramMap);// 批量命名更新public int[] updateNamedMap(String namedSql, List<Map<String, Object>> paramMaps);}

?

package com.team.dao;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.annotation.Resource;import org.springframework.dao.EmptyResultDataAccessException;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.SqlParameter;import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;import org.springframework.jdbc.core.namedparam.NamedParameterUtils;import org.springframework.jdbc.core.namedparam.ParsedSql;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.team.util.PageHelper;@Repository@Transactionalpublic class JdbcSupport implements Jdbc {@Resourceprotected JdbcTemplate jdbcTemplate;@Overridepublic JdbcTemplate getJdbcTemplate() {return jdbcTemplate;}@Overridepublic int update(String sql, Object... args) {return jdbcTemplate.update(sql, args);}@Override@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)public long stat(String sql, Object... args) {return jdbcTemplate.queryForLong(sql, args);}@Override@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)public <T> T find(String sql, Class<T> clazz, Object... args) {try {return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clazz), args);} catch (EmptyResultDataAccessException e) {return null;}}@Override@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)public <T> List<T> query(String sql, Class<T> clazz, Object... args) {return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz), args);}@Override@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)public <T> PageHelper<T> query(String sql, long count, Class<T> clazz, Object... args) {int page = 0;int size = 0;if (args != null && args.length >= 2) {page = (Integer)args[args.length - 2];size = (Integer)args[args.length - 1];}page = (page < 1) ? 1 : page;size = (size < 1) ? 1 : size;int from = (page - 1) * size;args[args.length - 2] = from;args[args.length - 1] = size;List<T> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(clazz), args);return new PageHelper<T>(list, count, page, size);}@Overridepublic <T> int updateNamed(String namedSql, T bean) {String sql = NamedParameterUtils.parseSqlStatementIntoString(namedSql);ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(namedSql);BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(bean);List<SqlParameter> params = NamedParameterUtils.buildSqlParameterList(parsedSql, source);Object[] args = NamedParameterUtils.buildValueArray(parsedSql, source, params);return jdbcTemplate.update(sql, args);}@Overridepublic <T> int[] updateNamed(String namedSql, List<T> beans) {String sql = NamedParameterUtils.parseSqlStatementIntoString(namedSql);ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(namedSql);List<Object[]> batchArgs = new ArrayList<Object[]>();for(T bean : beans){BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(bean);List<SqlParameter> params = NamedParameterUtils.buildSqlParameterList(parsedSql, source);Object[] args = NamedParameterUtils.buildValueArray(parsedSql, source, params);batchArgs.add(args);}return jdbcTemplate.batchUpdate(sql, batchArgs);}@Overridepublic int updateNamedMap(String namedSql, Map<String, Object> paramMap) {String sql = NamedParameterUtils.parseSqlStatementIntoString(namedSql);Object[] args = NamedParameterUtils.buildValueArray(namedSql, paramMap);return jdbcTemplate.update(sql, args);}@Overridepublic int[] updateNamedMap(String namedSql, List<Map<String, Object>> paramMaps) {String sql = NamedParameterUtils.parseSqlStatementIntoString(namedSql);List<Object[]> batchArgs = new ArrayList<Object[]>();for(Map<String, Object> paramMap : paramMaps){Object[] args = NamedParameterUtils.buildValueArray(namedSql, paramMap);batchArgs.add(args);}return jdbcTemplate.batchUpdate(sql, batchArgs);}}

?

p
package junit.test.service;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.team.bean.User;import com.team.dao.Jdbc;import com.team.util.PageHelper;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class JdbcTest {@Resourceprivate Jdbc jdbc;@Testpublic void update(){jdbc.update("delete from user where id = ?", 110);}@Testpublic void updateNamed(){User user = new User();user.setUsername("username");user.setRealname("realname");user.setPassword("password");jdbc.updateNamed("insert into user(username, realname, password, memo) "+         "values(:username, :realname, :password, :memo)", user);}@Test@SuppressWarnings("serial")public void batchUpdate(){List<User> list = new ArrayList<User>(){{add(new User(1, null, null, "changePassword", null));add(new User(2, null, null, "changePassword", null));add(new User(3, null, null, "changePassword", null));}};jdbc.updateNamed("update user set password = :password where id = :id", list);}@Test@SuppressWarnings("serial")public void updateMap(){Map<String, Object> map = new HashMap<String, Object>(){{put("password", "changePawword");put("id", 110);}};jdbc.updateNamedMap("update user set password = :password where id = :id", map);}@Test@SuppressWarnings("serial")public void batchUpdateMap(){List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();list.add(new HashMap<String, Object>() {{put("password", "changePawword");put("id", 110);}});list.add(new HashMap<String, Object>() {{put("password", "changePawword");put("id", 109);}});list.add(new HashMap<String, Object>() {{put("password", "changePawword");put("id", 108);}});jdbc.updateNamedMap("update user set password = :password where id = :id", list);}@Testpublic void query(){User user = jdbc.find("select * from user where id = ?", User.class, 110);System.out.println(user);List<User> list = jdbc.query("select * from user", User.class);for(User obj : list){System.out.println(obj);}long count = jdbc.stat("select count(*) from user");PageHelper<User> result = jdbc.query("select * from user limit ?, ?", count, User.class, 1, 10);for(User oo : result.getList()){System.out.println(oo);}}}
package com.team.util;import java.util.List;//分页类public class PageHelper<T> {private List<T> list; //结果集private long record; //总记录private long count; //总页数private int page;  //当前页private int size; //每页记录public PageHelper(List<T> list, long record, int page, int size) {this.list = list;this.page = page;this.size = size;this.record = record;this.count = (record+size-1)/size;}public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}public long getRecord() {return record;}public void setRecord(long record) {this.record = record;}public long getCount() {return count;}public void setCount(long count) {this.count = count;}public int getPage() {return page;}public void setPage(int page) {this.page = page;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}}
?

?

?

本文收藏自 http://my.oschina.net/jolphin/blog/61693

热点排行