[] http://blog.csdn.net/u011179993からの転送:より転載
データベースの構造
クラス1.サポート
SqlParameterSourceプロファイルは
SqlParameterSourceを達成するために使用することができますが、パラメータという名前のデフォルトの実装のための設定値として実装されています。
MapSqlParameterSourceの実装は非常に単純ですが、java.util.Mapをカプセル化します。
BeanPropertySqlParameterSourceはJavaBeanのオブジェクトという名前のパラメータ値がJavaBeanオブジェクトのプロパティによって決定されるカプセル化します。
プレースホルダを使用するために使用される空SqlParameterSourceをEmptySqlParameterSource
約RowMapperの
このインターフェイスのSQLクエリの結果とオブジェクトの間で切り替えるために、あなたがあなた自身を達成することができ、あなたはまた、主要業績カテゴリを達成するためにシステムを使用することができます:
SingleColumnRowMapper、SQL結果は、等リストの<string>、一覧<整数>、文字列、整数などのデータの単一列であります
BeanPropertyRowMapper、SQL結果が一覧<XxxVO>、XxxVOにオブジェクトに一致します
updateXXXメソッドを使用して2挿入/変更/削除、データ、
引数として地図を使用した
API:int型の更新(文字列のSQL、地図<文字列、?> ParamMap)
例:
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", UUID.randomUUID().toString());
paramMap.put("name", "小明");
paramMap.put("age", 33);
paramMap.put("homeAddress", "乐山");
paramMap.put("birthday", new Date());
template.update(
"insert into student(id,name,age,home_address,birthday) values (:id,:name,:age,:homeAddress,:birthday)",
paramMap
);
引数としてBeanPropertySqlParameterSourceの使用
API: int update(String sql, SqlParameterSource paramSource)
引数としてBeanPropertySqlParameterSourceの使用
public class StudentDTO{
private String id;
private String name;
private String homeAddress;
//getter,setter
}
StudentDTO dto=new StudentDTO();//这个DTO为传入数据
dto.setId(UUID.randomUUID().toString());
dto.setName("小红");
dto.setHomeAddress("成都");
//------------------------------
template.update("insert into student(id,name,home_address) values (:id,:name,:homeAddress)",
new BeanPropertySqlParameterSource(dto));
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", UUID.randomUUID().toString());
paramMap.put("name", "小明");
paramMap.put("homeAddress", "乐山");
//---------------
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(paramMap);
3.クエリ
を返す、単一の独立したデータ
API:公共<T> T queryForObject (文字列のSQL、地図<文字列、?> ParamMap、クラス<T> requiredType)
API:公共<T> T queryForObject(文字列のSQL、SqlParameterSource paramSource、クラス<T> requiredType)
例(使用EmptySqlParameterSourceに注意してください):
Integer count = template.queryForObject(
"select count(*) from student", new HashMap<>(), Integer.class);
String name = template.queryForObject( "select name from student where home_address limit 1 ", EmptySqlParameterSource.INSTANCE, String.class);