String sql = "update customers set name=?,address=? where id=? ";
jdbcTemplate.update(sql, "Tom","北京",64);
③ JdbcTemplate使用-BatchUpdate( )
String sql = "insert into customers(name,address,phone) values(?,?,?) ";
List
④ JdbcTemplate使用-QueryForObject( )
/**
* 获取单个列的值, 或做统计查询
* 使用 queryForObject(String sql, Class requiredType)
*/
@Test
public void testQueryObject2(){
String sql = "select name from test_user where id = ? ";
String name = jdbcTemplate.queryForObject(sql, String.class,1);
System.out.println(name);
⑥ queryForObject返回一个对象:
/**
* 从数据库中获取一条记录, 实际得到对应的一个对象
* 注意不是调用 queryForObject(String sql, Class requiredType, Object... args) 方法!
* 而需要调用 queryForObject(String sql, RowMapper rowMapper, Object... args)
*
* 1. 其中的 RowMapper 指定如何去映射结果集的行, 常用的实现类为 BeanPropertyRowMapper
* 2. 使用 SQL 中列的别名完成列名和类的属性名的映射. 例如 last_name lastName
* 3. 不支持级联属性. JdbcTemplate 到底是一个 JDBC 的小工具, 而不是 ORM 框架
*/
@Test
public void testQueryForObject(){
String sql = "select id,name,password,balance ,dept_id as \"department.id\" from test_user where id=? ";
RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
User user = jdbcTemplate.queryForObject(sql, rowMapper,1);
System.out.println(user);
}
⑦ JdbcTemplate使用-Query返回实体类集合
/**
* 查到实体类的集合
* 注意调用的不是 queryForList 方法
*/
@Test
public void testQueryForList(){
String sql = "select id,name,password,balance from test_user where id >= ? ";
RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
List users = jdbcTemplate.query(sql, rowMapper, 1);
System.out.println(users);
}
//如下返回对象集合
List queryList =jdbcTemplate.query(sql, new RowMapper() {
public TZhuanjia mapRow(ResultSet rs, int rowNum) throws SQLException {
TZhuanjia tZhuanjia = new TZhuanjia();
int id = rs.getInt("id");
tZhuanjia.setId(id);
int yiyuanId = rs.getInt("yiyuanId");
tZhuanjia.setId(yiyuanId);
int keshiId = rs.getInt("keshiId");
tZhuanjia.setId(keshiId);
int nianling = rs.getInt("nianling");
tZhuanjia.setNianling(nianling);
String xingming = rs.getString("xingming");
tZhuanjia.setXingming(xingming);
String xingbie = rs.getString("xingbie");
tZhuanjia.setXingbie(xingbie);
String shanchang = rs.getString("shanchang");
tZhuanjia.setShanchang(shanchang);
String leixing = rs.getString("leixing");
tZhuanjia.setLeixing(leixing);
String fujian = rs.getString("fujian");
tZhuanjia.setFujian(fujian);
String del = rs.getString("del");
tZhuanjia.setDel(del);
return tZhuanjia;
};
});
PS:JdbcTemplate不支持级联属性,故如下将不会得到department对象:
String sql = "select id,name,password,balance ,dept_id as
\"department.id\" from test_user where id=? ";
【3】DAO中注入JdbcTemplate
每次使用都创建一个 JdbcTemplate 的新实例, 这种做法效率很低下。
JdbcTemplate 类被设计成为线程安全的, 所以可以在 IOC 容器中声明它的单个实例, 并将这个实例注入到所有的 DAO 实例中。
XML配置:
【4】JdbcDaoSupport
Spring JDBC 框架还提供了一个 JdbcDaoSupport 类来简化 DAO 实现。
该类声明了 jdbcTemplate 属性, 它可以从 IOC 容器中注入, 或者自动从数据源中创建。
@Repository
public class DepartmentDAO extends JdbcDaoSupport {
@Autowired
public void setDataSource2(DataSource dataSource){
setDataSource(dataSource);
}
public Department getDepartment(int id){
String sql="select department_id id,department_name deptName from department where department_id=?";
RowMapper rowMapper = new BeanPropertyRowMapper(Department.class);
Department department = getJdbcTemplate().queryForObject(sql, rowMapper, id);
return department;
}
}
【5】在SpringJUnit4ClassRunner环境下测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class JdbcTest {
private ApplicationContext aContext = null;
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Resource(name="userDAO")
private UserDAO userDAO;
@Resource(name="departmentDAO")
private DepartmentDAO departmentDAO;
static{
System.out.println("加载静态方法块。。。");
}
{
aContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// jdbcTemplate = (JdbcTemplate) aContext.getBean("jdbcTemplate");
// userDAO = aContext.getBean(UserDAO.class);
// departmentDAO = aContext.getBean(DepartmentDAO.class);
System.out.println("加载方法块。。。");
}
/**
* execute insert update delete
*/
@Test
public void testUpdate(){
String sql = "update customers set name=?,address=? where id=? ";
jdbcTemplate.update(sql, "Tom","北京",64);
}
/**
* execute insert update delete
* 最后一个参数是Object[]的List类型
*/
@Test
public void testBatchUpdate(){
String sql = "insert into customers(name,address,phone) values(?,?,?) ";
List batchArgs = new ArrayList();
batchArgs.add(new Object[]{"tom1","北京","1392356824"});
batchArgs.add(new Object[]{"tom2","南京","1392356824"});
batchArgs.add(new Object[]{"tom3","东京","1392356824"});
batchArgs.add(new Object[]{"tom4","西京","1392356824"});
batchArgs.add(new Object[]{"tom5","边梁","1392356824"});
jdbcTemplate.batchUpdate(sql, batchArgs);
}
/**
* 获取单个列的值, 或做统计查询
* 使用 queryForObject(String sql, Class requiredType)
*/
@Test
public void testQueryObject2(){
String sql = "select name from test_user where id = ? ";
String name = jdbcTemplate.queryForObject(sql, String.class,1);
System.out.println(name);
}
/**
* 查到实体类的集合
* 注意调用的不是 queryForList 方法
*/
@Test
public void testQueryForList(){
String sql = "select id,name,password,balance from test_user where id >= ? ";
RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
List users = jdbcTemplate.query(sql, rowMapper, 1);
System.out.println(users);
}
/**
* 从数据库中获取一条记录, 实际得到对应的一个对象
* 注意不是调用 queryForObject(String sql, Class requiredType, Object... args) 方法!
* 而需要调用 queryForObject(String sql, RowMapper rowMapper, Object... args)
*
* 1. 其中的 RowMapper 指定如何去映射结果集的行, 常用的实现类为 BeanPropertyRowMapper
* 2. 使用 SQL 中列的别名完成列名和类的属性名的映射. 例如 last_name lastName
* 3. 不支持级联属性. JdbcTemplate 到底是一个 JDBC 的小工具, 而不是 ORM 框架
*/
@Test
public void testQueryForObject(){
String sql = "select id,name,password,balance ,dept_id as \"department.id\" from test_user where id=? ";
RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
User user = jdbcTemplate.queryForObject(sql, rowMapper,1);
System.out.println(user);
}
@Test
public void testDataSource() throws SQLException{
DataSource dataSource = aContext.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
@Test
public void testUserDAO() throws SQLException{
User user = userDAO.getUser(1);
System.out.println(user);
}
@Test
public void testDeptDAO() throws SQLException{
Department department= departmentDAO.getDepartment(1);
System.out.println(department);
}
}
什么是Thrift
The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and s
org.json.JSONException: No value for items
在JSON解析中会遇到一种错误,很常见的错误
06-21 12:19:08.714 2098-2127/com.jikexueyuan.secret I/System.out﹕ Result:{"status":1,"page":1,&