JAVA三阶段

JAVA三阶段

· 单词

  • client–>客户端、客户
  • exclusion–>排斥; 排除在外;
  • property–>性质、性能
  • constructor–>构造函数、构造器
  • arg–>(函数的)自变量
  • execute -->实行、执行
  • template -->模板

一、Maven项目构建管理工具

项目构建管理工具

Ant—> Maven—>Gradle(hibernate)

1、Maven安装与配置

1.1、解压压缩包

1.2、配置环境变量

1.3、cmd输出 mvn -version; 有信息提示则成功

1.4、配置settings.xml文件☆
1.4.1、更改本地仓库的路径:

D:/Plug_in/Maven_canku ————>存放Maven中从网络中下载的jar包与插件(plugin)

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
   <localRepository>D:/Plug_in/Maven_canku</localRepository>
1.4.2、镜像仓库配置
<mirror>
  <id>nexus-aliyun</id>
  <mirrorOf>central</mirrorOf>
  <name>Next aliyun</name>
  <url>https://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
1.5、eclipse中配置maven
1、User Setting 中设置maven的conf 文件路径

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sLJIHiSp-1605615357530)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201031214347731.png)]

2、工程文件命名约定:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T5efDu0j-1605615357537)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201031215252596.png)]

3、工程名命名、groupid[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-75ukRmB9-1605615357541)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201031215730032.png)]

2、创建一个maven工程

2.1源文件夹:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DroZneVz-1605615357546)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201031221654631.png)]

调整源文件夹顺序:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R8VJdYTY-1605615357550)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201031221457359.png)]

2.2、POM.xml 中配置
2.2.1 更改maven默认的 JDK1.5 的配置

法一、pom.xml中: 在pom.xml设置jdk的源码版本、编译版本

在中设置

   <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

在build -> plugins 中设置编译插件

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2k0uuFXk-1605615357552)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201031225203824.png)]

法二、 在下载的Maven目录中找到settings.xml文件在profiles标签加入如下配置

   <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile> 
2.2.2 依赖dependency☆

下载依赖网址

https://mvnrepository.com/?__cf_chl_captcha_tk__=9e41359936bac7d33b7910984068557a69b3182c-1604157563-0-AS56IhIEYIS4g4QpMKTso_6JNz8QMySjHcCPu7s8FIB-GYVSklCl1MQJ50dA1cnQi5PeAp5vwcRPJuN6QmPoD0d1wxMsWtdo6eEz8TAcT_G3auXbS9SxUsXrRKwkJ6r9CiBiu67--SPfb7MfnQMRc0nKRhIoCnEt5YgxsRNfq1okQL_Cz5vH6kY2gYCylJPpOWwMIMNmJcpYMv84G95LoMFz_ahoFbvQYZe9_U1foyKe90APMfXdzHXL1a3ES9-75gXTDmhMEHDZP2MyNczGqwXoC6JosT8qo4fn1AydxkXdvAyh7pxfYyr5vuqgLHliU-fzKkIgWlPiFZpD2aQwanwI4uN8n_E-GawOrNlFrhE82p9wv1_WzAzH-wLNDEy5-RDyhZEdlLI-rwNWDr_O3yt_Hg91j1YsiYItDRAElxiEc_j1xcMDfPTU7ewFg_qnrSuwEzJTpHJsqljLCXVLH74RyewzXd6DrTFNrfWWQMFmcRUJWiLcfiCLQaRs6HcaMQ2BC1f6Kn7ZERWhZYsfGNIBl7fj6IB0X8H4F5Rl61VV

*常用依赖:*☆

dom4j

mysql mysql-connector-java

gson

junit

javax.servlet javax.servlet-api

javax.servlet.jsp javax.servlet.jsp-api

jstl

2.2.3 Scope依赖关系作用域

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lgt6JG9k-1605615357555)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201031234050602.png)]

2.2.4 手动安装中央仓库中不存在的 jar包

​ 中央仓库中不存在的 jar包,可以通过手动安装到本地仓库,进行引用

​ A:手动安装

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JGjLqrPn-1605615357557)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201102210414698.png)]

​ B: 使用命令行安装

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pZKRF7K4-1605615357560)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201102202853523.png)]

2.2.5 依赖传递–依赖冲突

​ A工程:

image-20201102203359584

​ C工程:

image-20201102203600271
依赖冲突---------->自动判断:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hxmEwkxp-1605615357561)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201102221055363.png)]

image-20201102204058156
依赖冲突---------->手动判断:
1、排除依赖

​ D—引入B和C----但会自动依赖B的 log4j -(因为C工程有两层)---->排除B的依赖---->则引用C的依赖

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PqWHuAyt-1605615357563)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201104220642714.png)]

2、在pom.xml中指定具体引用版本☆

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QYS8aF0S-1605615357566)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201104235409845.png)]

2.2.6我的pom.xml配置☆
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.cyy3</groupId>
	<artifactId>Maven_1</artifactId>
	<version>0.0.1-SNAPSHOT</version><!--版本号-->
	<packaging>jar</packaging><!--打包为jar包-->
	
	<name>Maven_1</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<dependencies>
	
		<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>
	
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.47</version>
			<scope>runtime</scope>
		</dependency>
	
		<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<scope>compile</scope>
			<version>2.3.1</version>
		</dependency>
	
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	
		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
	
		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.2.1</version>
			<scope>provided</scope>
		</dependency>
	
		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
			<scope>compile</scope>
		</dependency>


	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
2.2.7 完整常用的依赖

​ Maven_D工程里

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bVaiym70-1605615357569)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201105000558529.png)]

2.3、Maven生命周期 命令操作
2.2.1 compile 编译
2.2.2 Test 测试
image-20201102211319432 image-20201102211431230 image-20201102211733926
2.3.3 package 打包
2.2.4 install 安装

​ 将 jar包安装到本地仓库中

2.2.5 deploy 发布

​ 将 jar包或者 war包,部署到容器(Tomcat)中

2.2.6 clean 清空

​ 清空target目录下的所有class

image-20201102220153665

3、安装私服仓库(服务器仓库)

3.1仓库分类:

​ 远程仓库: A:中央仓库

​ B:镜像仓库:阿里云

​ B:私服仓库:

​ C:本地仓库

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VPuEkEhm-1605615357571)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201105001414364.png)]

3.2、nexus安装与使用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SD1chg8h-1605615357573)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201105001953542.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MczSzYdF-1605615357575)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201105002006906.png)]

二、Spring框架

1、Spring的核心

​ 提供一种新的机制管理业务对象及其依赖关系

​ (IOC(控制反转/DI)、AOP(面向切面编程))

​ 高内聚、低耦合

​ Spring---->更多面向接口编程—>可以动态绑定

2、创建一个Spring框架

创建一个maven项目

2.1、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cyyproject1</groupId><artifactId>Spring_project_1_cyy</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>Spring_project_1_cyy Maven Webapp</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>
​			UTF-8
​		</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.6.RELEASE</version></dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.6.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>SpringProject_1</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-clean-plugin</artifactId>
				<version>3.1.0</version>
			</plugin>
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.0.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.0</version>
			</plugin>
			<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.0.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.1</version>
			</plugin>
			<plugin>
				<artifactId>maven-install-plugin</artifactId>
				<version>2.5.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-deploy-plugin</artifactId>
				<version>2.8.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-site-plugin</artifactId>
				<version>3.7.1</version>
			</plugin>
			<plugin>
				<artifactId>
						maven-project-info-reports-plugin
					</artifactId>
				<version>3.0.0</version>
			</plugin>
		</plugins>
	</build>

</project>
2.2、spring-config.xml

在src.main.resource 下创建一个spring配置文件:spring-config.xml

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1fnNobcD-1605615357578)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201106171839626.png)]

2.1、配置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5WxPXvHg-1605615357581)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201106205018130.png)]

3、IOC(控制反转)

3.1、含义:

​ 将设计好的类交给系统(框架)去控制,而不是在你的类中去控制

3.2、步骤:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fD8xdpDz-1605615357583)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201106235348640.png)]

image-20201107201624725

4、DI(依赖注入)

4.1、设值注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A1xBTPIY-1605615357586)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201107184628855.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NZyiyBNF-1605615357588)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201107193054321.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4brgyCev-1605615357590)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201107193039041.png)]

4.2、构造方法注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZwAZ1yfV-1605615357592)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201107202121473.png)]

4.3、同时

​ 设值注入与构造注入同时存在时,先执行构造注入,再执行设值注入

4.4、生命周期

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Hn9z94AE-1605615357594)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20201111181708773.png)]

5、Spring JDBC

5.1、设定数据源

spring-config.xml文件

<!--javabean对象 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 驱动名 -->
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver"></property>
		<!-- 数据库URL -->
		<property name="url"
			value="jdbc:mysql://127.0.0.1:3306/projectdata?useOldAliasMetadataBehavior=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"></property>
		<!-- 用户名 -->
		<property name="username" value="root"></property>
		<!-- 密码 -->
		<property name="password" value="root"></property>

	</bean>
5.2、获取数据
test1 IOC方法
package com.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

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

public class Test_1 {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		DataSource dataSource = (DataSource) context.getBean("dataSource");
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			System.out.println("dataSource = " + dataSource);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			conn = null;
			context=null;
		}
	}

}
test2 DI方法 (注入dataSource)

先配置test_2的对象:

设值注入

<bean id="test_2" class="com.jdbc.Test_2">
	<property name="dataSource" ref="dataSource"></property>
</bean>

创建SpringUtil工具类,context只用创建一次

package com.util;

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

public class SpringUtil {

	private static ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
	public static Object getBean(String beanname) {
		if (beanname==null||beanname.equalsIgnoreCase("")) {
			return null;
		}
		return context.getBean(beanname);
	}
}

测试

package com.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.util.SpringUtil;

public class Test_2 {
	//设值注入
	private DataSource dataSource;
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public void testConn() {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "select * from t_userinfo"; 
	
		try {
			conn = this.dataSource.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				System.out.println(rs.getInt("userid")+"   "+rs.getString("username")+"   "+rs.getString("truename"));
			}
			System.out.println("conn = " + conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		conn = null;
	}
	
	public static void main(String[] args) {
		Test_2 test_2 = (Test_2) SpringUtil.getBean("test_2");
		test_2.testConn();
	}

}
test3模板 (注入dataSource)
image-20201116190506198
package com.jdbc;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.util.SpringUtil;

public class Test_3 {
	

	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	/**
	 * 
	 * 方法功能说明:使用jdbcTemplate
	 * 创建日期:2020年11月16日下午7:07:46
	 * 作者:cyy
	 * @return void
	 */
	public void testConn() {
		//构造JdbcTemplate对象时构造注入dataSource
		//template就知道要操作哪个数据库了
		JdbcTemplate template = new JdbcTemplate(this.dataSource);
		
		String sql = "Select * from t_userinfo order by userid asc";
		
		//直接使用模板中的方法
		 List<Map<String, Object>> userList = template.queryForList(sql);
		 for (Map<String, Object> rowMap : userList) {
			System.out.println(rowMap.get("userid")+"  "+rowMap.get("username"));
		}
		 //模板会自动关闭数据
	
	}
	
	public static void main(String[] args) {
	Test_3 test_3 = (Test_3)SpringUtil.getBean("test_3");
	test_3.testConn();
	}

}
<bean id="test_3" class="com.jdbc.Test_3">
	<property name="dataSource" ref="dataSource"></property>
</bean>
test4 模板 (注入dataSource和JdbcTemplate)
<!--构造JdbcTemplate对象,向JdbcTemplate注入dataSource-->
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"></property>
</bean>
<!--向test_4中注入template-->
<bean id="test_4" class="com.jdbc.Test_4">
	<property name="template" ref="JdbcTemplate"></property>
</bean>
package com.jdbc;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.util.SpringUtil;

public class Test_4 {
	//设值注入
	private JdbcTemplate template;
	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}
	
	/**
	 * 方法功能说明:使用jdbcTemplate 注入模板
	 */
	public void testConn() {
		String sql = "Select * from t_userinfo order by userid asc";	
		//直接使用模板中的方法
		 List<Map<String, Object>> userList = template.queryForList(sql);
		 for (Map<String, Object> rowMap : userList) {
			System.out.println(rowMap.get("userid")+"  "+rowMap.get("username"));
		}
		 //模板会自动关闭数据
	}
	public static void main(String[] args) {
	Test_4 test_4 = (Test_4)SpringUtil.getBean("test_4");
	test_4.testConn();
	}
}
5.3、JDBC的xml配置
5.4、JdbcTemplate基本操作
DDL
package com.jdbc;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.util.SpringUtil;

public class Test_5_DDL {
	

	/**
	 * 执行DDL语句
	 */
	private JdbcTemplate template;
	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}
	
	/**
	 * 方法功能说明:创建表
	 */
	public void testCreat() {
		String sql = "create table t_temp(temp_id int,temp_name varchar(20))";
		template.execute(sql);
	}
	/**
	 * 方法功能说明:改表
	 */
	public void testAlter() {
		String sql = "alter table t_temp add temp_pwd varchar(10)";
		template.execute(sql);
	}
	/**
	 * 方法功能说明:删除表
	 */
	public void testDrop() {
		String sql = "drop table t_temp";
		template.execute(sql);
	}
	
	public static void main(String[] args) {
	Test_5_DDL test_5 = (Test_5_DDL)SpringUtil.getBean("test_5");

//	test_5.testCreat();
	test_5.testAlter();
//	test_5.testDrop();
	}
}
DML
Statement
package com.jdbc;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.util.SpringUtil;

public class Test_6_DML {

	/**
	 * 执行DML语句,相当于jdbc中statement对象来执行SQL语句
	 */
	private JdbcTemplate template;
	
	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}
	
	/**
	 * 方法功能说明:创建表
	 */
	public void insert() {
		String sql = "insert into t_temp(temp_id,temp_name,temp_pwd)values(2,'aaa','aaa') ";

//		template.execute(sql);
		int rowCount = template.update(sql);
		System.out.println("添加所影响的行数 = " + rowCount);
	}

	public void update() {
		String sql = "update t_temp set temp_id = 55,temp_name = 'aa55' where temp_id in(1,2)" ;
		int rowCount = template.update(sql);
		System.out.println("更新所影响的行数 = " + rowCount);
	}
	
	public void delete() {
		String sql = "delete from t_temp where temp_id in (1,3)";
		int rowCount = template.update(sql);
		System.out.println("删除所影响的行数 = " + rowCount);
	}
	
	public static void main(String[] args) {
		Test_6_DML test_6 = (Test_6_DML) SpringUtil.getBean("test_6");

//		test_6.insert();
//		test_6.update();
		test_6.delete();
	}
}
<bean id="test_6" class="com.jdbc.Test_6_DML">
	<property name="template" ref="JdbcTemplate"></property>
</bean>
PreparedStatement

1、常用、推荐

image-20201117191633934
 	public void insert_1() {
		String sql = "insert into t_temp(temp_id,temp_name,temp_pwd)values(?,?,?) ";
//		template.execute(sql);
		Object[] paramArrays = {4,"dd","dddd"};
		int rowCount = template.update(sql, paramArrays);
		System.out.println("添加所影响的行数 = " + rowCount);
	}

2、常用

image-20201117192419055
public void insert_2() {
	String sql = "insert into t_temp(temp_id,temp_name,temp_pwd)values(?,?,?) ";
	//匿名内部类,new PreparedStatementSetter()对象只为这个方法服务
	int rowCount = template.update(sql, new PreparedStatementSetter() {
		
		public void setValues(PreparedStatement ps) throws SQLException {
			// TODO Auto-generated method stub
			ps.setInt(1, 5);
			ps.setString(2, "ee");
			ps.setString(3, "ee44555");
		}
	});
	System.out.println("添加所影响的行数 = " + rowCount);
}

3、

image-20201117195722098
public void insert_3() {
	//提升为全局变量       /或成员变量
	final String sql = "insert into t_temp(temp_id,temp_name,temp_pwd)values(?,?,?) ";
	//匿名内部类,new PreparedStatementSetter()对象只为这个方法服务
	int rowCount = template.update(new PreparedStatementCreator() {
		
		public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
			//匿名类不能调用局部变量
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, 6);
			pstmt.setString(2, "ee");
			pstmt.setString(3, "ee44555");
			return pstmt;
		}
	});
	System.out.println("添加所影响的行数 = " + rowCount);
}

完整代码

package com.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.util.SpringUtil;

public class Test_7_DML_prepared {

	/**
	 * 执行DML语句,相当于jdbc中statement对象来执行SQL语句
	 */
	private JdbcTemplate template;
	
	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}public void insert_1() {
		String sql = "insert into t_temp(temp_id,temp_name,temp_pwd)values(?,?,?) ";

//		template.execute(sql);
		Object[] paramArrays = {4,"dd","dddd"};
		int rowCount = template.update(sql, paramArrays);
		System.out.println("添加所影响的行数 = " + rowCount);
	}
	

	public void insert_2() {
		String sql = "insert into t_temp(temp_id,temp_name,temp_pwd)values(?,?,?) ";
		//匿名内部类,new PreparedStatementSetter()对象只为这个方法服务
		int rowCount = template.update(sql, new PreparedStatementSetter() {
			
			public void setValues(PreparedStatement ps) throws SQLException {
				// TODO Auto-generated method stub
				ps.setInt(1, 5);
				ps.setString(2, "ee");
				ps.setString(3, "ee44555");
			}
		});
		System.out.println("添加所影响的行数 = " + rowCount);
	}
	
	public void insert_3() {
		//提升为全局变量       /或成员变量
		final String sql = "insert into t_temp(temp_id,temp_name,temp_pwd)values(?,?,?) ";
		//匿名内部类,new PreparedStatementSetter()对象只为这个方法服务
		int rowCount = template.update(new PreparedStatementCreator() {
			
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				//匿名类不能调用局部变量
				PreparedStatement pstmt = con.prepareStatement(sql);
				pstmt.setInt(1, 6);
				pstmt.setString(2, "ee");
				pstmt.setString(3, "ee44555");
				return pstmt;
			}
		});
		System.out.println("添加所影响的行数 = " + rowCount);
	}
	
	public void update() {
		String sql = "update t_temp set temp_id = 55,temp_name = 'aa55' where temp_id in(1,2)" ;
		int rowCount = template.update(sql);
		System.out.println("更新所影响的行数 = " + rowCount);
	}
	
	public void delete() {
		String sql = "delete from t_temp where temp_id in (1,3)";
		int rowCount = template.update(sql);
		System.out.println("删除所影响的行数 = " + rowCount);
	}
	
	public static void main(String[] args) {
		Test_7_DML_prepared test_7 = (Test_7_DML_prepared) SpringUtil.getBean("test_7");

//		test_7.insert_1();
//		test_7.insert_2();
		test_7.insert_3();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值