当前位置: 首页 > news >正文

新闻门户网站建设软件开发培训学校三八妇女节

新闻门户网站建设,软件开发培训学校三八妇女节,微网站开发系统,南京市溧水城市建设集团网站尚硅谷2023最新版Spring6课程_bilibili 1 Spring 【强制】Spring是什么#xff1f; 1) Spring是一款主流的Java EE轻量级开源框架。 轻量级#xff1a;体积很小#xff0c;且不需要依赖于其他组件。 2) 狭义的Spring。 Spring Framework。 3) 广义的Spring。 以Spring F…尚硅谷2023最新版Spring6课程_bilibili 1 Spring 【强制】Spring是什么 1) Spring是一款主流的Java EE轻量级开源框架。 轻量级体积很小且不需要依赖于其他组件。 2) 狭义的Spring。 Spring Framework。 3) 广义的Spring。 以Spring Framework为核心的Spring技术栈。 4) Spring的核心思想。 提供各种服务以帮助我们简化基于POJO的Java应用程序开发。 【强制】Spring模块组成。 1) Spring Core。 spring-core、spring-beans、spring-context、spring-expression。 2) Spring AOP。 spring-aop、spring-aspects、spring-instrument。 3) Spring Data Access。 spring-jdbc、spring-orm、spring-oxm、spring-jms、spring-tx。 4) Spring Web。 spring-web、spring-webmvc、spring-websocket、spring-webflux。 5) Spring Message。 spring-messaging。 6) Spring Test。 spring-test。 2 Spring IoC 2.1 概念 【强制】IoC。 1) IoCInversion of Control控制反转是一种设计思想。 2) IoC的理念。 让别人为你服务。原先是由程序员自己来包办关于对象的一切现在让IoC Service ProviderIoC服务提供者来负责。 3) IoC可以给我们带来什么 用一句话来概括就是IoC是一种可以帮助我们解耦各业务对象间依赖关系的对象绑定方式。 【强制】DI。 1) DIDependency Injection依赖注入。 和IoC是一回事只是观察的角度不同。 2) 依赖注入的实现方式。 构造方法注入、setter方法注入。 【强制】IoC容器。 1) IoC容器是Spring对IoC Service Provider的实现。 IoC Service ProviderIoC服务提供者是一个概念。 IoC Service Provider主要负责管理Java对象的实例化和初始化控制对象与对象之间的依赖关系。 2) Spring提供了两种容器类型BeanFactory和ApplicationContext。 BeanFactory是IoC容器的基本实现提供了 对象创建管理和依赖注入服务 两种特性。 ApplicationContext是BeanFactory的子接口在BeanFactory的基础上提供了 统一资源加载策略、国际化信息支持、容器内事件发布功能 等高级特性。 我们开发人员可以无脑选择ApplicationContext。 3) ApplicationContext接口的常用实现类。 ClassPathXmlApplicationContext。 FileSystemXmlApplicationContext。 ConfigurableApplicationContext。 WebApplicationContext。 【强制】Bean。 1) IoC容器管理的Java对象称为Spring Bean。 2) bean的作用域。 singleton在IoC容器中有且仅有一个实例默认。 prototype在IoC容器中可以有多个实例每次获取都返回一个新的实例。 request在一个请求范围内有效。 session在一个会话范围内有效。 3) bean的生命周期简化版本。 1. 创建bean。 以反射或者CGLIB动态字节码生成方式默认后者获得目标类实例。 以BeanWrapper对目标类实例进行包裹返回BeanWrapper实例。 2. 给bean设置属性包括Aware相关依赖。 3. 调用bean的后置处理器-初始化前方法。 执行自定义的BeanPostProcessor.postProcessBeforeInitialization方法。 4. 初始化bean。 执行自定义的init-method。 5. 调用bean的后置处理器-初始化后方法。 执行自定义的BeanPostProcessor.postProcessAfterInitialization方法。 6. 使用bean。 7. 销毁bean。 关闭容器时执行自定义的destory-method。 2.2 实践 【强制】获取bean。 1) 配置代码。 bean idmyBean classcom.mypath.MyBean/bean 2) Java代码。 ApplicationContext context new ClassPathXmlApplicationContext(bean.xml); // 1.根据ID获取bean MyBean myBean1 (MyBean)context.getBean(myBean); // 2.根据类型获取bean(要求IoC容器中指定类型的bean有且仅有1个) MyBean myBean2 context.getBean(MyBean.class); // 3.根据ID和类型获取bean MyBean myBean3 context.getBean(myBean, MyBean.class); 【强制】依赖注入。 1) 构造方法注入。 bean idstudent classcom.mypath.Studentconstructor-arg index0 valuellxz/constructor-argconstructor-arg index1 refdream/constructor-arg /bean bean iddream classcom.mypath.Dream/bean 2) setter方法注入。 bean idstudent classcom.mypath.Studentproperty namename valuellxz/propertyproperty namedream refdream/property /bean bean iddream classcom.mypath.Dream/bean 【强制】自动绑定。 1) 手动绑定。 使用constructor-arg或property为bean的属性注入依赖。 2) 自动绑定也叫自动装配。 通过bean的autowire属性可以指定采用何种类型的自动绑定模式。 Spring提供了5种自动绑定模式no、byName、byType、constructor、autodetect。默认为no手动绑定。 3) 代码示例byName。 Student.book自动绑定bean book。 class Student {private Book book;public void setBook(Book book) {this.book book;} }class Book { } bean idstudent classcom.mypath.Student autowirebyName/bean bean idbook classcom.mypath.Book/bean 【推荐】FactoryBean。 1) 简介。 FactoryBean是Spring提供的一种整合第三方框架的常用机制。配置一个FactoryBean类型的bean在获取bean的时候得到的并不是class属性配置的类的实例而是getObject()方法的返回值。工厂模式。 2) MyFactoryBean实现FactoryBean接口。 public class MyFactoryBean implements FactoryBeanMyBean {Overridepublic MyBean getObject() throws Exception {return new MyBean();}Overridepublic class? getObjectType() {return MyBean.class;} } 3) 配置bean。 bean idmyBean classcom.mypath.MyFactoryBean/bean 4) 获取bean。 ApplicationContext context new ClassPathXmlApplicationContext(spring-factorybean.xml); MyBean myBean (MyBean) context.getBean(myBean); 2.3 基于注解开发 【强制】开启组件扫描。 1) 开启组件扫描。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 开启组件扫描 --context:component-scan base-packagecom.mypath.mypackage/ /beans 2) 组件扫描的目标注解。 component-scan默认扫描的注解类型是Component。 在Component 语义基础上细化后的Repository、Service、Controller 也同样可以被component-scan扫描到。 3) 被添加到容器的组件的命名规则。 component-scan在扫描相关类定义并将它们添加到容器时会根据注解的value属性来生成beanName。默认的命名规则是 将类名首字母小写。 【强制】Autowired。 1) Autowired 按照类型匹配进行依赖注入。 类似于byType类型的自动绑定但它比byType更加灵活可以标注在属性、方法、构造方法、构造方法参数上。 2) Autowired 扫描。 IoC容器需要某种方式来了解哪些对象标注了Autowiredcomponent-scan对此提供了支持。 3) Qualifier。 如果用byType方式同时找到多个同类型的对象实例可以使用注解Qualifier 对依赖注入的条件做进一步限定。 Qualifier 是byName自动绑定的注解版可以单独使用也可以和Autowired 一起使用。 【强制】Resource。 1) Resource 是JDK扩展包提供的注解更加具有通用性。 除非是JDK 8否则需要引入依赖。 dependencygroupIdjakarta.annotation/groupIdartifactIdjakarta.annotation-api/artifactIdversion2.1.1/version /dependency 2) Resource 默认byName未指定name时会使用属性名作为name。通过name找不到的话会尝试byType。 3) Resource 用在属性、setter方法上。 【强制】全注解开发。 全注解开发就是不再使用Spring配置文件了写一个配置类来代替配置文件。 import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;Configuration ComponentScan(com.mypath.mypackage) public class MyConfig { } 3 Spring AOP 3.1 概念 【强制】AOP。 1) AOPAspect-Oriented Programming面向方面编程。 2) AOP可以给我们带来什么 使用AOP我们可以对类似于日志记录和安全检查等系统需求进行模块化的组织。 3) AOP的实现机制。 AOP的实现机制有动态代理、动态字节码增强、自定义类加载器、AOL扩展等。 在Spring AOP中在目标类实现了一些接口的情况下使用基于接口的动态代理技术否则使用基于类的CGLIB动态字节码增强技术。 【强制】动态字节码增强。 通常的class文件都是从Java源代码文件使用Javac编译器编译而成的但只要符含Java class规范我们也可以使用ASM或者CGLIB等Java工具库在程序运行期间动态构建字节码的class文件。 在此前提下我们可以为需要织入横切逻辑的模块类在运行期间通过动态字节码增强技术为这些系统模块类生成相应的子类而将横切逻辑加到这些子类中让应用程序在执行期间使用的是这些动态生成的子类从而达到将横切逻辑织入系统的目的。 不过如果需要扩展的类以及类中的实例方法等声明为final的话则无法对其进行子类化的扩展。 Spring AOP在无法采用动态代理机制进行AOP功能扩展的时候会使用CGLIB库的动态字节码增强支持来实现AOP的功能扩展。 【强制】AOP基本概念。 1) Joinpoint。 Jointpoint是可以进行织入操作的系统执行点。常见的Joinpoint类型有方法调用、方法执行、构造方法调用、构造方法执行、字段设置、字段获取、异常处理执行、类初始化。 2) Pointcut。 Pointcut是Jointpoint的表述方式。有点儿像引用。 Pointcut的表述方式有直接指定Joinpoint所在方法名称、正则表达式、使用特定的Pointcut表述语言。使用得比较普遍的是正则表达式。 3) Advice。 Advice是单一横切关注点逻辑的载体它代表将会织入到Jointpoint的横切逻辑。如果将Aspect比作OOP中的Class那么Advice就相当于Class中的Method。 按照Advice在Joinpoint位置执行时机的差异或者完成功能的不同Advice可以分为 1. Before Advice在Joinpoint指定位置之前执行的Advice。 2. After returning Advice只有在当前Joinpoint处执行流程正常完成后才执行。 3. After throwing Advice只有在当前Jointpoint处执行过程中抛出异常情况下才执行。 4. After Advice不管Joinpoint处执行流程是正常终了还是抛出异常都会执行。 5. Around Advice对附加其上的Joinpoint进行包裹可以在Joinpoint之前和之后都指定相应的逻辑甚至于中断或者忽略Joinpoint处原来程序流程的执行。我们常使用的Filter功能就是Around Advice的一种体现。 4) Aspect。 Aspect是对系统中的横切关注点逻辑进行模块化封装的AOP概念实体。 通常情况下Aspect可以包含多个Pointcut以及相关Advice定义。 5) Weaver。 Weaver是织入器职责是完成横切关注点逻辑到系统的最终织入。 ProxyFactory类是Spring AOP中最通用的织入器。 6) Target。 符合Pointcut指定的条件将被织入横切逻辑的对象。 7) AOP各个概念所处的场景。 3.2 实践 【强制】动态代理。 1) 代理模式。 提供一个代理类让我们在调用目标方法的时候不再是直接对目标方法进行调用而是通过代理类间接调用。让不属于目标方法核心逻辑的代码从目标方法中剥离出来即解耦。 2) 动态代理代码示例。 import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays;public class Test {public static void main(String[] args) {ProxyFactory factory new ProxyFactory(new MyInterfaceImpl());MyInterface proxy (MyInterface) factory.getProxy();proxy.hello(myParam);} }interface MyInterface {String hello(String s); }class MyInterfaceImpl implements MyInterface {Overridepublic String hello(String s) {return hello world;} }class ProxyFactory {private Object target;public ProxyFactory(Object target) {this.target target;}public Object getProxy() {ClassLoader classLoader target.getClass().getClassLoader();Class?[] interfaces target.getClass().getInterfaces();InvocationHandler invocationHandler new InvocationHandler() {Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object result null;try {System.out.println([动态代理][日志] 方法名: method.getName());System.out.println([动态代理][日志] 参数列表 Arrays.toString(args));System.out.println([动态代理][日志] 开始执行);result method.invoke(target, args);System.out.println([动态代理][日志] 返回值 result);} catch (Exception e) {System.out.println([动态代理][日志] 异常 e.getMessage());} finally {System.out.println([动态代理][日志] 执行完毕);}return result;}};return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);} } 【强制】Pointcut表达式。 1) 匹配模式。 execution、within、this、target、args等。 2) execution模式表达式格式。 execution([权限修饰符] [返回类型] [全类名].[方法名]([参数列表])) 权限修饰符、返回类型、全类名、方法名 可以用通配符 * 代替。 全类名、方法名可以部分匹配如*Service、get*。 参数列表 可以用 .. 代替。 【强制】AspectJ 形式的Advice。 1) Before。 2) AfterReturning。 3) AfterThrowing。 4) After。 5) Around。 【推荐】AspectJ 形式的Spring AOP。 1) 配置文件bean.xml。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd!-- 开启组件扫描 --context:component-scan base-packagecom.mypath/context:component-scan!-- 开启aop自动代理 --bean classorg.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreatorproperty nameproxyTargetClass valuetrue/property/bean!-- 基于Schema的aop自动代理 --!--aop:aspectj-autoproxy proxy-target-classtrue/-- /beans 2) Java代码。 package com.mypath;import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component;public class Test {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(bean.xml);MyTask proxy (MyTask) context.getBean(myTask);proxy.task1();proxy.tas k2();} }Component class MyTask {public void task1() {System.out.println(task1 执行);}public void task2() {System.out.println(task2 执行);} }Aspect Component class PerformanceTraceAspect {private final Logger logger Logger.getLogger(PerformanceTraceAspect.class);Pointcut(execution(public void com.mypath.MyTask.task1()) || execution(public void com.mypath.MyTask.task2()))public void pointcut() {}Around(pointcut())public Object performanceTrace(ProceedingJoinPoint joinPoint) throws Throwable {String methodName joinPoint.getSignature().getName();try {logger.info(methodName 开始执行);return joinPoint.proceed();} finally {logger.info(methodName 执行结束);}} } 3) 输出。 [02-13 12:42:20] [INFO] [com.mypath.MyAspect:45] task1 开始执行 task1 执行 [02-13 12:42:20] [INFO] [com.mypath.MyAspect:48] task1 执行结束 [02-13 12:42:20] [INFO] [com.mypath.MyAspect:45] task2 开始执行 task2 执行 [02-13 12:42:20] [INFO] [com.mypath.MyAspect:48] task2 执行结束 【推荐】基于Schema的Spring AOP。 1) Java代码。 package com.mypath;import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch;public class Test {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(bean.xml);MyTask myTask (MyTask) context.getBean(myTask);myTask.task();} }Component class MyTask {public void task() {System.out.println(task 执行);} }Component class MyAspect {private final Logger logger Logger.getLogger(MyAspect.class);public void doBefore(JoinPoint joinPoint) {if (logger.isInfoEnabled()) {logger.info(before method[ joinPoint.getSignature().getName() ] execution.);}}public void doAfterReturning(JoinPoint joinPoint) {if (logger.isInfoEnabled()) {logger.info(method[ joinPoint.getSignature().getName() ] completed successfully.);}}public void doAfterThrowing(RuntimeException e) {logger.error(ExceptionUtils.getFullStackTrace(e));}public void doAfter() {logger.warn(release system resources, etc.);}public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {StopWatch watch new StopWatch();try {watch.start();return joinPoint.proceed();} finally {watch.stop();if (logger.isInfoEnabled()) {logger.info(watch);}}} } 2) 配置文件bean.xml。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.3.xsd!-- 开启组件扫描 --context:component-scan base-packagecom.mypath/context:component-scan!-- aop配置 --aop:configaop:aspect idmyAspect refmyAspect order2aop:pointcut idprivatePointcut expressionexecution(public void com.mypath.MyTask.task())/aop:before pointcut-refprivatePointcut methoddoBefore/aop:after-returning pointcut-refprivatePointcut methoddoAfterReturning/aop:after-throwing pointcut-refprivatePointcut methoddoAfterThrowing throwinge/aop:after pointcut-refprivatePointcut methoddoAfter/aop:around pointcut-refprivatePointcut methoddoAround//aop:aspect/aop:config /beans 3) 输出。 [02-13 13:22:40] [INFO] [com.mypath.MyAspect:33] before method[task] execution. task 执行 [02-13 13:22:40] [INFO] [com.mypath.MyAspect:60] StopWatch : running time 11056500 ns; [] took 11056500 ns 100% [02-13 13:22:40] [WARN] [com.mypath.MyAspect:49] release system resources, etc. [02-13 13:22:40] [INFO] [com.mypath.MyAspect:40] method[task] completed successfully. 4 事务 【推荐】声明式事务。 事务控制的代码结构基本是确定的所以框架就可以将固定模式的代码抽取出来进行封装。 通过配置的方式、由框架实现的事务称为声明式事务。 【推荐】Spring事务属性。 1) 只读。 true/false。 2) 超时。 超时回滚。单位秒。 3) 回滚策略。 rollbackFor回滚。 noRollbackFor不回滚。 4) 隔离级别。 DEFAULT使用数据库默认的隔离级别。 READ_UNCOMMITTED读未提交。 READ_COMMITTED读已提交。 REPEATABLE_READ可重复读。 SERIALIZABLE串行化。 5) 传播行为。 REQUIRED支持当前事务如果当前没有事务就新建一个默认。 SUPPORTS支持当前事务如果当前没有事务就以非事务方式运行。 MANDATORY如果当前没有事务抛出异常。 REQUIRES_NEW开启一个新的事务如果有已存在的事务将已存在的事务挂起。 NOT_SUPPORTED以非事务方式运行如果有已存在的事务将已存在的事务挂起。 NEVER以非事务方式运行如果有已存在的事务抛出异常。 NESTED如果有已存在的事务则运行在一个嵌套式事务中。被嵌套的事务可以独立于外层事务进行提交或回滚。 【推荐】事务的全注解实现。 1) 配置事务。 import javax.sql.DataSource;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement;Configuration ComponentScan(com.mypath) EnableTransactionManagement public class Test {Beanpublic DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {DataSourceTransactionManager dataSourceTransactionManager new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource);return dataSourceTransactionManager;} } 2) 使用事务。 使用Transactional 注解标识类或方法。 5 统一资源加载 开发中。。 6 其他 6.1 反射 【推荐】反射。 1) 获取Class对象。 Class clazz1 MyClass.class; Class clazz2 new MyClass().getClass(); Class clazz3 Class.forName(com.mypath) 2) 获取构造方法。 // 获取所有的构造方法 Constructor[] constructors1 clazz2.getDeclaredConstructors(); // 获取所有的public构造方法 Constructor[] constructors2 clazz2.getConstructors();// 带参数的构造方法, 括号内为参数对应的Class对象列表 Constructor constructor clazz2.getConsturctor(String.class, int.class); 3) MyClass实例化。 // 如果constructor为private构造方法 // constructor.setAccessible(true); MyClass myClass (MyClass)constructor.newInstance(param1, 2); 4) 获取属性。 // 获取所有的属性 Field[] fields1 clazz1.getDeclaredFields(); // 获取所有的public属性 Field[] fields2 clazz1.getFields(); 5) 获取方法。 // 获取所有的方法 Method[] methods1 clazz2.getDeclaredMethods(); // 获取所有的public方法 Method[] methods2 clazz2.getMethods(); 6) 执行方法。 // 如果method为private方法 // method.setAccessible(true); method.invoke(param1, 2); 6.2 Log4j2 【推荐】Log4j2的重要组件。 1) 日志信息的优先级。 日志信息的优先级从低到高有TRACE追踪、DEBUG调试、INFO信息、WARN警告、FATAL严重错误。 2) 日志信息的输出目的地。 将日志打到控制台或文件。 3) 日志信息的输出格式。 【推荐】log4j2的使用。 1) 引入依赖。 dependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-core/artifactIdversion2.19.0/version /dependency dependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-slf4j2-impl/artifactIdversion2.19.0/version /dependency 2) 创建配置文件log4j2.xml。 省略。 3) 导包。 import org.slf4j.Logger; import org.slf4j.LoggerFactory; 4) 初始化Logger对象。 private Logger logger LoggerFactory.getLogger(MyClass.class); 5) 打印信息。 logger.info(hello world); 6.3 JdbcTemplate 【推荐】JdbcTemplate的使用。 1) 引入依赖。 !-- Spring JDBC -- dependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion6.0.2/version /dependency !-- Druid数据源 -- dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.15/version /dependency !-- MySQL驱动 -- dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.30/version /dependency 2) 创建配置文件jdbc.properties。 jdbc.userroo jdbc.passwordroot jdbc.urljdbc:mysql://localhost:3306/spring?characterEncodingutf8useSSLfalse jdbc.drivercom.mysql.cj.jdbc.Driver 3) 创建配置文件bean.xml。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd!-- 导入外部属性文件 --context:property-placeholder locationclasspath:jdbc.properties/!-- 配置JDBCTemplate --bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplateproperty namedataSource refdruidDataSource//bean!-- 配置数据源 --bean iddruidDataSource classcom.alibaba.druid.pool.DruidDataSourceproperty nameurl value${jdbc.url}/property namedriverClassName value${jdbc.driver}/property nameusername value${jdbc.user}/property namepassword value${jdbc.password}//bean /beans 4) 创建测试类。 import java.util.List;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate;public class Test {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(bean.xml);JdbcTemplate jdbcTemplate (JdbcTemplate) context.getBean(jdbcTemplate);String insertSql INSERT INTO my_table VALUES(?, ?, ?);Object[] params { 1, hello, world };jdbcTemplate.update(insertSql, params);String selectSql SELECT * FROM my_table;ListObject list jdbcTemplate.query(selectSql,new BeanPropertyRowMapper(Object.class));System.out.println(list);} } 【推荐】JdbcTemplate的全注解实现。 import javax.sql.DataSource;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate;import com.alibaba.druid.pool.DruidDataSource;Configuration ComponentScan(com.mypath) public class MySpringConfig {Bean(name jdbcTemplate)public JdbcTemplate getJdbcTemplate(DataSource dataSource) {JdbcTemplate jdbcTemplate new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}Beanpublic DataSource getDataSource() {DruidDataSource dataSource new DruidDataSource();dataSource.setDriverClassName(com.mysql.cj.jdbc.Driver);dataSource.setUsername(root);dataSource.setPassword(root);dataSource.setUrl(jdbc:mysql://localhost:3306/spring?characterEncodingutf8useSSLfalse);return dataSource;} }
http://www.hkea.cn/news/14264988/

相关文章:

  • 什么是网站建设与维护苏州自学网站建设平台
  • PHP网站开发简单实例网站设计工具
  • 国外 外贸 网站 源码外贸产品网站建设
  • 苏州诗华洛网站建设南京网站优化公司
  • 广州黄埔网站建设公司网站做下载功能
  • 网站页面设计欣赏网站备案部门
  • h5网站开发培训哪里好图片放大网站
  • 网站制作书生黄岛网站建设价格
  • asp网站建设实录源码公司网站建设带来的好处
  • 汽车行业网站建设方案wordpress上传文件去重复
  • 聚思博新网站建设网站开发要注意的漏洞
  • 网站营销外包如何做设计培训班学费一般多少
  • 乐清企业网站建站淘宝网站可以做seo吗
  • 定制型网站设计运营小程序的成本有哪些
  • 网站建设公众号管理wordpress开发插件
  • 网站建设综合技术怎么建网站做
  • dw可以做有后台的网站么南京网站建设王道下拉??
  • 公交公司网站建设的意义网页游戏制作软件
  • 北京当地网站 点做网站用什么系统
  • 北海建设厅网站上海做网站公司哪家好
  • 我的文档上传到网站 做链接做网页用什么编程语言
  • 建设银行网站入口WordPress菜单调用不出
  • 个人网站设计背景图淘宝指数
  • 模板网站如何建设网站优化课程培训
  • html5英文视频网站建设百度搜索网页
  • 东莞市电池网站建设宁波网站建设与推广方案
  • wordpress网站实现微信登录建e网官网效果图
  • 科技网站大全王通seo赚钱培训
  • 莆田市网站建设国家免费24小时律师咨询
  • 网站后台seo优化如何做seo网络培训班