2015-10-19 编程经验

思之无益,不如不思

 

酷酷酷

Q: 为应对紧急需求,在系统尚未上线之前,手动插入数据,弊端?

A: 非完整业务,缺失系统日志,在系统上线后,手动插入的数据和程序自动插入的数据搅和在一起,难以区分

A: 一个可能的解决方案:在表里面加一个source字段,1 手动插入  0 程序插入,值如果不存在,也表示是程序自动插入,避免最后手忙脚乱,不知道哪些数据是手动插入的,哪些是程序插入的

 

酷酷酷

Q: type,state等编码从0开始,还是从1开始

A: 如果是boolean类型的字段,从0开始,1是真,0是假,或者取否的意思

       如果状态很多,从1开始,值都是正数,0过于特殊了

 

酷酷酷

Mysql 字符串类型,区分NULL和空串,即:插入NULL,得到NULL,插入空串,得到空串

oracle不区分,空串到了数据库里面存的是NULL,NULL读出来是空串

 

 酷酷酷

获取罚款的上限和下限 -- 起个好名字很重要

 

	@RequestMapping("/getAmerceRange")
	@ResponseBody
	public AjaxResult getAmerceRange(Integer userId, String userName) {
		Map<String, Object> range = new HashMap<String, Object>();
		range.put("ceiling ", 20000f); // 上限
		range.put("floor", 0.01f); // 下限

		return AjaxResult.success(range);
	}

 

 酷酷酷

Spring MVC的service,如果没有任何方法配置了@Transactional注解,则不为此serivce生成代理类实例

 



 

 

  酷酷酷

Q几个问题:

1  Spring MVC的事务失效了,虽然抛出了异常,但是事务并没有回滚

2  repository的方法只要操作成功,立刻提交

3 多个reposioty操作不同的数据源,回滚是有必要的,此处必须要用事务

A 基础不扎实,需要继续精进研究

 

   酷酷酷

MyBatis传参数的两种形式:

1 传复杂数据类型:包含JAVA实体类、Map。运行时通过#{属性名}或#{map的KeyName}获取传入的值

2 传基本类型:包含int,String,Date等,传多个参数时必须使用@Param(value="XX")定义方法参数到mybatis-SQL参数的映射,如果只有一个参数可以不使用@Param注解

复杂数据类型参数传递

void modifyCooperatorComments(CooperatorComments comments);

	<update id="modifyCooperatorComments" parameterType="com.enterprise.domain.CooperatorComments">
		UPDATE hyip_task_cooperator_comments 
		SET comments=#{comments},update_time=NOW()
		WHERE user_id=#{userId}
	</update>
 

Map类型参数传递

<insert id="addMerchantPunishInfo" parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="punishId">
	insert into hyip_merchant_punish(user_id,username,punish_reason,punish_desc,audit_user_id,create_time) values(#{merchantUserId},#{userName},#{punishReason},#{punishDesc},#{auditUserId},NOW());
</insert>

void addMerchantPunishInfo(Map<String, Object> info);

                Map<String, Object> info = new HashMap<String, Object>();
		info.put("merchantUserId", userId);
		info.put("userName", userName);
		info.put("punishDesc", punishDesc);
		info.put("punishReason", punishReason);
		info.put("auditUserId", auditUserId);
		punishRepository.addMerchantPunishInfo(info);
 

基本类型多参数传递

Long addMerchantPunishInfo(@Param(value = "merchantUserId") Long merchantUserId, @Param(value = "userName") String userName,@Param(value = "punishDesc") String punishDesc, @Param(value = "punishReason") String punishReason);


<insert id="addMerchantPunishInfo" >
		insert into hyip_merchant_punish(user_id,username,punish_reason,punish_desc,audit_user_id,create_time) values(#{merchantUserId},#{userName},#{punishReason},#{punishDesc},#{auditUserId},NOW());
	</insert>

其它

1 入参如果有多个,无法指定 parameterType="XX"

2  如果有入参,最好指明入参的类型,虽然这并不是必须的

3  描述参数类型时,最好使用全限定名,可以通过IDE直接链接过去,便于查看

一个demo

	<select id="getCommentsOnCooperator" parameterType="java.lang.Integer"  resultType="com.enterprise.domain.CooperatorComments" >
		select * from hyip_task_cooperator_comments where user_id=#{merchantUserId}
	</select>

   酷酷酷

Q  mybatis获取刚插入数据的ID?

A   方式1 XML配置

<insert id="addMerchantPunishInfo" parameterType="java.util.Map">
		insert into hyip_merchant_punish(user_id,username,punish_reason,punish_desc,audit_user_id,create_time) 
		values(#{merchantUserId},#{userName},#{punishReason},#{punishDesc},#{auditUserId},NOW());
		<selectKey resultType="java.lang.Long" keyProperty="punishId">
	      	<![CDATA[
	        	SELECT last_insert_id();
	       	]]>
		</selectKey>
	</insert>

   方式2 XML配置

  

<insert id="addMerchantPunishInfo" parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="punishId">
		insert into hyip_merchant_punish(user_id,username,punish_reason,punish_desc,audit_user_id,create_time) values(#{merchantUserId},#{userName},#{punishReason},#{punishDesc},#{auditUserId},NOW());
	</insert>
	

 JAVA

	// 转成Map,插入除去处罚方式外的其它信息
		Map<String, Object> info = new HashMap<String, Object>();
		info.put("merchantUserId", userId);
		info.put("userName", userName);
		info.put("punishDesc", punishDesc);
		info.put("punishReason", punishReason);
		info.put("auditUserId", auditUserId);
		punishRepository.addMerchantPunishInfo(info);

		Long punishId = (Long) info.get("punishId"); // 获取插入记录的主键
		PunishMerchantResult punishResult = new PunishMerchantResult();

 

 

猜你喜欢

转载自curious.iteye.com/blog/2250062
今日推荐