Spring-jdbc(JdbcTemplate)

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a458383896/article/details/85298344
  • spring 提供用于操作JDBC工具类,类似:DBUtils。
  • 依赖 连接池DataSource (数据源)
  • 环境搭建

  • 创建表

create database spring_day;
use spring_day;
create table t_user(
  id int primary key auto_increment,
  username varchar(50),
  password varchar(32)
);

insert into t_user(username,password) values('jack','1234');
insert into t_user(username,password) values('rose','5678');

     或者用下面代码创建表

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		
		
		JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
		jdbcTemplate.execute("create table t_user(+ "+ 
							" id int primary key auto_increment," +
							" username varchar(50)," + 
							"password varchar(32)");
		System.out.println("账户表  t_user 创建成功");
  • 导入jar包

  • javabean

public class User {
	
	private Integer id;
	private String username;
	private String password;
  • 使用api(了解)

public static void main(String[] args) {
		
		// 1 创建数据源 (连接池) dbcp
		BasicDataSource dataSource = new BasicDataSource();
		// * 基本 4 项
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/spring_day");
		dataSource.setUsername("root");
		dataSource.setPassword("123456");
		
		// 2 创建模板
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(dataSource);
		
		// 3 通过  api 操作
		jdbcTemplate.update("insert into t_user(username,password) values(?,?);", "tom" , "998");
			
}
  • 配置DBCP

             UserDao

private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void updata(User user) {
		String sql = "updata t_user set username=?, password=? where id =?";
		Object[] args = {user.getUsername(),user.getPassword(),user.getId()};
		jdbcTemplate.update(sql,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-4.3.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/spring_day"></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="UserDaoId" class="com.c_dbcp.UserDao">
  		<property name="jdbcTemplate" ref="jdbcTemplateId"></property>
  	</bean>
  
  
  
</beans>

            TestDBCP

@Test
	public void demo02 () {
		User user = new User();
		user.setId(1);
		user.setUsername("接客");
		user.setPassword("998");
		
		
		String xmlPath = "com/c_dbcp/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		
		
		UserDao userDao = (UserDao) applicationContext.getBean("UserDaoId");
		userDao.updata(user);
	}
  • 配置C3P0

             UserDao


public class UserDao {
	
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void updata(User user) {
		String sql = "update t_user set username=?, password=? where id =?";
		Object[] args = {user.getUsername(),user.getPassword(),user.getId()};
		jdbcTemplate.update(sql,args);
	}

	// 查询所有
	public List<User> findAll() {
		return jdbcTemplate.query("select * from t_user",new Object[]{},  new BeanPropertyRowMapper<User>(User.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-4.3.xsd">
  
  	<!-- 创建数据源  c3p0 -->
  	<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/spring_day"></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="UserDaoId" class="com.d_c3p0.UserDao">
  		<property name="jdbcTemplate" ref="jdbcTemplateId"></property>
  	</bean>
  
  
  
</beans>

          TestC3P0


public class TestC3P0 {
	@Test
	public void demo02 () {
		User user = new User();
		user.setId(1);
		user.setUsername("接客");
		user.setPassword("999");
		
		
		String xmlPath = "com/d_c3p0/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		
		
		UserDao userDao = (UserDao) applicationContext.getBean("UserDaoId");
		userDao.updata(user);
		List<User> allUser = userDao.findAll();
		for (User user : allUser) {
			System.out.println(user);
		}
	}
}
  • 使用JdbcDaoSupport

  •      dao层 

  •      spring配置文件

	<!-- 创建数据源  c3p0 -->
  	<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/spring_day"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
  	</bean>
  
  
  	<!-- 配置dao -->
  	<bean id="UserDaoId" class="com.e_jdbcdaosupport.UserDao">
  		<property name="dataSource" ref="dataSourceId"></property>
  	</bean>
  •     源码分析

  • 配置properties(先创建properties文件)

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring_day
jdbc.user=root
jdbc.password=123456
  •      Spring配置

<!-- 加载配置文件 
		"classpath:"前缀表示 src下
		在配置文件之后通过  ${key} 获得内容
	-->
	<context:property-placeholder location="classpath:com/itheima/f_properties/jdbcInfo.properties"/>
	
	<!-- 创建数据源 c3p0-->
	<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>

猜你喜欢

转载自blog.csdn.net/a458383896/article/details/85298344