第三十七天学习java

总结

myBatis加强

  • XxxMapper…xml中的标签:

    1. if标签可以用来完成sql的拼接

      update product set
      		<if test="productName!=null">
      			,productName=#{productName}
      		</if> 
      
    2. 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>
      
    3. 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获取对象的两种方式:

  • 方式一:
    1. BeanFactory 懒加载 没获取对象前不会创建对象
  • 方式二:
    1. ApplicationContext BeanFactory的子类 迫切加载 没获取对象前前就会创建对象
  • 改成懒加载的方法:
    1. xml的beans标签后面加default-lazy-init=“true” beans里面的标签所有都懒加载
    2. 单个设置懒加载 lazy-init=“true”

依赖注入

有些时候获取对象时,希望把他的属性配置好,就像获取连接池时的四大金刚

  • xml注入注意:
    • 必须要有对应字段的set方法,否则会报错
  • 注解注入(常用)注意(今天还不行,要进行一些操作才可以):
    1. @Autowired 不需要set方法
    2. 会自动去xml中找对应的名称(id),没找到就找这个类的类型

Spring测试中可以用注解注入

  • 使用注解步骤:

    1. 导包(test、aop)
    2. 加载容器 @RunWith(SpringJUnit4ClassRunner.class)
    3. 配置文件(常用) @ContextConfiguration(“classpath:applicationContext.xml”)
  • 关于配置文件的注意:

    1. 有classpath时,会在根目录找(bin)
    2. 没有classpath时,会在当前包找名字 applicationContext.xml
    3. 后面什么都没有时,回去当前包找一个叫 类名-context.xml
  • bean标签中的属性

    1. scope:设置作用域(singleton、prototype、request)
    2. init-method=“init” 指定初始化方法
    3. destroy-method=“destroy” 指定销毁方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值