NamedParameterJdbcTemplate
簡單sql
NamedParameterJdbcTemplate
支持命名參數,這是原生jdbc
的一大缺點,由於jdbc
是採用索引的方式設置參數,在數據庫或者sql
發現變化時咱們須要修改代碼,而且這種維護成本很高,同時也很容易出錯,那命名參數能夠很好的解決這個問題。數據庫
NamedParameterJdbcTemplate
支持對象自動映射,以下一段代碼NamedParameterJdbcTemplate
會自動將返回結果映射爲Person
對象mybatis
Person p = new Person(); p.setName("kevin"); p.setAddress("Shanghai"); p.setCountry("China"); namedTemplate.update("insert into t_person(name, address, country) values(:name,:address,:country)", new BeanPropertySqlParameterSource(p))
快速,NamedParameterJdbcTemplate
只是實現了命名參數及數據封裝,沒有其它任何額外的開銷,在運行效率上無限接近原生jdbc
app
接下來咱們看一下使用NamedParameterJdbcTemplate
如何工做,還有相同場景下MyBatis
的代碼。code
String sql = "INSERT INTO `t_person` (firstName, lastName, age, gender, height, weight, address, hobby, createdTime)" + " VALUES (:firstName, :lastName, :age, :gender, :height, :weight, :address, :hobby, :createdTime)"; KeyHolder key = new GeneratedKeyHolder(); jdbcOperations.update(sql, new BeanPropertySqlParameterSource(p), key); p.setId(key.getKey().longValue());
咱們能夠經過BeanPropertySqlParameterSource
自動綁定SQL
參數只須要屬性名稱爲命名參數相同便可,同時咱們也能夠使用MapSqlParameterSource/Map
綁定SQL參數。對象
<insert id="insert" keyProperty="id" useGeneratedKeys="true"> INSERT INTO `t_person` (firstName, lastName, age, gender, height, weight, address, hobby, createdTime) VALUES (#{firstName}, #{lastName}, #{age}, #{gender}, #{height}, #{weight}, #{address}, #{hobby}, #{createdTime}) </insert>
String sql = "select * from t_person where id=:id"; jdbcOperations.queryForObject(sql, new MapSqlParameterSource("id", id), new BeanPropertyRowMapper<>(Person.class));
咱們能夠經過BeanPropertyRowMapper
將返回結果自動映射爲對象類型,和mybatis
同樣只須要返回的列名與屬性名稱相同便可索引
<select id="queryById" resultType="io.zhudy.namedjdbcmybatis.benchmark.Person"> SELECT * FROM t_person WHERE id=#{value} </select>
String sql = "select * from t_person where id=:id"; jdbcOperations.queryForObject(sql, new MapSqlParameterSource("id", selectId()), (rs, rowNum) -> { Person p = new Person(); p.setId(rs.getLong("id")); p.setFirstName(rs.getString("firstName")); p.setLastName(rs.getString("lastName")); p.setAge(rs.getInt("age")); p.setGender(rs.getInt("gender")); p.setHeight(rs.getInt("height")); p.setWeight(rs.getInt("weight")); p.setAddress(rs.getString("address")); p.setHobby(rs.getString("hobby")); p.setCreatedTime(rs.getLong("createdTime")); return p; });
<resultMap id="person" type="io.zhudy.namedjdbcmybatis.benchmark.Person"> <result column="id" property="id"/> <result column="firstName" property="firstName"/> <result column="lastName" property="lastName"/> <result column="age" property="age"/> <result column="gender" property="gender"/> <result column="height" property="height"/> <result column="weight" property="weight"/> <result column="weight" property="weight"/> <result column="address" property="address"/> <result column="hobby" property="hobby"/> <result column="createdTime" property="createdTime"/> </resultMap> <select id="queryByIdForManualMap" resultMap="person"> SELECT * FROM t_person WHERE id=#{value} </select>
String sql = "select * from t_person"; jdbcOperations.query(sql, EmptySqlParameterSource.INSTANCE, new BeanPropertyRowMapper<>(Person.class));
<select id="query" resultType="io.zhudy.namedjdbcmybatis.benchmark.Person"> SELECT * FROM t_person </select>
經過上面的代碼咱們能夠發現使用NamedParameterJdbcTemplate
操做數據庫很是的容易,不會給開發帶來額外的負擔,代碼很是的簡潔,同時程序的運行效率也很是的高。開發