1, Why Spring JDBC package offered?
Because Spring provides a complete template class and a base class to simplify the development, we only need a small amount of code to write.
2, examples to explain
The first step: introducing dependent mysql-connector spring-jdbc spring-tx spring-core spring-beans spring-context like
The second step: the establishment of a database, configuration xml
The third step: Get jdbcTemplate objects
Can be obtained by
private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource){ this.jdbcTemplate=new JdbcTemplate(dataSource); }
<bean id="userDao" class="com.test.jdbc.dao.impl.UserDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean>
Can inherit JdbcDaoSupport, use super.getJdbcTemplate () to obtain
Step Four: Use jdbcTemplate object CRUD
CRUD use Update (), the parameters of this method, one of the first sql statement, followed by a variable parameter, passed sql statement placeholder
Query using queryForObject () method of the first parameter is a parameter in sql required to achieve the third parameter RowMapper interface objects,
For the result set, this method requires a single object returns
query () method returns the collection of objects
public class UserDaoImpl extends JdbcDaoSupport implements IUserDao { public void save(User user) { super.getJdbcTemplate().update("insert into user(id,username,password,date,salary) values(null,?,?,?,?)", user.getUsername(),user.getPassword(),user.getDate(),user.getSalary()); } public void update(User user) { super.getJdbcTemplate().update("update user set username=?,password=?,date=?,salary=? where id=?", user.getUsername(), user.getPassword(), user.getDate(), user.getSalary(), user.getId()); } public void delete(Integer id) { super.getJdbcTemplate().update("delete from user where id=?",id); } public User findById(Integer id) { /* List<User> user = super.getJdbcTemplate().query("select * from user where id=?", new Object[]{id}, new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setId(resultSet.getInt("id")); user.setDate(resultSet.getDate("date")); user.setUsername(resultSet.getString("username")); user.setPassword(resultSet.getString("password")); user.setSalary(resultSet.getBigDecimal("salary")); return user; } //方法的返回值会直接封装到集合中 }); return user.size()>0?user.get(0):null;*/ User user = super.getJdbcTemplate().queryForObject("select * from user where id=?", new Object[]{id}, new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setId(resultSet.getInt("id")); user.setDate(resultSet.getDate("date")); user.setUsername(resultSet.getString("username")); user.setPassword(resultSet.getString("password")); user.setSalary(resultSet.getBigDecimal("salary")); return user; } }); return user; } public List<User> findAll() { List<User> users = super.getJdbcTemplate().query("select * from user", new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setId(resultSet.getInt("id")); user.setDate (ResultSet.getDate ( "DATE" )); user.setUsername(resultSet.getString( "username" )); user.setPassword (resultSet.getString ( "password" )); user.setSalary (ResultSet.getBigDecimal ( "the salary" )); return User ; } // the return value of the method will be set encapsulates }); return Users; } }
3, JdbcTemplate JDBC type is the central core of the package
Knowledge inquiry address:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/data-access.html#jdbc