ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.htmlphp
1. 不使用JdbcTemplate示例:若是不用JdbcTemplate,必須建立大量的冗餘代碼(建立鏈接,關閉鏈接,處理異常)中的全部DAO數據庫的操做方法 - 插入,更新和刪除。它的效率並非很高,容易出錯和乏味。html
conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, customer.getCustId());
2. 使用JdbcTemplate示例:使用JdbcTemplate可節省大量的冗餘代碼,由於JdbcTemplate類會自動處理它。java
private DataSource dataSource; private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void insert(Customer customer){ String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"; jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update(sql, new Object[] { customer.getCustId(), customer.getName(),customer.getAge() }); }
3. 使用JdbcDaoSupport示例經過擴展 JdbcDaoSupport,設置數據源,而且 JdbcTemplate 在你的類中再也不是必需的,只須要正確的數據源注入JdbcCustomerDAO。可使用 getJdbcTemplate()方法獲得 JdbcTemplate。mysql
customerDAO類函數:
public class JdbcCustomerDAO extends JdbcDaoSupport implements CustomerDAO { //no need to set datasource here public void insert(Customer customer){ String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
customer.setName("test',2);show databases;#");//沒法注入!spring自動將'進行轉義。即便爲test\',2)也沒法注入。
getJdbcTemplate().update(sql, new Object[] { customer.getCustId(),customer.getName(),customer.getAge() }); }//此處將
4.相應的xml文件設置dataSource/customerDAOweb
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/yiibaijava" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> </beans> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
public String findCustomerNameById(int custId){ String sql = "SELECT NAME FROM CUSTOMER WHERE CUST_ID = ?"; String name = (String)getJdbcTemplate().queryForObject( sql, new Object[] { custId }, String.class); return name; }//此處查詢條件爲int custID,顯然php中整數型注入沒法應用。
字符串查詢狀況:spring
public Customer findByCustomerName(String name){ String sql = "SELECT * FROM CUSTOMER WHERE NAME = ?"; name="name1';show databases;#";//spring直接轉義'name1\';show databases;#'。沒法注入! Customer customer = (Customer)getJdbcTemplate().queryForObject( sql, new Object[] { name }, new CustomerRowMapper()); return customer; }
與php不一樣,java中對象爲強類型,int沒法注入,而字符串又直接轉義,避免了注入的可能性。sql
public Customer findByCustomerId(int custId){ String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?"; Customer customer = (Customer)getJdbcTemplate().queryForObject( sql, new Object[] { custId }, new CustomerRowMapper()); return customer; } public class CustomerRowMapper implements RowMapper { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Customer customer = new Customer(); customer.setCustId(rs.getInt("CUST_ID")); customer.setName(rs.getString("NAME")); customer.setAge(rs.getInt("AGE")); return customer; } }
爲了解決這個問題,可使用「命名參數」,SQL參數是由一個冒號開始後續定義的名稱,而不是位置。在另外的,命名參數只是在SimpleJdbcTemplate類和NamedParameterJdbcTemplate支持。數據庫
例子向您展現如何使用命名參數在一個 INSERT 語句。 //insert with named parameter public void insertNamedParameter(Customer customer){ String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (:custId, :name, :age)"; Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("custId", customer.getCustId()); parameters.put("name", customer.getName()); parameters.put("age", customer.getAge()); getSimpleJdbcTemplate().update(sql, parameters); } 示例 2 例子來講明如何使用命名參數在批處理操做語句。 public void insertBatchNamedParameter(final List<Customer> customers){ String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (:custId, :name, :age)"; List<SqlParameterSource> parameters = new ArrayList<SqlParameterSource>(); for (Customer cust : customers) { parameters.add(new BeanPropertySqlParameterSource(cust)); } getSimpleJdbcTemplate().batchUpdate(sql, parameters.toArray(new SqlParameterSource[0])); }
8.spring web中xml配置jdbctemplate.編程
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/abc" /> <property name="resourceRef" value="true"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <resource-ref> <res-ref-name> jdbc/abc </res-ref-name> <res-type> javax.sql.DataSource </res-type> </resource-ref> tomcat/conf/context.xml: <Resource name="jdbc/abc" auth="Container" type="javax.sql.DataSource" ... </Resource >
9.getSimpleJdbcTemplate執行多行sql語句問題tomcat
在默認狀況下,即datasource設置爲"jdbc:mysql://192.168.0.10:3306/yiibai」,不能執行多行sql語句,將會產生錯誤:PreparedStatementCallback; bad SQL grammar。
若要執行多行語句,設置datasource爲「jdbc:mysql://192.168.0.10:3306/yiibai?allowMultiQueries=true」,則能夠順利執行。