Spring 基础知识四分析

不点 阅读:251 2021-03-31 14:14:37 评论:0

第一 Spring JdbcTemplate

1.1 JdbcTemplate 概述

       spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装

1.2 JdbcTemplate 环境搭建

1.2.1 spring+mysql8 +c3p0 版本基础jar 包依赖:

<!--spring jdbctemplate 依赖jar 包 --> 
		<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> 
		<!-- 数据库连接池 --> 
		<dependency> 
			<groupId>c3p0</groupId> 
			<artifactId>c3p0</artifactId> 
			<version>0.9.1.2</version> 
		</dependency> 
		<!--mysql8 驱动包 --> 
		<dependency> 
			<groupId>mysql</groupId> 
			<artifactId>mysql-connector-java</artifactId> 
			<version>8.0.15</version> 
		</dependency>

spring 配置文件(c3p0.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"> 
 
	<!-- 配置一个数据库的操作模板:JdbcTemplate --> 
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
 
 
	<bean id="dataSource" 
		class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
		<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property> 
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/myblog?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true"></property> 
		<property name="user" value="root"></property> 
		<property name="password" value="123456"></property> 
	</bean> 
 
</beans>

演示代码:

package com.zzg.jdbc.template.c3p0; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.jdbc.core.JdbcTemplate; 
 
public class C3p0Test { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		//1.获取 Spring 容器 
		ApplicationContext ac = new ClassPathXmlApplicationContext("c3p0.xml"); 
		//2.根据 id 获取 bean 对象 
		JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
		//3.执行操作 
		jt.execute("insert into blog_tag(name) values ('Java架构')"); 
	} 
 
} 

1.2.2 spring+mysql8 +DBCP版本基础jar 包依赖:

<!--spring jdbctemplate 依赖jar 包 --> 
		<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> 
		<!-- 数据库连接池 dbcp --> 
		<dependency> 
			<groupId>org.apache.commons</groupId> 
			<artifactId>commons-dbcp2</artifactId> 
			<version>2.5.0</version> 
		</dependency> 
		<dependency> 
			<groupId>org.apache.commons</groupId> 
			<artifactId>commons-pool2</artifactId> 
			<version>2.6.0</version> 
		</dependency> 
		<!--mysql8 驱动包 --> 
		<dependency> 
			<groupId>mysql</groupId> 
			<artifactId>mysql-connector-java</artifactId> 
			<version>8.0.15</version> 
		</dependency>

spring 配置文件(dbcp.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"> 
 
	<!-- 配置一个数据库的操作模板:JdbcTemplate --> 
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
 
	<!-- 配置一个数据库的数据源:BasicDataSource --> 
	<bean id="dataSource" 
		class="org.apache.commons.dbcp2.BasicDataSource"> 
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> 
		<property name="url" value="jdbc:mysql://localhost:3306/myblog?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true"></property> 
		<property name="username" value="root"></property> 
		<property name="password" value="123456"></property> 
	</bean> 
 
</beans>

演示代码:

package com.zzg.jdbc.template.dbcp; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.jdbc.core.JdbcTemplate; 
 
public class DbcpTest { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		// 1.获取 Spring 容器 
		ApplicationContext ac = new ClassPathXmlApplicationContext("dbcp.xml"); 
		// 2.根据 id 获取 bean 对象 
		JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
		// 3.执行操作 
		jt.execute("insert into blog_tag(name) values ('前端架构')"); 
	} 
 
} 

1.2.3 spring+mysql8 +spring 内置数据源 版本基础jar 包依赖:

依赖jar 文件在此省略

spring 配置文件(basic.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"> 
 
	<!-- 配置一个数据库的操作模板:JdbcTemplate --> 
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
 
	<!-- spring 默认内置数据源 --> 
	<bean id="dataSource" 
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<property name="driverClassName" 
			value="com.mysql.cj.jdbc.Driver"></property> 
		<property name="url" value="jdbc:mysql://localhost:3306/myblog?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true"></property> 
		<property name="username" value="root"></property> 
		<property name="password" value="123456"></property> 
	</bean> 
 
</beans>

演示代码:

package com.zzg.jdbc.template.basic; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.jdbc.core.JdbcTemplate; 
 
public class BasicTest { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		// 1.获取 Spring 容器 
		ApplicationContext ac = new ClassPathXmlApplicationContext("basic.xml"); 
		// 2.根据 id 获取 bean 对象 
		JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
		// 3.执行操作 
		jt.execute("insert into blog_tag(name) values ('Java基础知识')"); 
	} 
 
} 

1.2.4 spring 读取配置文件信息方法(*.properties)

第一种方式:

定义mysql 数据库连接配置文件(jdbc.properties)

jdbc.driverClass=com.mysql.cj.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/myblog?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true 
jdbc.username=root 
jdbc.password=123456

spring 引入外部配置文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="classpath:jdbc.properties"/> 
</bean>

演示实例:

<?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 
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="location" value="classpath:jdbc.properties" /> 
	</bean> 
 
	<!-- 配置一个数据库的操作模板:JdbcTemplate --> 
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
 
	<!-- spring 默认内置数据源 --> 
	<bean id="dataSource" 
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<property name="driverClassName" value="${jdbc.driverClass}"></property> 
		<property name="url" value="${jdbc.url}"></property> 
		<property name="username" value="${jdbc.username}"></property> 
		<property name="password" value="${jdbc.password}"></property> 
	</bean> 
 
</beans>

第二种方式:

spring 扫描引入资源配置文件(*.properties)

<context:property-placeholder location="classpath:jdbc.properties"/>

演示实例:

<?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"> 
	<!-- 
	<bean 
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="location" value="classpath:jdbc.properties" /> 
	</bean>  --> 
	<context:property-placeholder location="classpath:jdbc.properties"/> 
 
	<!-- 配置一个数据库的操作模板:JdbcTemplate --> 
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
 
	<!-- spring 默认内置数据源 --> 
	<bean id="dataSource" 
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<property name="driverClassName" value="${jdbc.driverClass}"></property> 
		<property name="url" value="${jdbc.url}"></property> 
		<property name="username" value="${jdbc.username}"></property> 
		<property name="password" value="${jdbc.password}"></property> 
	</bean> 
 
</beans>

1.3 JdbcTemplate 增删改查

spring的配置文件(common.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" 
	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"> 
	<!-- 
	<bean 
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="location" value="classpath:jdbc.properties" /> 
	</bean>  --> 
	<context:property-placeholder location="classpath:jdbc.properties"/> 
 
	<!-- 配置一个数据库的操作模板:JdbcTemplate --> 
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
 
	<!-- spring 默认内置数据源 --> 
	<bean id="dataSource" 
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<property name="driverClassName" value="${jdbc.driverClass}"></property> 
		<property name="url" value="${jdbc.url}"></property> 
		<property name="username" value="${jdbc.username}"></property> 
		<property name="password" value="${jdbc.password}"></property> 
	</bean> 
 
</beans>

核心功能代码:

package com.zzg.jdbc.template; 
 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.List; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.jdbc.core.JdbcTemplate; 
import org.springframework.jdbc.core.RowMapper; 
 
import com.alibaba.fastjson.JSONObject; 
 
public class Common { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		// 1.获取 Spring 容器 
				ApplicationContext ac = new ClassPathXmlApplicationContext("common.xml"); 
				// 2.根据 id 获取 bean 对象 
				JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
				 
				// 调用新增方法 
				// insert(jt, "大数据学习教程"); 
				 
				// 调用修改方法 
				// update(jt,"黑马程序员-大数据学习教程", 7); 
				 
				// 调用删除方法 
				// delete(jt, 7); 
				// 调用查询方法 
				select(jt); 
	} 
	 
	public static void insert(JdbcTemplate jt, String name) { 
		jt.execute("insert into blog_tag(name) values ('" + name + "')"); 
	} 
	 
	public static void update(JdbcTemplate jt, String name, Integer id) { 
		jt.update("update blog_tag set name = ? where id = ?", name, id); 
	} 
	 
	public static void select(JdbcTemplate jt) { 
		 
	List<JSONObject> list =	jt.query("select * from blog_tag", new RowMapper() { 
 
			@Override 
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException { 
				// TODO Auto-generated method stub 
				JSONObject object = new JSONObject(); 
				Integer id = rs.getInt(1); 
				String name = rs.getString(2); 
				 
				object.put("id", id); 
				object.put("name", name); 
				return object; 
			} 
			 
		}); 
	 
		System.out.println(list.toString()); 
	} 
	 
	public static void delete(JdbcTemplate jt, Integer id) { 
		jt.update("delete from blog_tag where id = ?", id); 
	} 
 
} 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号