ssplyh 发表于 2016-11-26 05:09:20

mybatis 映射xml的一些问题

  1:xml中的查询语句有to_date()时传入的时间不用单引号

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

and t.alarm_time<!]>=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代码  





[*]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]
查看完整版本: mybatis 映射xml的一些问题