爲了簡化持久化操做,Spring在JDBC API之上提供了JDBC Template組件。前端
添加Spring核心依賴,MySQL驅動java
<!--Spring核心基礎依賴--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- AOP聯盟依賴 --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- Mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.2.RELEASE</version> </dependency>
在applicationContext.xml文件中配置數據源mysql
<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" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置數據源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!--MySQL數據庫驅動--> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <!--鏈接數據庫的URL--> <property name="url" value="jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf8"></property> <!--鏈接數據庫的用戶名--> <property name="username" value="root"></property> <!--鏈接數據庫的密碼--> <property name="password" value="root"></property> </bean> <!--配置JDBC模板--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
使用update方法對數據進行增刪改操做
獲取JdbcTemplate對象spring
private JdbcTemplate jdbcTemplate; { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate"); }
添加數據sql
String sql = "insert into book(name,author,price) values(?,?,?)"; jdbcTemplate.update(sql,new Object[]{"西遊記","吳承恩",90.8});
修改數據數據庫
String sql = "update book set name=?,author=?,price=? where id=?";
jdbcTemplate.update(sql,"紅樓夢","曹雪芹",100,1);
刪除數據express
String sql = "delete from book where id=?";
jdbcTemplate.update(sql,1);
批量操做數據,批量執行多SQL語句app
String[] sqls = { "insert into book(name,author,price) values('Java基礎','張三',90)", "insert into book(name,author,price) values('C語言','李四',80)", "insert into book(name,author,price) values('Web前端','王五',95)", "update book set name='水滸傳',author='施耐庵' where id=1", }; jdbcTemplate.batchUpdate(sqls);
批量操做數據,執行同SQL語句url
String sql = "insert into book(name,author,price) values(?,?,?)"; List<Object[]> list = new ArrayList<Object[]>(); list.add(new Object[]{"HTML","Tom",90.8}); list.add(new Object[]{"CSS","Jack",88}); list.add(new Object[]{"JavaScript","Lily",89}); jdbcTemplate.batchUpdate(sql,list);
查詢單個數據spa
String sql = "select count(*) from book"; int count = jdbcTemplate.queryForObject(sql,Integer.class); System.out.println(count);
查詢多個數據
String sql = "select name from book where price=?"; List<String> list = jdbcTemplate.queryForList(sql,String.class,90); System.out.println(list);
查詢單個對象
String sql = "select * from book where id=?"; Map<String,Object> map = jdbcTemplate.queryForMap(sql,2); System.out.println(map);
查詢多個對象
String sql = "select * from book"; List<Map<String,Object>> list =jdbcTemplate.queryForList(sql); System.out.println(list);
建立實體類
public class Book { private int id; private String name; private String author; private double price; //getter、setter、toString方法 }
查詢單個對象
String sql = "select * from book where id=?"; Book book = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Book>(Book.class),2); System.out.println(book);
查詢多個對象
String sql = "select * from book"; List<Book> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class)); System.out.println(list);