MyBatis——3

简介: MyBatis笔记,参考文档: 多表查询:http://blog.csdn.net/happylee6688/article/details/45967763;

mybatis再xml文件中处理大于号小于号的方法

方法一:

用转义字符把>和<替换掉

XML转义字符

57cf835068962fecf9ff3cd9e2b4ae574475c246

44ad2dc441b295eb11ace0122720bf26657ebf5d

方法二:

使用<![CDATA[ ]]>符号进行说明,将此类符号不进行解析

600f5a644f9e4d57cbedcafb7f5c5f1294550781


MyBatis错误:Parameter 'xxx' not found.Available parameters are [1,0,param1,param2]

原本用的是名称匹配发现不了,换成0,1,2这样的方式序列匹配;如果还是解决不了的话可能是参数类型不一致或者其中参数为NULL。


MyBatis多表联合查询及优化

表关系

user(id,username,password,gmt_create,gmt_modify)

role(id,name,userid)

user,role:一对多

User和Role的实体类代码:

1dee1661abc13e98f2dfe552bfdb292d5f6caa96726eba52d94073bc1e7601a8f0e93e0e96bddf79

在User的实体中加入一个Role的属性,对应一对多的关系。


mapper 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="src.bonc.lbs.wt.dao.business.UserDao" > 
<!-- Result Map-->
<resultMap id="BaseResultMap" type="src.bonc.lbs.wt.entity.business.User" >
	<result column="id" property="id"/>
	<result column="username" property="username"/>
	<result column="password" property="password"/>
	<result column="gmt_create" property="gmt_create"/>
	<result column="gmt_modify" property="gmt_modify"/>
</resultMap>

<!-- Result Map -->
<resultMap type="src.bonc.lbs.wt.entity.business.User" id="queryForListMap">
	<id column="id" property="id"/>
	<result column="username" property="username"/>
	<result column="password" property="password"/>
	<collection property="roles" javaType="java.util.List" ofType="src.bonc.lbs.wt.entity.business.Role">
		<id column="r_id" property="id" jdbcType="VARCHAR" />  
        <result column="r_name" property="name" jdbcType="VARCHAR" /> 
	</collection>
</resultMap>
       
<!-- user table all fields -->
<sql id="Base_Column_List" >
	 id,username,password,gmt_create,gmt_modify
</sql>
   
   
<!-- 查询条件 -->
<sql id="Example_Where_Clause">
where 1=1
<trim  suffixOverrides="," >
	<if test="id != null and id != ''" >
	    and id =  #{id}
	</if>
	<if test="username != null and username != ''" >
	    and username =  #{username}
	</if>
	<if test="password != null and password != ''" >
	    and password =  #{password}
	</if>
</trim>
</sql>
   

<!-- 插入记录 -->
<insert id="add" parameterType="Object" >
  insert into user(id,username,password)
 values(#{id},#{username},#{password})
</insert>

<!-- 根据id,修改记录-->  
 <update id="update" parameterType="Object" >
  update user set username=#{username},password=#{password} where id=#{id}
 </update>
 
 <!-- 修改记录,只修改只不为空的字段 -->
<update id="updateBySelective" parameterType="Object" >
	update user set 
	<trim  suffixOverrides="," >
	<if test="username != null  ">
		username=#{username},
	</if>
	<if test="password != null  ">
		password=#{password},
	</if>
	<if test="gmt_create != null  ">
		gmt_create=#{gmt_create},
	</if>
	<if test="gmt_modify != null  ">
		gmt_modify=#{gmt_modify},
	</if>
	</trim> where id=#{id}
</update>

<!-- 删除记录 -->
<delete id="delete" parameterType="Object">
	delete 	 from user where id = #{id}
</delete>
 
<!-- 根据id查询 用户 -->
<select id="queryById"  resultMap="BaseResultMap" parameterType="Object">
	select <include refid="Base_Column_List" /> 
	 from user where id = #{id}
</select>

<!-- 用户 列表总数-->
<select id="queryByCount" resultType="java.lang.Integer"  parameterType="Object">
	select count(1) from user 
	<include refid="Example_Where_Clause"/>
</select>
  	
<!-- 查询用户列表 -->
<select id="queryByList" resultMap="BaseResultMap"  parameterType="Object">
	select 
	<include refid="Base_Column_List"/>
	from user 
	<include refid="Example_Where_Clause"/>
	<if test="pager.orderCondition != null and pager.orderCondition != ''" >
      ${pager.orderCondition}
    </if>
    <if test="pager.mysqlQueryCondition != null and pager.mysqlQueryCondition != ''" >
       ${pager.mysqlQueryCondition}
    </if>
</select>

<select id="queryForList" resultMap="queryForListMap">
	SELECT u.id,u.username,u.password,r.id r_id,r.name r_name FROM user u LEFT JOIN role r ON u.id = r.userid
</select>

<select id="queryForListByCT" resultMap="queryForListMap">
	SELECT u.id,u.username,u.password,r.id r_id,r.name r_name FROM user u LEFT JOIN role r ON u.id = r.userid
	WHERE u.gmt_create > #{0} AND u.gmt_create < #{1} 
</select>
  	
</mapper>   


mapper接口

package src.bonc.lbs.wt.dao.business;


import java.util.List;

import src.bonc.com.base.dao.BaseDao;
import src.bonc.lbs.wt.entity.business.User;
/**
 * 
 * <br>
 * <b>功能:</b>UserDao<br>
 * <b>作者:</b>bonc<br>
 * <b>日期:</b> Feb 2, 2017 <br>
 * <b>版权所有:<b>版权所有(C) 2017,bonc.com.cn<br>
 */
public interface UserDao<T> extends BaseDao<T> {
	
	List<User> queryForList();
	
	List<User> queryForListByCT(String startTime, String endTime);
}














相关文章
|
2天前
|
数据采集 人工智能 安全
|
11天前
|
云安全 监控 安全
|
3天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1006 151
|
3天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话
|
16天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1699 9
|
8天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
643 152
|
10天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
613 13
|
9天前
|
人工智能 自然语言处理 API
Next AI Draw.io:当AI遇见Draw.io图表绘制
Next AI Draw.io 是一款融合AI与图表绘制的开源工具,基于Next.js实现,支持自然语言生成架构图、流程图等专业图表。集成多款主流大模型,提供智能绘图、图像识别优化、版本管理等功能,部署简单,安全可控,助力技术文档与系统设计高效创作。
687 151