总结
myBatis加强
-
XxxMapper…xml中的标签:
-
if标签可以用来完成sql的拼接
update product set <if test="productName!=null"> ,productName=#{productName} </if>
-
where标签可以用来完成sql判断的拼接
select * from product <where> <if test="pName"> and productName like concat('%',#{pName},'%') </if> <if test="id"> and dir_id=#{id} </if> <if test="salePrice"> <![CDATA[salePrice<#{salePrice}]]> </if> </where>
-
sql标签提取公共方法include标签调用
<select id="query" parameterType="cn.itsource.query.ProductQuery" resultType="product"> <include refid="mysql"></include> </select> <sql id="mysql"> select * from product <where> <if test="pName"> and productName like concat('%',#{pName},'%') </if> <if test="id"> and dir_id=#{id} </if> <if test="salePrice"> <![CDATA[salePrice<#{salePrice}]]> </if> </where> </sql>
-
Spring
通过Spring获取对象的两种方式:
- 方式一:
- BeanFactory 懒加载 没获取对象前不会创建对象
- 方式二:
- ApplicationContext BeanFactory的子类 迫切加载 没获取对象前前就会创建对象
- 改成懒加载的方法:
- xml的beans标签后面加default-lazy-init=“true” beans里面的标签所有都懒加载
- 单个设置懒加载 lazy-init=“true”
依赖注入
有些时候获取对象时,希望把他的属性配置好,就像获取连接池时的四大金刚
- xml注入注意:
- 必须要有对应字段的set方法,否则会报错
- 注解注入(常用)注意(今天还不行,要进行一些操作才可以):
- @Autowired 不需要set方法
- 会自动去xml中找对应的名称(id),没找到就找这个类的类型
Spring测试中可以用注解注入
-
使用注解步骤:
- 导包(test、aop)
- 加载容器 @RunWith(SpringJUnit4ClassRunner.class)
- 配置文件(常用) @ContextConfiguration(“classpath:applicationContext.xml”)
-
关于配置文件的注意:
- 有classpath时,会在根目录找(bin)
- 没有classpath时,会在当前包找名字 applicationContext.xml
- 后面什么都没有时,回去当前包找一个叫 类名-context.xml
-
bean标签中的属性
- scope:设置作用域(singleton、prototype、request)
- init-method=“init” 指定初始化方法
- destroy-method=“destroy” 指定销毁方法