SSM整合再回顾
一.spring
前言:提起spring就不得不说到它的IOC和AOP的概念。IOC就是一个对象容器,程序员可以将对象的创建交给spring的IOC容器来创建,不再使用传统的new对象方式,从而极大程度了降低了程序间的耦合性。IOC内部是怎么存储对象的呢?通过Map键值对的方式!每个对象都有它的id名字和class全类名,通过名字来获取对象。AOP就是面向切面编程,可以减少编写可重用代码,提取出公用的代码块,从而在不改变源代码的情况下增强原方法。原理就是动态代理。
IOC
1.创建bean对象的三种方式
1 <!-- 1.通过构造方法创建bean对象--> 2 <!-- <bean id="UserService" class="cn.hongyuan.service.UserService" scope="prototype"></bean>--> 3 4 <!-- 2.通过工厂来创建bean对象--> 5 <!-- <bean id="instanceFactory" class="cn.hongyuan.utils.InstanceFactory"></bean>--> 6 <!-- <bean id="UserService" factory-bean="instanceFactory" factory-method="getUserService"></bean>--> 7 8 <!-- 3.通过静态方法来创建对象--> 9 <!-- <bean id="UserService" class="cn.hongyuan.utils.InstanceFactory" factory-method="getUserService"></bean>-->
2.DI依赖注入
有三种注入的方式
1 <!-- 依赖注入的三种方式--> 2 <!-- 1.通过构造方法注入--> 3 <!-- 2.通过set方法注入--> 4 <!-- 3.注入集合数据--> 5 <bean id="UserService" class="cn.hongyuan.service.UserService"> 6 <!-- <constructor-arg name="age" value="23"></constructor-arg>--> 7 <!-- <constructor-arg name="name" value="zhangsan"></constructor-arg>--> 8 <property name="age" value="25"></property> 9 <property name="name" value="lvjianyou"></property> 10 <property name="a"> 11 <array> 12 <value>1</value> 13 <value>2</value> 14 <value>3</value> 15 </array> 16 </property> 17 <property name="lits"> 18 <list> 19 <value>4</value> 20 <value>5</value> 21 <value>6</value> 22 </list> 23 </property> 24 25 <property name="map"> 26 <map> 27 <entry key="zhangsan" value="13"></entry> 28 <entry key="lisi" value="24"></entry> 29 </map> 30 </property>
AOP
1.专用术语
1.target:目标类,需要被代理的类。例如:UserService
2.Joinpoint(连接点):所谓连接点是指那些可能被拦截到的方法。例如:所有的方法
3.PointCut 切入点:已经被增强的连接点。例如:addUser()
4.advice 通知/增强,增强代码。例如:after、before
5. Weaving(织入):是指把增强advice应用到目标对象target来创建新的代理对象proxy的过程.
6.proxy 代理类
7. Aspect(切面): 是切入点pointcut和通知advice的结合
2.配置方式
<!-- 1 创建目标类 -->
<bean id="userServiceId" class="com.itheima.c_spring_aop.UserServiceImpl"></bean>
<!-- 2 创建切面类(通知) -->
<bean id="myAspectId" class="com.itheima.c_spring_aop.MyAspect"></bean>
<!-- 3 aop编程
3.1 导入命名空间
3.2 使用 <aop:config>进行配置
proxy-target-class="true" 声明时使用cglib代理
<aop:pointcut> 切入点 ,从目标对象获得具体方法
<aop:advisor> 特殊的切面,只有一个通知 和 一个切入点
advice-ref 通知引用
pointcut-ref 切入点引用
3.3 切入点表达式
execution(* com.itheima.c_spring_aop.*.*(..))
选择方法 返回值任意 包 类名任意 方法名任意 参数任意
1 2.2.2.1 第一步:把通知类用 bean 标签配置起来 2 <!-- 配置通知 --> 3 <bean id="txManager" class="com.itheima.utils.TransactionManager"> 4 <property name="dbAssit" ref="dbAssit"></property> 5 </bean> 6 2.2.2.2 第二步:使用 aop:config 声明 aop 配置 7 aop:config: 8 作用: 用于声明开始 aop 的配置 9 <aop:config> 11 <!-- 配置的代码都写在此处 --> 12 </aop:config> 13 2.2.2.3 第三步:使用 aop:aspect 配置切面 14 aop:aspect: 15 作用: 16 用于配置切面。 17 属性: 18 id: 给切面提供一个唯一标识。 19 ref: 引用配置好的通知类 bean 的 id。 20 <aop:aspect id="txAdvice" ref="txManager"> 21 <!--配置通知的类型要写在此处--> 22 </aop:aspect> 23 2.2.2.4 第四步:使用 aop:pointcut 配置切入点表达式 24 aop:pointcut: 25 作用: 26 用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。 27 属性: 28 expression:用于定义切入点表达式。 29 id: 用于给切入点表达式提供一个唯一标识 30 <aop:pointcut expression="execution( 31 public void com.itheima.service.impl.AccountServiceImpl.transfer( 32 java.lang.String, java.lang.String, java.lang.Float) 33 )" id="pt1"/> 34 2.2.2.5 第五步:使用 aop:xxx 配置对应的通知类型 35 aop:before 36 作用: 37 用于配置前置通知。 指定增强的方法在切入点方法之前执行 38 属性: 39 method:用于指定通知类中的增强方法名称 40 ponitcut-ref:用于指定切入点的表达式的引用 41 poinitcut:用于指定切入点表达式 42 执行时间点: 43 切入点方法执行之前执行 44 <aop:before method="beginTransaction" pointcut-ref="pt1"/> 45 aop:after-returning
Mybatis
动态SQL
1. 根据不同的条件需要执行不同的 SQL 命令.称为动态 SQL
2. MyBatis 中动态 SQL 在 mapper.xml 中添加逻辑判断等.
3. If 使用
1 <select id="selByAccinAccout" resultType="log"> 2 select * from log where 1=1 3 <!-- OGNL 表达式,直接写 key 或对象的属性.不需要添加任 4 何特字符号 --> 5 <if test="accin!=null and accin!=''"> 6 and accin=#{accin} 7 </if> 8 <if test="accout!=null and accout!=''"> 9 and accout=#{accout} 10 </if> 11 </select>
4. <where>
4.1 当编写 where 标签时,如果内容中第一个是 and 去掉第一个
and
4.2 如果<where>中有内容会生成 where 关键字,如果没有内容不
生成 where 关键
4.3 使用示例
4.3.1 比直接使用<if>少写 where 1=1
1 <select id="selByAccinAccout" resultType="log"> 2 select * from log 3 <where> 4 <if test="accin!=null and accin!=''"> 5 and accin=#{accin} 6 </if> 7 <if test="accout!=null and accout!=''"> 8 and accout=#{accout} 9 </if> 10 </where> 11 </select>
5. <choose> <when> <otherwise>
5.1 只有有一个成立,其他都不执行.
5.2 代码示例
5.2.1 如果 accin 和 accout 都不是 null 或不是””生成的 sql 中只
有 where accin=?
1 <select id="selByAccinAccout" resultType="log"> 2 select * from log 3 <where> 4 <choose> 5 <when test="accin!=null and accin!=''"> 6 and accin=#{accin} 7 </when> 8 <when test="accout!=null and accout!=''"> 9 and accout=#{accout} 10 </when> 11 </choose> 12 </where> 13 </select>
6. <set>用在修改 SQL 中 set 从句
6.1 作用:去掉最后一个逗号
6.2 作用:如果<set>里面有内容生成 set 关键字,没有就不生成
6.3 示例
6.3.1 id=#{id} 目的防止<set>中没有内容,mybatis 不生成 set 关
键字,如果修改中没有 set 从句 SQL 语法错误.
1 <update id="upd" parameterType="log" > 2 update log 3 <set> 4 id=#{id}, 5 <if test="accIn!=null and accIn!=''"> 6 accin=#{accIn}, 7 </if> 8 <if test="accOut!=null and accOut!=''"> 9 accout=#{accOut}, 10 </if> 11 </set> 12 where id=#{id} 13 </update>