当前位置:Gxlcms > 数据库问题 > JDBCTemplate

JDBCTemplate

时间:2021-07-01 10:21:17 帮助过:25人阅读


  如果要使用具名参数的sql语句就必须在spring配置文件中配置NamedParameterJdbcTemplat这个模板类,而不能使用原来的JdbcTemplate,因为JdbcTemplate不能完成这样的任务!

<!-- 加载properties文件中 信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.passowrd}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
</bean>

<!-- 配置JdbcTemplate对应的bean, 并装配dataSource数据源属性-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 为了执行带有具名参数的SQL语句,需要配置NamedParameterJdbcTemplate -->
<!-- 该NamedParameterJdbcTemplate类没有无参构造器,需要传入JdbcTemplate对象或者数据源对象[DataSource] -->
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<!-- 不能使用property标签配置哦 -->
<constructor-arg ref="jdbcTemplate"></constructor-arg>
</bean>

  

public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private NamedParameterJdbcTemplate namedJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);

@Test
public void test01(){
String sql="INSERT INTO employee(`emp_name`,`salary`) VALUES(:paramName,:paramSalary)";
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("paramName","张学友" );
paramMap.put("paramSalary",1000);

namedJdbcTemplate.update(sql, paramMap);
}
}

  

实验8:重复实验7,以SqlParameterSource形式传入参数值

public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private NamedParameterJdbcTemplate namedJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);

@Test
public void test01(){
String sql="INSERT INTO employee(`emp_name`,`salary`) VALUES(:empName,:salary)";
//该BeanPropertySqlParameterSource类构造器需要一个对象参数,该对象参数是一个封装了sql语句参数的对象!
//此时要求对象的属性名要和sql中的参数名保持一致!这里我们使用Employee对象来完成
Employee employee= new Employee(null, "郭富城", 1500);
//以实体对象的形式封装具名参数和值
SqlParameterSource source = new BeanPropertySqlParameterSource(employee);

namedJdbcTemplate.update(sql, source);
}
}

  

实验9:创建JdbcTemplateDao,自动装配JdbcTemplate对象
1.创建dao类:

@Repository
public class JdbcTemplateDao {
@Autowired
private JdbcTemplate jdbcTemplate;

public void update(String sql,Object ...args){
jdbcTemplate.update(sql, args);
}
}


2.配置spring的配置文件

<!-- 配置扫描的包 -->
<context:component-scan base-package="com.neuedu.dao"></context:component-scan>
<!-- 加载properties文件中 信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.passowrd}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
</bean>

<!-- 配置JdbcTemplate对应的bean, 并装配dataSource数据源属性-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>

  

3.测试该dao

public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private NamedParameterJdbcTemplate namedJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);

@Test
public void test01(){
JdbcTemplateDao dao = ioc.getBean(JdbcTemplateDao.class);
String sql = "INSERT INTO employee(`emp_name`,`salary`) VALUES(?,?)";
dao.update(sql, "比尔盖茨",10000000);

}
}

  

 

 

JDBCTemplate

标签:root   tom   oid   sso   style   row   exce   print   package   

人气教程排行