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",会报异常:
- com.mysql.jdbc.MysqlDataTruncation: Data truncation:xxxxx
给截断了,经过一翻google后,发现以下这代话:
- 对于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">
分享到:
相关推荐
一个简略的代码生成工具,能快速生成ssm框架等集成mybatis所需的实体bean+dao接口+xml文件等
主要介绍了mybatis映射XML文件详解及实例的相关资料,需要的朋友可以参考下
mybatis映射文件自动生成工具,在generator.xml配置数据表和连接参数,命令行运行generator核心jar文件,包含命令语句
自动生成mybatis映射文件工具; 步骤: 1》打开generatorConfig.xml文件,配置自己的数据库连接参数 2》配置实体类、mapper映射文件、mapper接口 3》:回到根目录,按住Shif+鼠标右键(即在当前目录打开命令行),...
eclipse中Mybatis与mapper.xml映射文件的关联,直接点击类上面的方法,就可以跳转相应的方法的位置
jar包,maven项目中,使用mybatis和mysql,自动根据数据库表生成xml文件和映射类...
在配置文件中配置数据库信息,里面有很详细的使用说明和配置注释。相信大家都会用 然后就可以自动生成mybatis相关的dao bean xml 生成
Mybatis自动映射工具,该工具可自动生成相应的映射文件(xml文件、java文件)
支持在mysql数据库下导出mybatis xml映射文件(个人原创,如有不足,欢迎站内M)
用idea开发springboot,整合mybatis,配置多个数据源,动态切换数据源demo
基本编写mybatis 高级映射,使用xml方式编写mapper文件,关联嵌套和鉴别器的使用,内有sql文件,可以直接导入项目使用,注意:sql文件格式为UTF-8,项目格式为GBK
读取mysql数据库,创建所有的Entity实体类,实体类里面有对应的注释,...mapper.xml文件里面配置了resultMap,实现字段映射 mapper.xml文件里面配置了insert语句,两个配置为了当数据特别多的时候,免去很多工作量。
mybatis – MyBatis 3 _ XML 映射器.html
mybatis自动生成SQL,生成xml映射文件,实体,修改generator_oracle_apply.xml路径,当当前目录下打开cmd 运行txt里面语句。完成。
里面包含说明使用方法,主要是mybatis生成mysql 数据库表并生成mapper对象和xml,此jar包请解压后,查看源码
generatorSqlmapCustom 逆向工程生成代码,mybatis映射文件,xml 与接口,与实体类自动生成,用法简单,只需要修改链接数据库的方式就可以使用了(私聊我免费给你分享)
压缩包下面有是java代码,配置你数据库地址,用户名等信息后,执行代码,可以在设置的路径下面得到关于SSM框架的文件目录以及对应的bean和mapper.xml,主要提高开发效率。
mapper的映射文件,与类相对应。
2018-05-25 上传大小:1.87MB 数据库自动生成pojojava 解压配置好generator.xml后,打开cmd,使用(说明.txt)中的命令。...就可以生成实体类和mybatis映射,dao等文件,这样就可以根据数据库表自动生成java实体类~~
初次使用mybatis映射机制,很好用。文件生成后对于单表操作的SQL是可以直接调用的,不在xml文件中显示。