设为首页 收藏本站
查看: 1086|回复: 0

[经验分享] mybatis 映射xml的一些问题

[复制链接]

尚未签到

发表于 2016-11-26 05:09:20 | 显示全部楼层 |阅读模式
  1:xml中的查询语句有to_date()时传入的时间不用单引号

to_date(#{startTime},'yyyy-mm-dd hh24:mi:ss')
  2:一些特殊符号的处理:<![CDATA[]]>

and t.alarm_time<![CDATA[<]]>=to_date(#{endTime},'yyyy-mm-dd hh24:mi:ss')
  3:使用if判断时,空值可以这样判断

<if test="endTime !=null and endTime !='' ">
  4:传递多个参数据 (一种是用map ,一种是用bean(略))

<!--
使用HashMap传递多个参数
parameterType 可以是别名或完全限定名 ,map->java.util.Map,这两个都是可以的
-->
<select id="selectBlogByMap" parameterType="map" resultType="Blog">
SELECT t.ID, t.title, t.content
FROM blog t
WHERE t.title = #{h_title}
AND t.content =#{h_content}
</select>
<!-- 使用JavaBean传递多个参数 -->
<select id="selectBlogByBean" parameterType="Blog" resultType="Blog">
SELECT t.ID, t.title, t.content
FROM blog t
WHERE t.title = #{title} <!--名字要与bean中的一致,大小写也要一致-->
AND t.content =#{content}
</select>

/**
* 通过Map传递多个参数
*/
@Test
public void testSelectByMap() {
SqlSession session = sqlSessionFactory.openSession();
Map<String, Object> param=new HashMap<String, Object>();
param.put("h_title", "oracle");
param.put("h_content", "使用序列!");
Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param);
session.close();
System.out.println("blog title:"+blog.getTitle());
}
/**
* 通过JavaBean传递多个参数
*/
@Test
public void testSelectByBean() {
SqlSession session = sqlSessionFactory.openSession();
Blog blog=new Blog();
blog.setTitle("oracle");
blog.setContent("使用序列!");
Blog newBlog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByBean",blog);
session.close();
System.out.println("new Blog ID:"+newBlog.getId());
}
  使用注解

Mapper

public List<BigCustomerSumBandwidth> getBigCustomerSumByBigCustomerId(@Param("id") String id,
@Param("startTime") Date startTime);
  xml

<select id="getBigCustomerSumByBigCustomerId"
resultType="com.chinacache.entity.BigCustomerSumBandwidth">
select * from big_customer_sumbandwidth where
big_customer_id=#{id} and day>=#{startTime,jdbcType=DATE}
</select>
  5:使用缓存(二级缓存)
  在spring的配置文件中加入mybatis的配置文件

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

mybatis的配置文件

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE configuration  
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" />
</settings>
</configuration>
 map.xml的缓存设配

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chinacache.mapper.util.ChannelConfig">
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />
 6:映射时间类型(转)

在字段中有Date和DateTime类型,在插入数据时只要将实体的属性设置成Timestamp就会对应mysql的DateTime类型,Date会对应mysql的Date类型。

在MyBatis映射文件中要表明映射类型

<result column="modified_date" jdbcType="TIMESTAMP" property="modified_date" javaType="java.sql.Timestamp" />
<result column="date" jdbcType="DATE" property="date" javaType="java.util.Date" />

在使用字段的时候也要标明类型#{modified_date,jdbcType=TIMESTAMP}、#{date,jdbcType=DATE}。

别外在处理char类型时会出错,尽量用varchar2

保存完整时间

在用mybatis时,对mysql数据库是datatime字段添加值是,发现添加成功后查看数据库字段值是,只有年月日有值,时分秒则为0来表示的,更改为java.sql.date,time等也不行,如果将mybatis的映射xml的jdbcType="DATE"改为="TIME",会报异常: 

Java代码   DSC0000.png





  • com.mysql.jdbc.MysqlDataTruncation: Data truncation:xxxxx  



给截断了,经过一翻google后,发现以下这代话: 

Java代码  





  • 对于Ibatis操作Date/Time/DateTime,总结如下:  
  • 将pojo的属性类型设置为java.sql.Date(或java.sql.Time, java.sql.Timestamp),此时会严格遵循这三种类型的语义。但此方法因存在前文中提到的性能问题,在JDK1.6以前的JDK版本中能少使用就少使用。  
  • 如果你想在pojo中使用java.util.Date, 则要注意:  
  • 完整的日期时间,要确保jdbcType为空,或为DATE,TIME以外的值  
  • 只需要时间,要指定jdbcType=”TIME”  
  • 只需要日期,要指定jdbcType=”DATE”  



于是将映射的XML文件里的,jdbcType="DATE"这段代码清了就可以了。特此记录 

 7:There is no getter for property named '**' in 'class java.lang.String (参数用_parameter代替,sql中用${_parameter } )

<select id="getBigCustomerLimitByBu" resultType="com.chinacache.entity.BigCustomer"
parameterType="java.lang.String">
<if test="_parameter == 'SOC' ">
<![CDATA[
select t.* from (select * from big_user_config
where rownum<=15 order by order_band) t where rownum<=15
]]>
</if>
<if test="_parameter != 'SOC'">
<![CDATA[
select t.* from (select * from big_user_config
where
bu=#{_parameter} and rownum<=15 order by order_band) t where rownum<=15
]]>
</if>
</select>
 8:MyBatis中出现Mapped Statements collection does not contain value异常解决方案

1、mapper.xml中没有加入namespace
2、mapper.xml中的方法和接口mapper的方法不对应
3、mapper.xml没有加入到mybatis-config.xml中(即总的配置文件),例外:配置了mapper文件的包路径的除外
4、mapper.xml文件名和所写的mapper名称不相同。

 9:attempted to return null from a method with a primitive return type (long).

我xml中映射 resultType="java.lang.Long" ,但接收这个方法使用了long,这样就会报这个错误。别外还要注意mapper的方法返回值类型也要是Long

10:模糊查询

<select id="selectByName" parameterType="String" resultType="Student">
select * from Student s where s.name like "%"#{name}"%";
</select>

 11:复用

定意sql(名字:CHOSEN_CARS_WHERE_CONDITION)


<sql id="CHOSEN_CARS_WHERE_CONDITION">
<where>
<if test="bindHw != -1">AND c.hardware=#{bindHw}</if>
</where>
</sql>

 使用它(<include refid=""/>)


<select id="getCarAndHwCount" parameterType="com.autoyolConsole.vo.CarSearchCondition"
resultType="int">
SELECT COUNT(c.id) FROM car c JOIN member m ON c.mem_no=m.reg_no
<include refid="CHOSEN_CARS_WHERE_CONDITION" />
</select>

 12:Document root element "mapper", must match DOCTYPE root "configuration"


mybatis.xml和mapper.xml都用了一个doctype
改回来即可

mybatis.xml:

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

mapper.xml

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305522-1-1.html 上篇帖子: mybatis自动生成代码配置文件 下篇帖子: Spring+Mybatis 多数据源配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表