标签:
使用Spring整合
@Resource
JdbcTemplate tm;
/**
*
* @Title: save
* @Description: 批量保存,批量删除与此方法类似
* @author
* @param
* @return void 返回类型
* @throws
*/
@Test
public void save() {
final List<Student> list = new ArrayList<Student>();
Student s = new Student();
s.setName("小明1212123");
Student s2 = new Student();
s2.setName("小明123fdfd");
Student s3 = new Student();
s3.setName("小明123adsaa");
list.add(s);
list.add(s2);
list.add(s3);
String sql = "insert into student(name) values (?)";
tm.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, list.get(i).getName());
}
@Override
public int getBatchSize() {
return list.size();
}
});
}
/**
*
* @Title: find
* @Description: 查询结果返回实体list 第一种方式
* @author
* @param
* @return void 返回类型
* @throws
*/
@SuppressWarnings("unchecked")
@Test
public void find() {
String sql = "select * from student where name=?";
List<Student> list = tm.query(sql, new String[] { "小明" }, new BeanPropertyRowMapper(Student.class));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
}
/**
*
* @Title: findEntityList
* @Description: 查询结果返回实体list 第二种方式,此处的实体类Student需要实现RowMapper接口
* @author
* @param
* @return void 返回类型
* @throws
*/
@Test
public void findEntityList() {
test_trans
String sql = "select id,name from student";
List<Student> list = tm.query(sql, new Student());
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
System.out.println(s.getId() + ">>>>>>>" + s.getName());
}
}
/**
*
* @Title: findEntityListWhere
* @Description: 通过条件查询数据
* @author
* @param
* @return void 返回类型
* @throws
*/
@Test
public void findEntityListWhere() {
String sql = "select id,name from student where id=?";
List<Student> list = tm.query(sql, new Object[] { 1 }, new Student());
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
System.out.println(s.getId() + ">>>>>>>" + s.getName());
}
}
/**
*
* @Title: findEntity
* @Description: 查询结果返回单个实体
* @author
* @param
* @return void 返回类型
* @throws
*/
@Test
public void findEntity() {
String sql = "select id,name from student where id=1";
Student s = tm.queryForObject(sql, new Student());
System.out.println(s.getId() + ">>>>>>>" + s.getName());
}
/**
*
* @Title: findMap
* @Description: 查询结果返回map
* @author
* @param
* @return void 返回类型
* @throws
*/
@Test
public void findMap() {
String sql = "select * from student";
List<Map<String, Object>> list = tm.queryForList(sql);
for (int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
System.out.println(map.get("id") + ">>>>>>" + map.get("name"));
}
}
/**
*
* @Title: count
* @Description: 统计数量
* @author
* @param
* @return void 返回类型
* @throws
*/
@Test
public void count() {
String sql = "select count(*) as ‘count‘ from student";
Map<String, Object> map = tm.queryForMap(sql);
System.out.println("共找到>>>>>>>>>>:" + map.get("count") + "条数据");
}
实体Student类
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.persistence.Transient;
import org.springframework.jdbc.core.RowMapper;
public class Student implements RowMapper<Student> {
private Integer id;
private String name;
private String nothing;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Transient
public String getNothing() {
return this.nothing;
}
public void setNothing(String nothing) {
this.nothing = nothing;
}
@Override
public Student mapRow(ResultSet rs, int num) throws SQLException {
// TODO Auto-generated method stub
Student s = new Student();
try {
s.setId(rs.getInt("id"));
s.setName(rs.getString("name"));
s.setNothing(rs.getString("nothing"));
} catch (Exception e) {
// TODO: handle exception
}
return s;
}
}标签:
原文地址:http://my.oschina.net/u/2292000/blog/395255