naTest1()
naTest2()
1.在context.xml 中配置
<!--配置NamedParameterJdbcTemplated-->
<bean id="NamedParameterJdbcTemplated"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
2.Test
package SpringTemplate.NamedParameterJT;
import SpringTemplate.Tem2.Three;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class NamedPjt {
private ApplicationContext context = null;
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate na;
{
context = new ClassPathXmlApplicationContext("JDBCTEM/JDBCTEMcontext.xml");
jdbcTemplate = (JdbcTemplate) context.getBean("JdbcTemplate");
na=(NamedParameterJdbcTemplate) context.getBean("NamedParameterJdbcTemplated");
}
@Test
public void naTest1(){
String sql = "insert into three values(:id,:name,:value)";
Map<String,Object> map = new HashMap<String,Object>();
map.put("id",5);
map.put("name","agi");
map.put("value",100);
na.update(sql,map);
}
@Test
public void naTest2(){
String sql = "insert into three values(:id,:name,:value)";
Three three = new Three();
three.setId(6);
three.setName("QQ");
three.setValue(1000);
SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(three);
na.update(sql,sqlParameterSource);
}
}