Spring提出JdbcTemplate类作为数据库访问类,该类是Spring框架数据抽象的基础,更高层次的抽象类是构建于JdbcTemplate类之上。所以掌握了JdbcTemplate就相当于掌握了SpringJDBC数据库操作的核心。
JdbcTemplate常用方法:(假设jTemplate是其对象,先别管怎么来的,先要大概了解它是干啥的)
execute(string sql); 执行一条 sql 语句。
update(String sql):执行一条sql语句,并返回影响的行数
update(string sql,Object...args):要求参数不为空,返回影响的行数,后面args为前面sql的参数
query(String sql,Object[]args,RowMapper rowMapper):该方法返回List类型的数据,args为前面sql的参数,RowMapper传入查询结果返回的类型
queryForObject(String sql,Object[[]args,RowMapper rowMapper): 返回一个Object
queryForList(String sql,Object []args,class<T>elementType):返回一个类型为T的List,作为查询结果。
下面是例子:
1.导包
2.你要先建一个spring数据库。
配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="fpy/.123698741"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="myJdbcTemplate" class="cn.wanhao.MyJdbcTemplate">
<property name="template" ref="jdbcTemplate"></property>
</bean>
<bean id="user" class="cn.wanhao.User"></bean>
</beans>
配置myJdbcTemplate这个Bean,主要是为了使用方便,虽然在这个项目中没有体现出来。
配置User这个Bean,是与数据库中的表相对应。
MyJdbcTemplate.javapackage cn.wanhao;
import org.springframework.jdbc.core.JdbcTemplate;
public class MyJdbcTemplate {
private JdbcTemplate template;
public JdbcTemplate getTemplate() {
return template;
}
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
}
User.java
package cn.wanhao;
public class User {
private int id;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
springTest.java
package junit.Test;
import java.util.List;
import org.junit.Before;
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.simple.ParameterizedBeanPropertyRowMapper;
import cn.wanhao.MyJdbcTemplate;
import cn.wanhao.User;
public class springTest {
JdbcTemplate jTemplate=null;
@Before
public void before()
{
String path="cn/wanhao/beans.xml";
ApplicationContext application=new ClassPathXmlApplicationContext(path);
MyJdbcTemplate template=(MyJdbcTemplate) application.getBean("myJdbcTemplate");
jTemplate=template.getTemplate();
}
@Test
public void TableTest()
{
// jTemplate.execute("create table user(user_id int primary key auto_increment, username varchar(30));");
String sql="insert into t_user(username) value(?);";
Object obj[]={"wh"};
jTemplate.update(sql,obj);
}
@Test
public void updateTest()
{
String sql="update user set username=? where id=?";
int hang=jTemplate.update(sql,new Object[]{"wh2",2});
System.out.println(hang);
}
@Test
public void queryTest()
{
// List<User>users=null;
// String psc="select * from user";
// users=jTemplate.query(psc, ParameterizedBeanPropertyRowMapper.newInstance(User.class));
// for(User u:users)
// {
// System.out.println(u.getId()+":"+u.getUsername());
// }
String sql="select * from user where id="+1;
System.out.println(jTemplate.queryForObject(sql, ParameterizedBeanPropertyRowMapper.newInstance(User.class)).getId());
}
}
String path="cn/wanhao/beans.xml";注意不要写成 cn.wanhao.beans.xml。
updateTest 更新数据,打印出影响几条数据。
queryTest 使用query函数,ParameterizedBeanPropertyRowMapper.newInstance(User.class)) 用这种方式得到RowMapper , 并传入返回参数的类型。