Spring学习总结

1、Spring特性

1、IOC

表示反转控制:Inverse Of Control

2、DI

表示依赖注入:Dependency Injection

Spring框架,把java日常开发中,类的实例化在自己内部,通过配置文件的方式,实现自动管理。

2、配置文件

配置文件为一个xml格式的文件,一般起名为:
applicationContext.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">
    <context:component-scan base-package="com.mapbar"></context:component-scan>
    <!--<context:annotation-config></context:annotation-config>-->
    <bean id="categoryDao" class="com.mapbar.demo3.CategoryDao"></bean>
    <bean id="productDao" class="com.mapbar.demo3.ProductDao"></bean>
    <bean id="productService" class="com.mapbar.demo3.ProductService">
        <property name="productDao" ref="productDao"></property>
        <property name="categoryDao" ref="categoryDao"></property>
    </bean>
</beans>

3、Bean的配置

XML的方式

主要通过xml文件中的bean标签,进行相应的配置。

bean配置会有一个id属性,标识这个bean,也有一个class属性,标识对应的类。

使用方式:

// 第一步:获取配置上下文
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

// 第二步:通过getBean方法,获取对应的类,进行实例化。
ProductService productService = (ProductService) applicationContext.getBean("productService");
// 第三步:调用类的方法
productService.save();

关于属性配置:

通过property标签进行类的属性配置。

如果属性的值为基本类型,则直接用value设置,如果为对象类型,则用ref配置。

<bean id="productService" class="com.mapbar.demo3.ProductService">
        <property name="productDao" ref="productDao"></property>
        <property name="categoryDao" ref="categoryDao"></property>
</bean>

注解的方式

有四种注解的方式,可以设置类

@Component(“userService”)

@Service(“userService”)

@Controller(“userService”)

@Repository(“userService”)

这四种方法,基本功能上是一样的,区别仅仅是在语义上的区别。

属性注解:

@Value=,设置基本类型的数据

@Resource(name = “productDao”),可以设置对象类型的数据。

JDBC相关

Java的jdbc模块,进行链接数据库等各个操作,需要各种麻烦的api,而spring进行了简化操作。

数据库链接

数据库链接配置在xml配置文件中。

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/selection_course?useUnicode=true&amp;characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="woainia123"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

核心配置为以上代码。

在Java应用里面的代码就变为:

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");

CRUD操作

1、update

2、batchUpdate

3、queryForObject

4、queryForList

5、queryForMap

Spring事务

1、mysql事务相关

begin:开启一个事务

commit:提交一个事务

rollback:回滚一个事务

2、mysql事务的并发

脏读:由于公用一块内存,让一个未提交的事务影响了另一个事务的读取数据。解决方法:让每一个客户端的操作,都有单的内存空间。

不可重复读:两个事务同时操作一条数据,导致数据的异常。解决方法:锁定当前操作的某一条数据。

幻读:一个事务对所有的数据进行操作,但是另一个事务进行添加操作,导致不能得到想要的结果。解决方法:锁表。

事务隔离级别

读未提交(read-uncommited)

读已提交(read-commited)

可重复读(repeatable-read)

串行化(serializable)

select @@tx_isolation;
set sesstion transation isolation level read uncommited

3、JDBC对事务的操作

JDBC是通过Connection对象进行事务操作的。

有这么几个方法

setAutoCommit,设置自动提交

commit,提交事务

rollback,回滚事务

4、JDBC获取和设置事务隔离级别

getTransactionIsolation

setTransactionIsolation

5、Spring的事务

TransactionDefinition-事务定义

隔离级别

默认超时

事务传播行为

PlatformTransactionManager

TransactionStatus

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值