一、@Resource与@autowired的区别:
@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:
Java代码
1。@Autowired() @Qualifier("baseDao")
2.private BaseDao baseDao;
3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定,
如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
- @Resource(name="baseDao")
- private BaseDao baseDao;
二、@value 属于SpringFarmwork中的,可以用来读取配置文件的值
三、
@Transactional(readOnly = true)
readOnly为true 时读取的数据如果缓存中存在就从缓存中读取 这是没有问题的
readOnly为false 时读取的数据就不能从缓存中读取了 万一你第一个操作是修改这个数据,第二个操作是读取这个数据而这个数据从缓存中拿的 那不就gg了,防止脏读
四、@Cacheable
@Cacheable(value = {"redis"}, key = "'pcc:allList'", unless = "#result == null")
该注解打在方法上,表示为默认将查询到的数据存入到redis中,除非结果为null,注意:使用@Cacheable生成的缓存是不会清除的,同时当redis中已经存在相同的key时,@Cacheable默认不生成缓存,即
@CachePut Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中
五、mybatis动态参数 ,例子:动态参数可以是表,也可以是字段
<select id="getMaxNo" parameterType="string" resultType="string">
select MAX(substr(${colName},15,4)) from ${tableName}
WHERE SUBSTR(${colName},1,LENGTH(#{moduleName})) = #{moduleName}
AND SUBSTR(${colName},LENGTH(#{moduleName}) + 1,8) = #{currentDate}
AND SUBSTR(${colName},LENGTH(#{moduleName}) + 9,4)= #{subMobile}
</select>
String getMaxNo(@Param("tableName") String tableName, @Param("colName") String colName, @Param("moduleName") String moduleName, @Param("currentDate") String currentDate, @Param("subMobile") String subMobile);
六、
boolean flag = redisTemplate.opsForHash().hasKey("redisKey","Info"+guid);
if(flag){
Object redisKey = redisTemplate.opsForHash().get("redisKey", "Info" + guid);
redisTemplate.opsForHash().put("redisKey","Info"+guid,helpBaseInfoList);
七、@Transactional(rollbackFor=Exception.class)
在项目中,@Transactional(rollbackFor=Exception.class),如果类加了这个注解,那么这个类里面的方法抛出异常,就会回滚,数据库里面的数据也会回滚。
在@Transactional注解中如果不配置rollbackFor属性,那么事物只会在遇到RuntimeException的时候才会回滚,加上rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚
参考: https://www.cnblogs.com/clwydjgs/p/9317849.html
八、stringbuild > stringBuffer >string 字符串操作效率比较
九、mybatisxml文件如果遇到有实体类中包含实体类的写法
<resultMap id="BaseResultMap" type="cn.com.oceansoft.osc.ms.domain.SystemHelpBaseInfo" >
<id column="GUID" property="guid" jdbcType="VARCHAR" />
<collection property="titleList" ofType="cn.com.oceansoft.osc.ms.domain.SystemHelpBaseInfo">
<id property="guid" column="sub_guid"/>
<collection property="helpAtt" ofType="cn.com.oceansoft.osc.ms.domain.SystemHelpAtt">
<id property="guid" column="ts_guid"/>
</collection>
</collection>
<collection property="helpAtt" ofType="cn.com.oceansoft.osc.ms.domain.SystemHelpAtt">
<id property="guid" column="t_guid"/>
</collection>
</resultMap>
十、方法传递参数的时候,如果多个方法类似,参数确不一致,可以用hashMap传递参数,这样可以让代码更优雅
时间格式化 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")