1.JdbcTemplate
在Spring中,JdbcTemplate是最常被使用的类,它为用户程序提供了许多便利的数据库操作方法,比如查询、更新等。JdbcTemplate是core包的核心类,它替我们完成了资源的创建及释放工作,从而简化了对JDBC的使用。
1)执行SQL语句
一旦获得一个DataSource和一个JdbcTemplate,我们就可以使用JdbcTemplate提供的丰富功能实现我们的操作。下面创建一张表:
public void execute(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.[color=red]execute[/color]("create table username(id integer,username varchar(20),password varchar(20),email varchar(50))");
}
2)执行更新语句
可以使用update()来执行一个更新的SQL语句,其中的问号表示的参数使用数组表示,如下所示:
public void updateUsername(int id,String name){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.update("update mytable set name=?where id=?",[color=red]new [/color][color=red]Object[]{name,new Long(id)});[/color]
}
execute和update方法之间的区别是:update方法返回的是受影响的记录数目的一个计数,并且如果传入一个参数的话,使用的是Java.sql.PreparedStatement;而execute方法总是使用java.sql.Statement,不接受参数,而且它不返回受影响记录的计数,更适合创建和丢弃表的语句,而update方法更适合于插入、更新和删除操作,这是需要注意的。
3)执行单表查询
比如返回一个汇总(count)结果或者从返回行结果中取得指定列的值,我们使用queryForInt()、queryForLong()或者queryForObject()方法。queryForObject方法用来将返回的JDBC类型对象转换成指定的Java对象,如下例:
public int getCount(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
return count=jdbc.[color=red]queryForInt[/color]("select count(*)from user");
}
public String getUsername(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
return name=(String) jdbc.[color=red]queryForObject[/color]("select name from username",[color=red]String.class[/color]);
}
4)执行条件查询
在调用queryForObject()进行查询时,需要先使用回调方法创建一个RowMapper类型变量,在执行查询时输入该回调变量,并输入查询的参数数组,如下所示:
public User findUser(long id){
String sql="select id,username,password,email from user where id=?";
RowMapper mapper = new RowMapper(){
public Object mapRow(ResultSet rs,int rowNum)throws SQLException{
User user = new User();
user.setId(rs.getLong("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
return user;
}
};
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
return (User)jdbc.[color=red]queryForObject[/color](sql,[color=red]mapper,new Object[]{Long.vlaueof(id)});[/color]
}
5)返回查询列表
返回表中的所有记录:
public List getUserList(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List rows = jdbc.[color=red]queryForList[/color]("select id,username from user");
return rows;
}
返回的结果集类似下面这种形式:
[{id=1,username=admin},{id=2,username=test}]
如果返回的结果集部止两个字段,则可以使用回调的方式将每一行记录转换为一个JavaBean对象。如下所示,使用query函数进行查询,并进行了行的回调。在回调中对每一个结果集转换为User对象:
public boolean getUserList(String username,String password){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List userList=jdbc.query("select*from user"),
[color=red] new RowMapper(){
public Object mapRow(ResultSet rs,int rowNum)throws Exception{[/color]
User user = new User();
user.setId(is.getLong("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
return user;
}
});
return userList;
}
在Spring中,JdbcTemplate是最常被使用的类,它为用户程序提供了许多便利的数据库操作方法,比如查询、更新等。JdbcTemplate是core包的核心类,它替我们完成了资源的创建及释放工作,从而简化了对JDBC的使用。
1)执行SQL语句
一旦获得一个DataSource和一个JdbcTemplate,我们就可以使用JdbcTemplate提供的丰富功能实现我们的操作。下面创建一张表:
public void execute(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.[color=red]execute[/color]("create table username(id integer,username varchar(20),password varchar(20),email varchar(50))");
}
2)执行更新语句
可以使用update()来执行一个更新的SQL语句,其中的问号表示的参数使用数组表示,如下所示:
public void updateUsername(int id,String name){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.update("update mytable set name=?where id=?",[color=red]new [/color][color=red]Object[]{name,new Long(id)});[/color]
}
execute和update方法之间的区别是:update方法返回的是受影响的记录数目的一个计数,并且如果传入一个参数的话,使用的是Java.sql.PreparedStatement;而execute方法总是使用java.sql.Statement,不接受参数,而且它不返回受影响记录的计数,更适合创建和丢弃表的语句,而update方法更适合于插入、更新和删除操作,这是需要注意的。
3)执行单表查询
比如返回一个汇总(count)结果或者从返回行结果中取得指定列的值,我们使用queryForInt()、queryForLong()或者queryForObject()方法。queryForObject方法用来将返回的JDBC类型对象转换成指定的Java对象,如下例:
public int getCount(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
return count=jdbc.[color=red]queryForInt[/color]("select count(*)from user");
}
public String getUsername(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
return name=(String) jdbc.[color=red]queryForObject[/color]("select name from username",[color=red]String.class[/color]);
}
4)执行条件查询
在调用queryForObject()进行查询时,需要先使用回调方法创建一个RowMapper类型变量,在执行查询时输入该回调变量,并输入查询的参数数组,如下所示:
public User findUser(long id){
String sql="select id,username,password,email from user where id=?";
RowMapper mapper = new RowMapper(){
public Object mapRow(ResultSet rs,int rowNum)throws SQLException{
User user = new User();
user.setId(rs.getLong("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
return user;
}
};
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
return (User)jdbc.[color=red]queryForObject[/color](sql,[color=red]mapper,new Object[]{Long.vlaueof(id)});[/color]
}
5)返回查询列表
返回表中的所有记录:
public List getUserList(){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List rows = jdbc.[color=red]queryForList[/color]("select id,username from user");
return rows;
}
返回的结果集类似下面这种形式:
[{id=1,username=admin},{id=2,username=test}]
如果返回的结果集部止两个字段,则可以使用回调的方式将每一行记录转换为一个JavaBean对象。如下所示,使用query函数进行查询,并进行了行的回调。在回调中对每一个结果集转换为User对象:
public boolean getUserList(String username,String password){
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List userList=jdbc.query("select*from user"),
[color=red] new RowMapper(){
public Object mapRow(ResultSet rs,int rowNum)throws Exception{[/color]
User user = new User();
user.setId(is.getLong("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
return user;
}
});
return userList;
}
JdbcTemplate使用详解
1676

被折叠的 条评论
为什么被折叠?



