Spring PreparedStatement是spring JdbcTemplate中的参数预处理对象,用于防止sql注入,使用起来也很简单,它用于excute执行语句时传递参数,如下是它常用的方法:
public T execute(String sql,PreparedStatementCallback<T>);
PreparedStatement接口中有一个PreparedStatementCallback方法,用户参数回调,设置sql语句参数就是在这下面设置的,方法如下:
public T doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException
我们用 一个案例来学习PreparedStatement预处理对象的使用。
步骤一:创建一个数据库表,本案例使用oracle数据库,如下:
create table employee( id number(10), name varchar2(100), salary number(10) );
步骤二:创建Employee.java实体类,代码如下:
package com.tpyyes; public class Employee { private int id; private String name; private float salary; //getters and setters }
步骤三:创建EmployeeDao.java类,在里面执行sql语句,使用PreparedStatement预处理来设置值,代码如下:
package com.tpyyes; import java.sql.PreparedStatement; import java.sql.SQLException; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCallback; public class EmployeeDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public Boolean saveEmployeeByPreparedStatement(final Employee e){ String query="insert into employee values(?,?,?)"; return jdbcTemplate.execute(query,new PreparedStatementCallback<Boolean>(){ @Override public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setInt(1,e.getId()); ps.setString(2,e.getName()); ps.setFloat(3,e.getSalary()); return ps.execute(); } }); } }
步骤四:在applicationContext.xml配置文件中set注入spring 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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="system" /> <property name="password" value="oracle" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> </bean> <bean id="edao" class="com.tpyyes.EmployeeDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>
步骤五:测试PreparedStatement预处理对象设置值是否有效,新建Test.java类,测试代码如下:
package com.tpyyes; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); EmployeeDao dao=(EmployeeDao)ctx.getBean("edao"); dao.saveEmployeeByPreparedStatement(new Employee(108,"Amit",35000)); } }
PreparedStatement预处理对象就是用来设置sql语句的参数,从而防止sql注入的,所以学会它很有必要。