Spring(五)--JdbcTemplate
1、概念和准备
1.1、JdbcTemplate:Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作
1.2 准备工作
1.2.1 引入相关依赖
<!-- JdbcTemplate-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
1.2.2在spring配置文件配置数据库连接池
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/userDB?useSSL=true&useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
1.2.3配置 JdbcTemplate 对象,注入 DataSource
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
1.2.4创建 service 类,创建 dao 类,在 dao 注入 jdbcTemplate 对象
开启组件扫描
<context:component-scan base-package="com.ljc"></context:component-scan>
Service
@Service
public class BookService {
//注入dao
@Autowired
private BookDao bookDao;
}
Dao
@Repository
public class BookDaoImpl {
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
}
2、JdbcTemplate操作数据库(添加)
2.1 对应数据库创建实体类
public class Book {
private int book_id;
private String bookName;
private String bStatus;
...getset...
2.2 编写service和dao
(1)在 dao 进行数据库添加操作
(2)调用 JdbcTemplate 对象里面 update 方法实现添加操作
第一个参数:sql语句
第二个参数:可变参数,设置sql语句值
dao
@Repository
public class BookDaoImpl implements BookDao {
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void insert(Book book) {
//1.创建sql语句
String sql = "insert into t_book value(?,?,?)";
//2.调用方法实现
Object[] args = {book.getBook_id(), book.getBookName(), book.getbStatus()};
int result = jdbcTemplate.update(sql, args);
System.out.println(result);
}
}
Service
//添加方法
public void insert(Book book){
bookDao.insert(book);
}
test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setBookName("xxx");
book.setbStatus("yes");
bookService.insert(book);
}
3、JdbcTemplate操作数据库(修改和删除)
@Override
//修改
public void updateBook(Book book) {
String sql = "update t_book set bookname = ?,bstatus=? where book_id = ?";
Object[] args = {book.getBookName(), book.getbStatus(),book.getBook_id()};
int result = jdbcTemplate.update(sql, args);
System.out.println(result);
}
@Override
//删除
public void deleteBook(int book_id) {
String sql = "delete from t_book where book_id = ?";
int result = jdbcTemplate.update(sql,book_id);
System.out.println(result);
}
测试:
@org.junit.Test
public void testUpdateBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setBookName("xxrrr");
book.setbStatus("yes");
book.setBook_id(1);
bookService.updateBook(book);
}
@org.junit.Test
public void testDeleteBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.deleteBook(1);
}
4、JdbcTemplate 操作数据库(查询)
4.1查询返回某个值
1、查询表里面有多少条记录,返回是某个值
2、使用JdbcTemplate实现查询返回某个值代码
有两个参数
第一个参数:sql语句
第二个参数:返回类型 Class
//查询返回某个值
public int selectCount() {
String sql = "select count(*) from t_book";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
4.2查询返回对象
1、场景:查询图书详情
2、JdbcTemplate实现查询返回对象
有三个参数
第一个参数:sql语句
第二个参数:RowMapper,是接口,返回不同类型数据,使用这个接口里面实现类完成数据封装
第三个参数:sql语句值
public Book selectBookInfo(int book_id) {
String sql = "select * from t_book where book_id = ?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), book_id);
return book;
}
查询出对象后以set方法设置进去(注意列名与属性名对应)
4.3查询返回集合
1、场景:查询图书列表分页...
2、调用JdbcTemplate方法实现查询返回集合
有三个参数
第一个参数:sql语句
第二个参数:RowMapper,是接口,返回不同类型数据,使用这个接口里面实现类完成数据封装
第三个参数:sql语句值
public List<Book> findAllBook() {
String sql = "select * from t_book";
List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
return bookList;
}
测试
public void testFindAllBookInfo(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Book> list = bookService.findAll();
System.out.println(list);
}
5、JdbcTemplate操作数据库(批量操作)
5.1批量添加操作
有两个参数
第一个参数:sql语句
第二个参数:List集合,添加多条数据
底层:遍历数组逐个添加
public void batchAddBook(List<Object[]> batchArgs) {
String sql = "insert into t_book value(?,?,?)";
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}
测试:
public void testBatchAdd(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 ={"5","java","aaa"};
Object[] o2 ={"6","c++","bbb"};
Object[] o3 ={"7","python","ccc"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
bookService.batchAdd(batchArgs);
}
5.2实现批量修改操作
public void batchUpdateBook(List<Object[]> batchArgs) {
String sql = "update t_book set bookname = ?,bstatus=? where book_id = ?";
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(ints);
}
5.3实现批量删除操作
public void batchDeleteBook(List<Object[]> batchArgs) {
String sql = "delete from t_book where book_id = ?";
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}