spring学习篇之JdbcTemplate(3)

1.Spring JdbcTemplate基本使用

1.1 基本概述

它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装,spring框架为我们提供了很多的操作模板类,例如:操作关系型数据的Jdbc Template和HibernateTempalte,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。

1.2 JdbcTemplate开发步骤

1.导入Spring-jdbc和spring-tx坐标

	<!--JdbcTemplate-->
	<dependency>
    	<groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.0.2.RELEASE</version>
	</dependency>
	<!--事务-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

2.创建数据库表和实体

//创建数据库表
create table account(string name,double money);
//java代码中创建表的实体
public class Account{
	private String name;
	private double money;
	getset方法 tostring方法
	...
}

3.创建JdbcTemplate对象
4.执行数据库操作

//测试
@Test
public void test(){
	//创建数据源对象
	ComboPoolDataSource datasource = new ComboPoolDataSource();
	datasource.setDriverClass("com.mysql.jdbc.Driver")
	datasource.setJdbcUrl("jdbc:mysql://localhost:3306/test")
	datasource.setUser("root");
	datasource.setPassword("2019");
	JdbcTemplate jdbctemplate = new JdbcTemplate();
	//设置数据源对象
	jdbctemplate.setDataSource(datasource);
	//增删改
	int row1 = jdbctemplate.update(sql);
	//查
	jdbctemplate.query(sql);
}

上述步骤太过繁琐,我们可以将JdbcTemplate的创建交给spring容器

//applicationContext.xml
//数据源datasource
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
	<property name="jdbcUrl" value="jdbc:mysql://locallhost:3306/test"></property>
	<property name="user" value="root"></property>
	<property name="password value= "2019"></property>
</bean>
//jdbcTemplate
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="datasource" value="datasource"></property>
</bean>
//java代码
public void jdbcTest(){
	ApplicationContext app= new ApplicationContext("applicationContext.xml");
	JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
	...
}

上述还可以对jdbc的配置文件进行抽取

//新建jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://locallhost:3306/test
jdbc.username=root
jdbc.password=2019
//在applicationContext.xml中加载jdbc.properties文件
//命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
//加载
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
//替换写死的代码
com.mysql.jdbc.Driver 替换为${jdbc.driver}
jdbc:mysql://locallhost:3306/test 替换为 ${jdbc.url}
...

测试:

//
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestCURD{
	@Autowired 
	private JdbcTemplate jdbc; 
	@Test
	public void testUpdate(){
	//更新语句
		jdbc.update(sql);
		...
	//查询多个对象 BeanPropertyMapper<Account>(Account.class)中Account为实体类
	List<Account> list jdbc.query("select * from account",new BeanPropertyMapper<Account>(Account.class));
	//查询一个对象
	Account account = jdbc.queryForObject("select * from account where name=?",new BeanPropertyMapper<Account>(Account.class),"zhangsan")
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值