例如:String sql = "update table_name set del_flag = '0' where uuid in (:ids)";
//数据持久层
//spring 整合JdbcTemplate必须加@Component注解
@Component
public class HealthcheckDao {
//jdbcTemplate 必须用@Resource(name="namedParameterJdbcTemplate")引进,如:
@Resource(name="namedParameterJdbcTemplate")
private NamedParameterJdbcTemplate jdbcN;
/**
* private JdbcTemplate jdbcT;
* @param List<String> idList
* @param 参数说明
*/
public void batchUpdate(List<String> idList){
String sql = "update table_name set del_flag = '0' where uuid in (:ids)";
try {
//参数必须要用jdbc提供的 MapSqlParameterSource.addValue(String arg0 ,Object value)方法添加
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("ids", idList);
System.out.println(sql);
int n = jdbcN.update(sql, parameters);
System.out.println("++++++++++++更新"+n+"条数据++++++++++");
} catch (DataAccessException e) {
e.printStackTrace();
}
}
@Component
public class RptHealthcheckDao {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcT;
@Resource(name="namedParameterJdbcTemplate")
private NamedParameterJdbcTemplate jdbcN;
public List<Map<String, Object>> queryList(Map<String, Object> map){
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String sql = "SELECT "
+ " REPLACE (uuid(), '-', '') as id, "
+ " t.hospital_id as hospital_id,"
+ " :rpt_date as rpt_data,"
+ " result_time,"
+ " count(1) check_count, "
+ " sum(IF(t.check_result = 0, 1, 0)) AS ok_count,"
+ " sum(IF(t.check_result = 0, 0, 1)) AS fail_count"
+ " FROM heal_t_check_recode t"
+ " WHERE t.check_time >= CONCAT(:rpt_date, ' 00:00:00')"
+ " AND t.check_time <= CONCAT(:rpt_date, ' 23:59:59')"
+ " AND t.`status` != '-1'"
+ " GROUP BY t.hospital_id";
try {
System.out.println(sql);
list = jdbcN.queryForList(sql, map);
} catch (DataAccessException e) {
e.printStackTrace();
}
return list;
}
}
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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- <bean class="com.sycloud.report.utils.SpringContextHolder" lazy-init="false" /> -->
<!-- 数据库配置-->
<bean id="jdbcDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="${c3p0.driver}">
</property>
<property name="url"
value="${c3p0.url}">
</property>
<property name="username" value="${c3p0.user}"></property>
<property name="password" value="${c3p0.password}"></property>
</bean>
<bean id="healchkDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${c3p0.healchekDriver}"/>
<property name="url" value="${c3p0.healchekUrl}"/>
<property name="username" value="${c3p0.healchekUser}" />
<property name="password" value="${c3p0.healchekPassword}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
abstract="false" lazy-init="false" autowire="default" >
<property name="dataSource"> <ref bean="healchkDataSource"/></property>
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="healchkDataSource"></constructor-arg>
</bean>
<!-- 自动扫描 -->
<context:component-scan base-package="com.**.report" />
</beans>
这个里有个坑,如果和其他语句一样用 int n = jdbcN.update(sql, map);不会报错,但是始终不会更新,大家一定要注意,这是我的第一篇bolg希望大家多多指正