Spring的JDBC的模板的使用

Spring是EE开发的一站式框架,有EE开发的每一层模板

一.JDBC模板的使用入门

1.创建数据库和表

SHOW DATABASES
USE hero
SHOW TABLES
CREATE TABLE account (id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),money DOUBLE)
SELECT * FROM account

2.创建JDBC模板

public static void main(String[]args){
        //创建连接池
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///hero");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        //创建JDBC模板
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("insert into account values(null,?,?)","张三",1000.0);
    }

 

二.将连接池和模板交给spring管理

1.在spring.xml中进行配置

<!--配置连接池-->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     		<!--属性注入-->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql:///hero"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>


	<!--配置spring的JDBC的模板-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

 

2.编写配置测试类

 @Resource(name="jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    public static void main(String[]args){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
        jdbcTemplate.update("insert into account values(null,?,?)","lisi",4000.0);

    }

 

三.使用c3p0

<!--配置C3P0-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///hero"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>

 

 

四.抽取配置到属性文件

1.在resources目录下创建配置文件 jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///hero
jdbc.username=root
jdbc.password=123456

 

2.在spring.xml中进行配置

<!--引入属性文件-->
	<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
	<!--配置C3P0-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!--配置spring的JDBC的模板-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

 

五.使用JDBC完成增删改查的功能

1.增

jdbcTemplate.update("insert into account values(null,?,?)","zhaoliu",4000.0);

 

2.删

jdbcTemplate.update("delete from account where id=?",7);

3.改

jdbcTemplate.update("update account set name=?,money=? where id=?","anny",3000,4);

 

4.查

查询某个属性

String name = jdbcTemplate.queryForObject("select name from account where id=?",String.class,6);
        System.out.println(name);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值