@Test
public void test3() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
int updateRows = jdbcTemplate.update("INSERT INTO t_user (name) VALUE (?)", new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, "mysql系列");
}
});
System.out.println("影响行数:" + updateRows);
}
获取自增列的值
Api
public int update(final PreparedStatementCreator psc, final KeyHolder generatedKeyHolder)
案例
@Test
public void test4() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "INSERT INTO t_user (name) VALUE (?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
int rowCount = jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
//手动创建PreparedStatement,注意第二个参数:Statement.RETURN_GENERATED_KEYS
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, "获取自增列的值");
return ps;
}
}, keyHolder);
System.out.println("新记录id:" + keyHolder.getKey().intValue());
}
输出
新记录id:122
mysql> select id,name from t_user;
+-----+-----------------------+
| id | name |
+-----+-----------------------+
| 114 | 路人 |
| 115 | java高并发 |
| 116 | spring系列 |
| 117 | maven系列 |
| 118 | mysql系列 |
| 122 | 获取自增列的值 |
+-----+-----------------------+
6 rows in set (0.00 sec)
批量增删改操作
Api
int[] batchUpdate(final String[] sql);
int[] batchUpdate(String sql, List
案例
@Test
public void test5() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List list = Arrays.asList(
new Object[]{"刘德华"},
new Object[]{"郭富城"},
new Object[]{"张学友"},
new Object[]{"黎明"});
int[] updateRows = jdbcTemplate.batchUpdate("INSERT INTO t_user (name) VALUE (?)", list);
for (int updateRow : updateRows) {
System.out.println(updateRow);
}
}
查询操作
查询一列单行
Api
/**
* sql:执行的sql,如果有参数,参数占位符?
* requiredType:返回的一列数据对应的java类型,如String
* args:?占位符对应的参数列表
**/
T queryForObject(String sql, Class requiredType, @Nullable Object... args)
案例
@Test
public void test6() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String name = jdbcTemplate.queryForObject("select name from t_user where id = ?", String.class, 114);
System.out.println(name);
}
输出
路人
db中对应数据
mysql> select name from t_user where id = 114;
+--------+
| name |
+--------+
| 路人 |
+--------+
1 row in set (0.00 sec)
使用注意
若queryForObject中sql查询无结果时,会报错
如id为0的记录不存在
mysql> select name from t_user where id = 0;
Empty set (0.00 sec)
@Test
public void test7() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String name = jdbcTemplate.queryForObject("select name from t_user where id = ?", String.class, 0);
System.out.println(name);
}
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
at org.springframework.dao.support.DataAccessUtils.nullableSingleResult(DataAccessUtils.java:97)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:784)
List queryForList(String sql, Class elementType);
List queryForList(String sql, Class elementType, @Nullable Object... args);
List queryForList(String sql, Object[] args, Class elementType);
List queryForList(String sql, Object[] args, int[] argTypes, Class elementType);
@Test
public void test8() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
// List queryForList(String sql, Class elementType);
List list1 = jdbcTemplate.queryForList("select name from t_user where id>131", String.class);
System.out.println("list1:" + list1);
// List queryForList(String sql, Class elementType, @Nullable Object... args);
List list2 = jdbcTemplate.queryForList("select name from t_user where id>?", String.class, 131);
System.out.println("list2:" + list2);
// List queryForList(String sql, Object[] args, Class elementType);
List list3 = jdbcTemplate.queryForList("select name from t_user where id>?", new Object[]{131}, String.class);
System.out.println("list3:" + list3);
// List queryForList(String sql, Object[] args, int[] argTypes, Class elementType);
List list4 = jdbcTemplate.queryForList("select name from t_user where id>?", new Object[]{131}, new int[]{java.sql.Types.INTEGER}, String.class);
System.out.println("list4:" + list4);
}
List results = new ArrayList<>();
int rowNum = 0;
while (rs.next()) {
results.add(this.rowMapper.mapRow(rs, rowNum++));
}
return results;
案例
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private Integer id;
private String name;
}
@Test
public void test9() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select id,name from t_user where id = ?";
//查询id为34的用户信息
User user = jdbcTemplate.queryForObject(sql, new RowMapper() {
@Nullable
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(1));
return user;
}
}, 134);
System.out.println(user);
}
@Test
public void test10() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select id,name from t_user where id = ?";
//查询id为34的用户信息
RowMapper rowMapper = new BeanPropertyRowMapper<>(User.class);
User user = jdbcTemplate.queryForObject(sql, rowMapper, 134);
System.out.println(user);
}
查询多列多行,每行结果为一个Map
Api
List
每行结果为一个Map,key为列名小写,value为列对应的值。
案例
@Test
public void test11() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select id,name from t_user where id>?";
List> maps = jdbcTemplate.queryForList(sql, 130);
System.out.println(maps);
}
List query(String sql, RowMapper rowMapper, @Nullable Object... args)
案例
@Test
public void test12() {
DataSource dataSource = DataSourceUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select id,name from t_user where id>?";
List maps = jdbcTemplate.query(sql, new RowMapper() {
@Nullable
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(1));
return user;
}
}, 130);
System.out.println(maps);
}
#include<iostream>
#include<cassert>
using namespace std;
template<class T, int SIZE = 50>
class Stack{
private:
T list[SIZE];//数组存放栈的元素
int top;//栈顶位置
public:
Stack(
Gson提供了丰富的预定义类型适配器,在对象和JSON串之间进行序列化和反序列化时,指定对象和字符串之间的转换方式,
DateTypeAdapter
public final class DateTypeAdapter extends TypeAdapter<Date> {
public static final TypeAdapterFacto