Spring学习历程---十分钟入门教程JdbcTemplate

37 篇文章 0 订阅
29 篇文章 2 订阅

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.java

package 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。

TableTest 注释部分建立一个表,然后插入数据。
updateTest 更新数据,打印出影响几条数据。

queryTest      使用query函数,ParameterizedBeanPropertyRowMapper.newInstance(User.class)) 用这种方式得到RowMapper , 并传入返回参数的类型。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值