MyBatis
mybatis配置文件(基于JDBC的)
<configuration>
//引入properties文件
<properties resource="" />
//给包起一个别名
<typeAliases>
//
<typeAlias type="" alias="" />
// 包的方式
<package name="" />
</typeAliases>
// setting配置
<settings>
//参考官网 -- name表示内容,例如是否开启延迟加载 value表示true代表开启,false表示关闭
<setting name="" value=""></setting>
</settings>
//环境配置
<environments default="develoment">
<environment id="develoment">
<transationManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
//映射文件配置
<mappers>
//文件引入
<mapper resource="映射文件地址"/>
//包引入(常用这个,资源引入一般不常用)
<package name="映射文件地址"/>
</mappers>
</configuration>
properties格式为,关键字.变量=值,例如:
jdbc.url="xxxxxx"
配置SqlSession会话文件
- 封装
- 第一步
//基本配置文件
SqlSession sqlSession=null;
try{
inputStream is=Resource.getResourceAsStream("myBatis配置文件");
sqlSessionFactory sqlSessionFactory=new sqlSessionFactoryBuider().build(is);
sqlSession sqlSession=sqlSessionFactory.openSession("true");
}catch(IOException e){
e.printStackTrace();
}
return sqlSession;
- 使用
SqlSession sqlSession=配置文件类名.配置文件方法名();
Mapper接口文件 mapper=sqlSession.getMapper(Mapper接口文件.class)
开始业务使用
mapper映射文件(面向接口的)
接口文件(Mapper接口文件)
public interfance 接口名{
接口类型 接口方法()
}
映射标签
<resultMap>
<id property="javaBean变量名称" column="数据库column名称" jdbcType="column数据类型"/>
<result property="javaBean变量名称" column="数据库column名称" jdbcType="column数据类型" />
<assocation></assocation>
<collection></collection>
</resultMap>
mapper映射文件
<Mapper nameSpace="接口名">
<insert id="接口方法" resultType="方法类型">
SQL语句
</insert>
</Mapper>
select标签中有resultMap属性以及resultType,但是这两个属性只能存在一个
resultMap:处理一对多或者多对多的映射
resultType:设置结果类型
获取参数值的两种方式 #{}和${}(默认以键值对形式)
Param
public interfance 接口名{
接口类型 接口方法(@Param("键名") 数据类型 键值)
}
获取形参(重点这里是键值对获取的方式)
#{}占位符
${}字符串拼接,必须带单引号
动态SQL
Sql片段
- 定义
<sql id="设置唯一"></sql>
- 引入
<include refid="引用sql标签的id"></include>
if,trim
<insert id="mapper接口文件方法名" resultType="Mapper接口文件方法类型">
<trim perfix="" suffix="" suffixOvertides="" prefixOverrides="">
<if test="条件">
</if>
</trim>
</insert>
where, choose, when ,otherwise
<insert id="mapper接口文件方法名" resultType="mapper接口文件方法类型">
<where>
<choose>
<when test="条件"></when>
<otherwise></otherwise>
</choose>
</where>
<insert>
forEach(不适合具键的数据,例如map,@Param()等等,适合list[])
<insert>
<foreach collection="需要循环的数据" item="每一项的数据" separator="分隔符" open="开始符号" close="结束符号">
</foreach>
</insert>
二级缓存
<cache readOnly="" size="" flushInterval="" evication=""/>
- evication
- LRU 最近使用规则
- FIFO 先进先出规则
- SOFT 软引用规则
- WEAK 弱引用规则
- flushInterval
- 刷新间隔
- readOnly
true 只读缓存 false 读写缓存
- size(存储池--正整数)
SpringIOC------Bean(面向类名的)
基于XML
配置
<bean 属性="属性值"></bean>
- 属性
id:唯一标识符
class:类的路径
scoped:作用域 prototype(多例模式)/singleton(默认-单例模式)
Bean的实例化(无论是XML还是注解都需要)
- 第一步
类型 对象=new ClassPathXmlApplicationContext:根据类路径查找Xml(重要)
类型 对象=new FileSystemXmlApplicationContext:根据系统路径查找的XML
类型 对象=new ConfigurableApplicationContext:给IOC提供刷新关闭的方法
类型 对象=new webApplicationContext
- 第二步
Bean的类型 对象=第一步对象.getBean()
getBean():
2.1 可通过Bean的id获取,
2.2 根据Bean的类型,即被IOC管理的类名,例如,类名.class
Bean的依赖注入(方法很多),依赖注入也叫DI
- setter注入(重要最常用)
- 第一步
给JavaBean设置setter方法
- 第二步
bean标签嵌套property标签 格式为:
<bean id="唯一标识符" class="ioc管理的类">
<property name="javaBean的变量名" value="需要注入的值"></property>
</bean>
- 构造器注入(常用)
- 第一步
给JavaBean设置构造方法
- 第二步
bean标签嵌套constructor-org标签 格式为
<bean id="唯一标识符" class="ioc管理的类">
<contructor-org value="需要注入的值"></contructor-org>
</bean>
其中contructor-org的属性name可指具体变量
property标签
基本格式
<property name="javeBean变量名" value="需要注入的值"></property>
- 特殊值处理
- null类型数据
在property里嵌套null单标签
<property name="">
<null/>
</property>
- CDATA节(会将一些特殊值,特殊字符原样输出)
在property里嵌套value双标签在value里输入IDEA编辑器的快捷键大写CD
<property name="">
<value>CD</value>
</property>
3.引用外部Bean
3.1 第一步
javaBean需要引用即将被Bean管理的类,数据类型就是类类型
3.2 第二步
<property name="" ref="外部bean的id"></property>
4.内部bean(无法被IOC管理)
4.1 第一步
同引用外部Bean的第一步
4.2 第二步
<property name="">
<bean id="" class="">
<property name="" value=""></property>
</bean>
</property>
- 数组类型数据
<property name="">
<array>
<value></value>
<value></value>
<value></value>
</array>
</property>
- list集合
6.1.1 第一种方式:直接赋值
<property name="">
<list>
<value></value>
<value></value>
<value></value>
</list>
</property>
6.1.2 第二种种方式
上述步骤的value标签可以用以下标签替代使用
<ref bean="bean的id"></bean>
6.1.3 第三种方式:引用外部list,利用util约束
第一步
<property name="" ref="外部id"></property>
第二步
<util:list id="">
<value></value>
<value></value>
<value></value>
</util:list>
其中util约束中value标签也可以用以下替代
<ref bean="bean的id"></bean>
- map集合
7.1 第一种方式
<bean>
<property name="">
<map>
<entry key="" value-ref="引用外部bean的id"/>
</map>
</property>
</bean>
7.2 第二种方式:引用外部map,利用util约束
第一步
<property name="" ref="外部id"/>
第二步
<util:map id="">
<entry key="" value-ref=""/>
<entry key="" value-ref=""/>
</util:map>
- Druid连接池
区别:
JDBC有SQL,
Druid:能够防止SQL注入,监控特性
<context:property-placehoder loaction="properties文件地址"></context:property-placehoder>
<bean id="" class="Druid的数据源">
<property name="url" value="${url}"></property>
<property name="driverclassname" value="${driver}"></property>
<property name="password" value="${password}"></property>
<property name="username" value="${username}"></property>
</bean>
- p的约束
<bean id="" class="" p:属性名="属性值" p:属性名map-ref="外部bean的id"></bean>
bean的扩展
- Bean的作用域
prototype多例模式
singlton单例模式
区别: 当我们需要获得同一个Bean多次时可使用prototype
- Bean的生命周期
-
第一,实例化
-
第二,依赖注入
-
第三,初始化
1.第一步
在javaBean中配置初始化方法
2.第二步
在Bean中设置 init-methods
<Bean id="" class="" init-methods=""></bean>3.第三步
测试类中使用
-
第四,销毁
1.第一步
在javaBean中配置销毁方法
2.第二步
在Bean中设置destroy方法
<bean id="" class="" destroy-mehods=""></bean>3 第三步
测试类中调用configurableApplicationContext对象,然后再使用close方法
注解
- 第一步
@service业务层组件
@controller 控制层组件
@repository持久层组件
@component普通组件
- 默认id为类名,其类名为小驼峰,即首字母为小写
- 自定义id为给组件名称设置value,例如@controller(自定义id)
- 第二步
XML中配置:
扫描组件
<context:component-scan base-package="需要扫描的类"></context:component-scan>
排除扫描
<context:exclude-fifter type="annotation/assignable" expression=""></context:exclude-fifter>
Spring Aop
注意: Spring Aop依赖Spring aspects
基于注解
- 切面 Aspect 实现普通组件的切面类
- 切入点 Pointcut("excution(接口的实现类路径)") 注解在无参构造函数上
各种通知
- Before("切点方法") 前置通知
- After("切点方法") 后置通知
- return
bean中实现
<context:component-scan="扫描包路径"><context:component-scan>
测试类注意地方
getBean(目标对象.class) 此时的目标对象是接口名字(我是这么理解的)
springMVC
requestMapping注解
- 相关属性
- url
- methods
- value
- header
requestBody注解
WebMvcConfig配置文件
addCorsMapping 解决跨域的
addInterceptors 拦截器
....未完待续(懒)