最近在做java后台所遇到的问题和总结,本文针对的是springboot+springmvc+mybatis 环境下的,所用的数据库是oracle。
一、普通查询,参数为map,注解和方法的例子如下:
@Select("select ID,BIDas bid,CUSTOMER_BUS_ID as CBID," +
"MD5,OWNERID,STATUS,FIELD_NAME as fieldName,FIELD_INDEX as fieldIndex," +
"FIELD_CONTENT as fieldContent, CREATE_TIME as createTime,UPDATE_TIME " +
"as updateTime,SPARE1,SPARE2,SPARE3 from 表名 t where t.ownerada = #{ownerID} and t.business_id = #{bId}")
public AEntity findCustomerFieldByMap(Map
注意:
1.有数据库的字段名是有下划线的,而实体类的属性是按照java命名规则来命名的,这就导致了数据库的字段名和实体的属性名不一致,所以要使用as别名才可以查出结果,否则就会出现数据库有数据,但是你查询的时候是查不出来的;例如:FIELD_NAME as fieldName 。
2.由于查询的参数是map,所以查询条件所传入的值是map的key,例如:t.ownerada = #{ownerID}
3.因为数据库的字段名和实体的属性名不一致,所以不能使用select * 查询。
二、in查询,参数为map,注解和方法的例子如下:
@Select("select ID,BIDas businessId,CBID as customerBusId," +
"MD5,OWNERID,STATUS,FIELD_NAME as fieldName,FIELD_INDEX as fieldIndex," +
"FIELD_CONTENT as fieldContent, CREATE_TIME as createTime,UPDATE_TIME " +
"as updateTime,SPARE1,SPARE2,SPARE3 from mstb_crm_customer_field t " +
"where t.CBID in ${customerBusId}")
public List
注意:在用in查询的时候,参数要使用$,而不是#。其他要注意的和普通查询一样。
三、插入数据,参数为对象,注解和方法的例子如下:
@Insert("INSERT INTO mstb_crm_customer_field(ID,BID,CBID," +
"MD5,OWNERADA,STATUS,FIELD_NAME,FIELD_INDEX,FIELD_CONTENT," +
"CREATE_TIME,UPDATE_TIME,SPARE1,SPARE2,SPARE3)" +
"VALUES(MSTB_CRM_CUSTOMER_FIELD_SEQ.nextval," +
"#{businessId,jdbcType=VARCHAR}," +
"#{customerBusId,jdbcType=VARCHAR}," +
"#{md5,jdbcType=VARCHAR}," +
"#{ownerada,jdbcType=VARCHAR}," +
"#{status,jdbcType=INTEGER}," +
"#{fieldName,jdbcType=VARCHAR}," +
"#{fieldIndex,jdbcType=INTEGER}," +
"#{fieldContent,jdbcType=VARCHAR}," +
"#{createTime,jdbcType=TIMESTAMP}," +
"#{updateTime,jdbcType=TIMESTAMP}," +
"#{spare1,jdbcType=VARCHAR}," +
"#{spare2,jdbcType=VARCHAR}," +
"#{spare3,jdbcType=VARCHAR})")
public void insert(MstbCrmCustomerField customerField) ;
注意:在插入的时候有些值允许为空的,则要加上jdbcType,这样在插入的时候才不会报错;不允许为空的可以不加jdbcType,但是防止出错和以后数据维护,建议所有的参数都加上jdbcType。
四、修改数据,参数为对象,注解和方法的例子如下:
@Update("UPDATE mstb_crm_customer_field SET BID=#{businessId,jdbcType=VARCHAR}," +
"CBID=#{customerBusId,jdbcType=VARCHAR}," +
"MD5=#{md5,jdbcType=VARCHAR}," +
"OWNERADA=#{ownerada,jdbcType=VARCHAR}," +
"STATUS=#{status,jdbcType=INTEGER}," +
"TITLE=#{fieldName,jdbcType=VARCHAR}," +
"WORK_DATE=#{fieldIndex,jdbcType=INTEGER}," +
"CONTENT=#{fieldContent,jdbcType=VARCHAR}," +
"CREATE_TIME=#{createTime,jdbcType=TIMESTAMP}," +
"UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP}," +
"SPARE1=#{spare1,jdbcType=VARCHAR}," +
"SPARE2=#{spare2,jdbcType=VARCHAR}," +
"SPARE3=#{spare3,jdbcType=VARCHAR}" +
" WHERE ID=#{id,jdbcType=INTEGER}")
public void update(MstbCrmCustomerField customerField) ;
注意:修改要注意的和新增的一样。