简介
Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。
Spring框架提供了JdbcTemplate类,该类是Spring框架数据抽象层的基础这是管理所有数据库通信和异常处理的中央框架类。
JdbcTempla使用
- 添加所需依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.9.RELEASE</version> </dependency>
- 配置 JdbcTemplate 对象,注入 DataSource.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--组件扫描--> <context:component-scan base-package="dao"></context:component-scan> <context:component-scan base-package="entity"></context:component-scan> <context:component-scan base-package="service"></context:component-scan> <!--数据库连接池--> <!--<bean id="dataSource" class="com.mysql.cj.jdbc.Driver" ></bean>--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql:///test"></property> <property name="username" value="root"></property> <property name="password" value="...."></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> <!--jdbcTemplate对象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入DataSource--> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
@Repository public class BookDaoImpl implements BookDao { //注入 JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; //实现添加书籍操作 @Override public void add(Book book){ //创建SQL语句 String sql="insert into book values(?,?,?)"; //调用方法实现 Object[] args={book.getId(),book.getBookname(),book.getBookstuts()}; int update=jdbcTemplate.update(sql,args); System.out.println(update); } }
@Service public class BookService { //注入 dao @Autowired private BookDao bookDao; //添加方法 public void addbook(Book book){ bookDao.add(book); } }
public class Book { private int id; private String bookname; private String bookstuts; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getBookstuts() { return bookstuts; } public void setBookstuts(String bookstuts) { this.bookstuts = bookstuts; } }
测试类
//test01:测试添加 @Test public void test01(){ AbstractApplicationContext abstractApplicationContext=new ClassPathXmlApplicationContext("jdbc.xml"); BookService bookService=abstractApplicationContext.getBean("bookService",BookService.class); Book book=new Book(); book.setId(2); book.setBookname("abc"); book.setBookstuts("ok"); bookService.addbook(book); String str= JSONObject.toJSONString(book); logger.info(str); }
利用JdbcTemplate实现批量的操作
//批量 @Override public void batchadd(List<Object[]> batchargs){ //创建SQL语句 String sql="insert into book values(?,?,?)"; int[] ints=jdbcTemplate.batchUpdate(sql,batchargs); }
void batchadd(List<Object[]> batchargs);
//批量添加 public void batchadd(List<Object[]> batchargs){ bookDao.batchadd(batchargs); }
批量操作的测试
//test07 批量操作数据库 @Test public void test07(){ AbstractApplicationContext abstractApplicationContext=new ClassPathXmlApplicationContext("jdbc.xml"); BookService bookService=abstractApplicationContext.getBean("bookService",BookService.class); List<Object[]> objects=new ArrayList<>(); Object[] objects1={3,"book1", "ok"}; Object[] objects2={4,"book2", "ok"}; Object[] objects3={5,"book3", "ok"}; objects.add(objects1); objects.add(objects2); objects.add(objects3); bookService.batchadd(objects); String str= JSONObject.toJSONString(objects); logger.info(str); }