JDBC Template

导包:

分别从  依赖 Spring jar包里找 

1.使用api

  • 创建表
package JDBC_Template;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class User {
	private int id;
	private String name;
	private String password;
	public static void main(String args[]) {
		//使用api连接
		BasicDataSource dataSource = new BasicDataSource();
		//四步
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setUsername("root");
		dataSource.setPassword("123456");
		//创建模板
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(dataSource);
		
		jdbcTemplate.update("insert into user_table(name,password) value(?,?)","强月城","qycssg!");
		
		
	}
}

 

2.配置DBCP

  • 创建bean
  • 创建dao
  • 配置文件
  • 测试

UserDao

package JDBC_Template_dbcp;

import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {
	//jdbc模板由spring注入
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public void update(User user) {
		String sqlString = "update user_table set name=?,password=? where id=?";
		Object[] args = {user.getName(),user.getPassword(),user.getId()};
		jdbcTemplate.update(sqlString, args);
		
	}
}

配置文件

<?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="dataSourceID" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>

<!-- 创建模板 -->
<bean id="jdbcTemplateID" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceID"></property>
</bean>

<!-- 配置dao  注入 -->
<bean id="Userdao" class="JDBC_Template_dbcp.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplateID"></property>
</bean>
</beans>

测试

package JDBC_Template_dbcp;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.qyc.zhushi.StudentWeb;

public class Test01 {
	@Test
	public void testdemo(){
		User user = new User();
		user.setId(1);
		user.setName("qyc");
		user.setPassword("123456");
		String xmlpath = "JDBC_Template_dbcp/Bean.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
		UserDao userDao = (UserDao) applicationContext.getBean("Userdao");
		userDao.update(user);
		
	}
}

 

3.配置C3P0

  • 修改创建数据源的一段
  • 该class的路径
<?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="dataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>

<!-- 创建模板 -->
<bean id="jdbcTemplateID" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceID"></property>
</bean>

<!-- 配置dao  注入 -->
<bean id="Userdao" class="JDBC_Template_c3p0.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplateID"></property>
</bean>
</beans>

查询

	public List<User> select() {
		
		return jdbcTemplate.query("select * from user_table",new BeanPropertyRowMapper<User>(User.class));
		
	}

测试

        List<User> list = userDao.select();
	for(User user2:list){
		System.out.println(user2);
	}
		

User [id=1, name=qyc, password=123456]
User [id=2, name=强月城, password=qycssg!]

 

 

4 使用JdbcDaoSupport

jdbc.properties文件

jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.jdbcUrl =jdbc:mysql://localhost:3306/test
jdbc.user = root
jdbc.password = 123456

配置文件

<?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_Template_JdbcDaoSupport/jdbc.properties"/>
<!-- 创建数据源 -->
<bean id="dataSourceID" 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.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!-- 配置dao  注入 -->
<bean id="Userdao" class="JDBC_Template_JdbcDaoSupport.UserDao">
<property name="dataSource" ref="dataSourceID"></property>
</bean>
</beans>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值