Spring中的JDBCTemplate

src\dayday\JDBCTestTest

package dayday;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
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.Connection;
import java.sql.SQLException;
import java.util.*; import static javafx.scene.input.KeyCode.S;
import static javafx.scene.input.KeyCode.T;
import static org.junit.Assert.*; /**
* Created by I am master on 2016/12/5.
*/
public class JDBCTestTest {
private ApplicationContext ctx=null;
private JdbcTemplate jdbcTemplate=null;
private UserDao userDao=null;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate=null;
{
ctx=new ClassPathXmlApplicationContext("beans.xml");
jdbcTemplate=ctx.getBean("template",JdbcTemplate.class);
userDao=ctx.getBean(UserDao.class);
namedParameterJdbcTemplate=ctx.getBean("namedParameterJdbcTemplate",NamedParameterJdbcTemplate.class);
} @Test
public void testDataSource() throws SQLException {
DataSource dataSource=ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
@Test
/*
数据库的增删改查操作
*/
public void testUpdate(){
String sql="update bankuser set password=? where name=? ";
jdbcTemplate.update(sql,"daydayup01","dayday");
}
/*
执行批量处理
*/
@Test
public void testbatchUpdate(){
String sql="insert into bankuser values(?,?,?)";
List<Object[]> batcharrays=new ArrayList<>();
batcharrays.add(new Object[]{"A","1","100"});
batcharrays.add(new Object[]{"B","2","200"});
batcharrays.add(new Object[]{"C","3","300"});
jdbcTemplate.batchUpdate(sql,batcharrays);
}
/*
*从数据库中读取一条记录,并实际返回一个对象
*注意不是调用 queryForObject(String sql, Class<Employee> requiredType, Object... args) 方法!
**/
@Test
public void testqueryForObject(){
String sql="select password , property from bankuser where name=?";
RowMapper<User>rowMapper=new BeanPropertyRowMapper<>(User.class);
User user=jdbcTemplate.queryForObject(sql,rowMapper,"dayday");
System.out.println(user);
}
/*
* 查到实体类的集合
*/
@Test
public void testqueryForList(){
String sql="select password , name from bankuser where property>?";
RowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
List<User> users=jdbcTemplate.query(sql,rowMapper,200);
System.out.println(users); }
/*
* 查询单列的值,或做统计查询
* */
@Test
public void testqueryForObject1(){
String sql="select count(name) from bankuser";
long count=jdbcTemplate.queryForObject(sql,long.class);
System.out.println(count);
}
@Test
public void testUserDao(){
System.out.println(userDao.get("dayday"));
}
/*
* 测试JdbcTemplate的具名参数
* 好处:若有多个参数,则不用再去对应位置,直接对应参数名
* 坏处:较为麻烦*/
@Test
public void testNamedParameterJdbcTemplate(){
String sql="insert into bankuser value(:name,:password,:property)";
Map<String,Object> paramMap=new HashMap<>();
paramMap.put("name","DD");
paramMap.put("password","4");
paramMap.put("property","400");
namedParameterJdbcTemplate.update(sql,paramMap);
}
/*
* 使用具名参数时可以使用update(String sql,SqlParameterSource paramSource)方法进行更新操作
* 1.sql语句中的参数名和类的属性一致
* 2.使用SqlParameterSource的BeanPropertySqlParameterSource实现类作为参数
* */
@Test
public void testNamedParameterJdbcTemplate2(){
String sql="insert into bankuser value(:name,:password,:property)";
User user=new User();
user.setName("E");
user.setPassword("5");
user.setProperty("500");
SqlParameterSource sqlParameterSource=new BeanPropertySqlParameterSource(user);
namedParameterJdbcTemplate.update(sql,sqlParameterSource);
}
}

src\dayday\UserDAO.java

src\dayday\User.java

package dayday;

/**
* Created by I am master on 2016/12/5.
*/
public class User {
private String name;
private String password;
private String property;
public void setName(String name){
this.name=name;
}
public void setPassword(String password){
this.password=password;
}
public void setProperty(String property){
this.property=property;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String getProperty(){
return property;
}
public String toString(){
return "[ name="+name+" password="+password+" property="+property+"]";
}
}

src\jdbc.property

jdbc.user=root
jdbc.password=root
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/bank
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.initPoolSize=5
jdbc.maxPoolSize=10

src\beans.xml

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--导入资源文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置c3p0数据源-->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!--配置Spring中的jdbcTemplate-->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"></property>
</bean>
<context:component-scan base-package="dayday"></context:component-scan>
<!--配置NamedParameterJdbcTemplate,该对象可以使用具名参数,其无无参构造器,所以必须为其构造器指定参数-->
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="datasource"></constructor-arg>
</bean>
</beans>
上一篇:BZOJ_2017_[Usaco2009 Nov]硬币游戏_博弈论+DP


下一篇:通过Git Gui Here上传本地项目到GitHub上