2 sen linzhiwang sen_linzhiwang 于 2015.05.28 22:45 提问

MyBatis如何执行动态多表关联的sql

Mybatis实现动态sql的查询,这个sql不固定,可能会查多个表的多个字段。如何设计一个通用的方式,可以查询任意sql?目前我实现可以查询的,也是基于一个实体来查询,而查询的内容,与这个实体无任何关系,这样虽然也能够查,但是个人觉得不够专业。希望得到大家的解答。
谢谢

2个回答

jiuqiyuliang
jiuqiyuliang   2015.05.29 09:18
u012083961
u012083961   2015.05.29 10:20

<?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">













<select id="getCountAllHouses" resultType="int">
    select count(*) from House
</select>
<select id="getAllHouses" resultMap="houseResultMap">
    select * from House
</select>
<select id="getHouses" resultMap="houseResultMap">
    select * from house where ID=#{id}
</select>
<insert id="insertHouse" parameterType="House">
    <selectKey keyProperty="ID" resultType="int" order="BEFORE">
        select seq_id.nextVal from dual
    </selectKey>
    insert into house(ID,USER_ID,TYPE_ID,TITLE,DESCRIPTION,PRICE,PUBDATE,FLOORAGE,CONTACT,STREET_ID)
    values
        (#{id},#{userId},#{typeId},#{title},#{description},#{price},#{pubdate},#{floorage},#{contact},#{streetId})
</insert>
<delete id="deleteHouse" parameterType="int">
    delete from house where ID=#{id}
</delete>
<update id="updateHouse" parameterType="House">
    update house set USER_ID=#{userId},TYPE_ID=#{typeId},TITLE=#{title},DESCRIPTION=#{description},
        PRICE=#{price},PUBDATE=#{pubdate},FLOORAGE=#{floorage},CONTACT=#{contact},STREET_ID=#{streetId}
    where ID=#{id}
</update>
<select id="findHouseByExampleif" parameterType="House" resultType="House">
    select * from house where 1=1
    <if test="price != null">
        and PRICE <![CDATA[ > ]]> #{price}
    </if>
    <if test="pubdate != null">
        and PUBDATE = #{pubdate}
    </if>
    <if test="title != null">
        and TITLE = #{title}
    </if>
</select>

基础查询语句的配置

Csdn user default icon
上传中...
上传图片
插入图片