Spring 使用JdbcTemplate操作資料庫
阿新 • • 發佈:2018-12-25
1、匯入jar包
2、db.properties配置檔案
jdbc.username=root
jdbc.password=123456
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lf
3、Spring配置jdbc並且使用JdbcTemplate模板
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:property-placeholder location="classpath:db.properties" /> <context:component-scan base-package="cn.com.day03"></context:component-scan> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> </bean> <!--Spring配置JdbcTemplate模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="datasource"></property> </bean> </beans>
POJO類
package cn.com.day03; public class User { private String number; private String name; private String kinds; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getKinds() { return kinds; } public void setKinds(String kinds) { this.kinds = kinds; } public User(String number, String name, String kinds) { super(); this.number = number; this.name = name; this.kinds = kinds; } @Override public String toString() { return "User [number=" + number + ", name=" + name + ", kinds=" + kinds + "]"; } public User() { // TODO Auto-generated constructor stub } }
4、測試類
package cn.com.day03; import java.math.BigInteger; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; public class JdbcUtils { /* * author:命運的信徒 * date:2018/12/22 * arm:Spring對jdbc的支援 */ private ApplicationContext ioc=null; //模板 private JdbcTemplate jdbcTemplate=null; private TemplateDao templateDao=null; { ioc=new ClassPathXmlApplicationContext("bean-jdbc.xml"); jdbcTemplate=(JdbcTemplate) ioc.getBean("jdbcTemplate"); templateDao=ioc.getBean(TemplateDao.class); } //測試是否可以連線上資料庫 public void getSource(){ DataSource da=ioc.getBean(DataSource.class); try { System.out.println(da.getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //修改單條資料 /* *修改單個數據真神奇,java中不需要對應的類,直接通過sql語句就可以了 * */ public void update(){ String sql="update user set name=? where number=?"; jdbcTemplate.update(sql, "son","777"); } //批量插入多個數據 public void batchInsert(){ String sql="insert into user values(?,?,?)"; List<Object []> batchArgs=new ArrayList<>(); batchArgs.add(new Object[]{"1","野村萬齋","狂言師"}); batchArgs.add(new Object[]{"2","陳道明","演員"}); batchArgs.add(new Object[]{"3","孫耀威","歌手"}); batchArgs.add(new Object[]{"4","侯夢瑤","演員"}); jdbcTemplate.batchUpdate(sql, batchArgs); } //批量修改多個數據 public void batchUpdate(){ String sql="update user set name=? where number=?"; List<Object[]> batchArgs=new ArrayList<>(); batchArgs.add(new Object[]{"野村萬司","1"}); batchArgs.add(new Object[]{"康熙","2"}); batchArgs.add(new Object[]{"劉病已","3"}); jdbcTemplate.batchUpdate(sql, batchArgs); } //從資料庫中查詢某一條記錄來獲取一個物件 /*注意不是呼叫queryForObject(String sql,Class[User] requiredType,Object ...args)方法 * 而需要呼叫queryForObject(sql, rowMapper,"1"); * 1其中的RowMapper指定如何去對映結果集中的行,常用的實現類是BeanPropertyRowMapper<>(User.class); * 2.使用SQL中的列名完成列名和類的屬性名的對映 * */ public void queryObject(){ String sql="select number,name,kinds from user where number=?"; //不是呼叫queryForObject(sql, User.class, "1")這個,這個是返回指定型別的屬性的時候用到的 /*User u=jdbcTemplate.queryForObject(sql, User.class, "1");*/ RowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class); User u=jdbcTemplate.queryForObject(sql, rowMapper,"1"); System.out.println(u.toString()); } //查詢實體類的集合 public void querylist(){ String sql="select * from user where number>?"; RowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class); List<User> ll=jdbcTemplate.query(sql, rowMapper,"1"); for (User user : ll) { System.out.println(user.getName()); } } //獲取單個的值 public void queryone(){ String sql="select count(*) from user"; System.out.println(jdbcTemplate.queryForObject(sql, BigInteger.class)); } //測試模板整合的方法 @Test public void TestTemplateDao(){ User u=templateDao.queryObject("1"); System.out.println(u); } }