Spring&Spring搭建&SpringJdbcTemplate&Spring Bean管理&Spring结合Mybatis

Spring基础

Spring是什么

Spring是一个轻量级的IOC和AOP的一站式Java开发框架,是为了简化企业级开发而生的

轻量级

框架体积小(核心模块)

IOC

Inversion Of Control (控制反转)缩写为IOC,是由Spring管理对象,而非传统实现中由程序代码直接操控,将创建对象的控制权反转给spring框架

以前在程序中需要对象都是自己new,例如new Student对象

AOP

Aspect Oriented Programming 面向切面编程

将程序中的一些公共的非业务代码分离提取出来,然后在业务代码执行时,给他们横切进来

使用的动态代理的机制实现,在我们业务代码,不显示的调用,但是执行业务代码,会通过代理对象,调用非业务代码

一站式

除了核心的IOC和AOP功能之外,还对数据访问层,web层,都有封装,所以是一站式的

Spring体系结构

Spring搭建

1.创建一个maven项目,在pom.xml中导入spring的jar包

xml 复制代码
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>

2.在resources文件夹中创建spring.xml文件

编写spring配置文件

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">
<bean id="user" class="com.ff.spring.model.User"> </bean>
</beans>

spring bean管理

Bean对象

由于把对象交给spring管理后,spring会对对象进行功能的增强,所以在spring框架中生成的对象,统一称为Bean对象

叫bean对象是为了区分这个对象是我们自己new的还是spring框架生成的

spring中bean管理有两种方式

1.xml配置方式

复制代码
 在spring框架中注册需要被spring管理的类
    使用Bean标签配置需要让spring管理的类
    id="对象名称",可以在getBean中获得spring生成的对象
    class="需要让spring管理的类的地址"
    scope="配置bean的作用域"
    scope="Singleton" 单例的,在spring框架启动时,就创建对象,而且始终只创建一个对象
    scope="prototype" 原型的(多例的)  会在每一次获得对象时,创建一个新的对象

    IOC指的是spring框架创建对象,创建对象的同时,还有一个动作称为依赖注入
    依赖注入:在创建对象的时候,为对象中属性赋值
    依赖注入有两种方式:
    1,通过属性注入,属性的set方法
    2.通过构造方法注入
-->
<!--<bean id="admin" class="com.george.springpro.model.Admin">
    <property name="account" value="admin"/>
    <property name="password" value="111"/>
</bean>-->

    <bean id="admin" class="com.george.springpro.model.Admin">
        <constructor-arg name="account" value="admin"/>
        <constructor-arg name="password" value="111"/>
    </bean>

    <bean id="adminDao" class="com.george.springpro.dao.AdminDao"></bean>

    <bean id="adminService" class="com.george.springpro.service.AdminService">
        <property name="adminDao" ref="adminDao"></property>
    </bean>

2.注解配置方式

首先要在spring中开启注解方式

xml 复制代码
<context:component-scan base-package="包名"> </context:component-scan>

注解创建对象

@Component(value="user")等同于

@Service

@Repository

以上注解都可以实现创建对象功能,只是为了后续扩展功能,在不同的层使用不同的注解标记

@Scope(value="prototype") 原型

@Scope(value=" singleton ") 单例

注解方式注入

@Autowired

@Autowired 是 Spring 提供的注解,可以写在字段和 setter 方法上。如果写在字段上,那么就不需要再写 setter 方法。默认情况下它要求依赖对象必须存在,如果允许 null 值,可以设置它的 required 属性为 false。

byType 自动注入

该注解默认使用按类型自动装配 Bean 的方式。

byName 自动注入

如果我们想使用按照名称(byName)来装配,可以结合@Qualifier 注解一起使用。

需要在引用属性上联合使用注解@Autowired 与@Qualifier。@Qualifier 的value 属性用于指定要匹配的 Bean 的 id 值。

JDK 注解@Resource 自动注入

Spring 提供了对 jdk 中@Resource 注解的支持。@Resource 注解既可以按名称匹配 Bean,也可以按类型匹配 Bean。默认按照 ByName 自动注入

byName 注入引用类型属性

@Resource 注解指定其 name 属性,则 name 的值即为按照名称进行匹配的 Bean 的 id。

注解与XML的对比

注解的优点:

方便,直观,高效

注解的缺点:

以硬编码的方式写入到Java代码中,修改是需要重新编译代码的

XML的优点:

配置和代码是分离的,在XML中做修改,无需编译代码,只需要重启服务器即可将新的配置加载

XML的缺点:

编写麻烦,效率低,大型项目过于复杂

Spring数据访问层管理

Spring本身提供了web层的SpringWeb和持久层的Spring JdbcTemplate

Spring JdbcTemplate

1,下载Spring Jdbc数据访问层jar包

xml 复制代码
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>

<!--记得导入MySQL的jar包-->
<!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>

2.导入属性文件

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

管理数据源对象,本次采用阿里巴巴数据库连接管理对象druid

xml 复制代码
<bean id="druidDataSource"class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value=""></property>
<property name="url" value=""></property>
<property name="username" value=""></property>
<property name="password" value=""></property>
<property name="initialSize" value="10"></property><!--初始化连接数量-->
<property name="maxActive" value="20"></property><!--最大连接数量-->
</bean>

Spring集成Mybatis

Spring 集成 Mybatis 其核心是将 SqlSessionFactory 交由 Spring 管理,并由Spring 管理对

dao接口的代理实现

首先需要导入mybatis的jar包

同时还需要导入Spring结合Mybatis的jar包

xml 复制代码
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>

<!-- Spring结合Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>

配置sqlSessionFactory

xml 复制代码
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:mappers/*Mapper.xml">
</property>
</bean>

指定生成接口代理

xml 复制代码
<bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ffyc.ssm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
</property>
</bean>

在service中注入Dao代理接口,此接口具有Spring代理实现

java 复制代码
@Autowired
DeptDao deptDao;
相关推荐
NCIN EXPE1 小时前
redis 使用
数据库·redis·缓存
MongoDB 数据平台1 小时前
为编码代理引入 MongoDB 代理技能和插件
数据库·mongodb
lUie INGA1 小时前
在2023idea中如何创建SpringBoot
java·spring boot·后端
极客on之路1 小时前
mysql explain type 各个字段解释
数据库·mysql
代码雕刻家1 小时前
MySQL与SQL Server的基本指令
数据库·mysql·sqlserver
lThE ANDE1 小时前
开启mysql的binlog日志
数据库·mysql
yejqvow121 小时前
CSS如何控制placeholder文字的颜色_使用--placeholder伪元素
jvm·数据库·python
handler011 小时前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习
电子云与长程纠缠2 小时前
UE5 两种方式解决Decal Actor贴花拉伸问题
学习·ue5·游戏引擎
oLLI PILO2 小时前
nacos2.3.0 接入pgsql或其他数据库
数据库