JdbcTemplate概念及使用
a)Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
b)引入相关 jar 包
c)在 spring 配置文件配置数据库连接池xml
1 2 3 4 5 6 7
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql:///test" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> </bean>
|
d)配置 JdbcTemplate 对象,注入 DataSource
1 2 3 4 5 6 7
| <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property>
</bean>
|
e)创建 service 类,创建 dao 类,在 dao 注入 jdbcTemplate 对象
1 2
| <context:component-scan base-package="com.atguigu"></context:component-scan>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Service public class BookService { @Autowired private BookDao bookDao; }
@Repository public class BookDaoImpl implements BookDao { @Autowired private JdbcTemplate jdbcTemplate; }
|
JdbcTemplate 操作数据库(添加)
a)对应数据库创建实体类
b)创建service和dao
(1)在 dao 进行数据库添加操作
(2)调用 JdbcTemplate 对象里面 update 方法实现添加操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Repository public class BookDaoImpl implements BookDao { @Autowired private JdbcTemplate jdbcTemplate; @Override public void add(Book book) { String sql = "insert into t_book values(?,?,?)"; Object[] args = {book.getUserId(), book.getUsername(),book.getUstatus()}; int update = jdbcTemplate.update(sql,args); System.out.println(update); } }
|
JdbcTemplate 操作数据库(修改和删除)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Override public void updateBook(Book book) { String sql = "update t_book set username=?,ustatus=? where user_id=?"; Object[] args = {book.getUsername(), book.getUstatus(),book.getUserId()}; int update = jdbcTemplate.update(sql, args); System.out.println(update); }
@Override public void delete(String id) { String sql = "delete from t_book where user_id=?"; int update = jdbcTemplate.update(sql, id); System.out.println(update); }
|
JdbcTemplate 操作数据库(查询返回某个值)
1 2 3 4 5 6 7 8 9
| @Override public int selectCount() { String sql = "select count(*) from t_book";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class); return count; } JdbcTemplate 操作数据库(
|
JdbcTemplate 操作数据库(查询返回对象)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Override public Book findBookInfo(String id) { String sql = "select * from t_book where user_id=?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id); return book; }
|
JdbcTemplate 操作数据库(查询返回集合)
1 2 3 4 5 6 7 8 9
|
@Override public List<Book> findAllBook() { String sql = "select * from t_book"; List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class)); return bookList; }
|
JdbcTemplate 操作数据库(批量操作)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Override public void batchAddBook(List<Object[]> batchArgs) { String sql = "insert into t_book values(?,?,?)";
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs); System.out.println(Arrays.toString(ints)); }
List<Object[]> batchArgs = new ArrayList<>(); Object[] o1 = {"3","java","a"}; Object[] o2 = {"4","c++","b"}; Object[] o3 = {"5","MySQL","c"}; batchArgs.add(o1); batchArgs.add(o2); batchArgs.add(o3);
bookService.batchAdd(batchArgs);
|
JdbcTemplate 实现批量修改操作
1 2 3 4 5 6 7
| @Override public void batchUpdateBook(List<Object[]> batchArgs) { String sql = "update t_book set username=?,ustatus=? where user_id=?"; int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs); System.out.println(Arrays.toString(ints)); }
|