MyBatis中映射文件的记录

映射文件就是实现Dao接口的Dao.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">

//Dao接口所在的路径
<mapper namespace="dao.HouseDao">

//设定resultMap
<resultMap type="House" id="selectHouses">

//column为数据库中的字段名,property为实体对象的属性名,这里是为了将字段名和属性名作一一对应
<result column="TITLE" property="TITLE"/>
<result column="DESCRIPTION" property="DESCRIPTION"/>
<result column="PRICE" property="PRICE"/>
<result column="PUBDATE" property="PUBDATE"/>
<result column="FLOORAGE" property="FLOORAGE"/>
<result column="CONTACT" property="CONTACT"/>
<result column="IMAGE" property="IMAGE"/>

//这是当字段名对应的属性是一个实体对象时的做法  需要Dao接口中有对应的方法,并且Dao.xml中有实现的方法时才能生效
<association property="users" column="USER_ID" select="dao.UsersDao.selectUsersById"/>
<association property="types" column="TYPE_ID" select="dao.TypesDao.selectTypesById"/>
<association property="street" column="STREET_ID" select="dao.StreetDao.selectStreetById"/>
</resultMap>
//sql语句的具体操作:parameterType为传入的数据的类型,resultType为返回值的类型,resultMap为返回值为你自己设定的resultMap
<select id="selectHouseByIndex" parameterType="PageUtil" resultMap="selectHouses">
select * from house limit #{pageIndex},#{everyPageMessage}
</select>
<select id="selectMessageCount" resultType="int">
select count(*) from house
</select>

//#{xxx}这种情况,占位符中的字母必须要和传入的实体对象中的属性名相同,如果属性又是一个实体对象,就需要如:type_id=#{types.ID}的写法
<update id="updateByHouse" parameterType="House" >
update house set type_id=#{types.ID},title=#{TITLE},price=#{PRICE},pubdate=#{PUBDATE},floorage=#{FLOORAGE},contact=#{CONTACT},street_id=#{street.ID},image=#{IMAGE} where id=#{ID}
</update>
<delete id="deleteByHouseId" parameterType="int">
delete from house where id = #{houseId}
</delete>
<select id="selectByUsersId" parameterType="int" resultMap="selectHouses">
select * from house where user_id = #{usersId}
</select>
<select id="selectById" parameterType="int" resultMap="selectHouses" >
select * from house where id = #{id}
</select>
<select id="selectAllHouse" resultMap="selectHouses">
select * from house
</select>

//条件查询 传入的对象类型为Map ,Map中的key对应<if test="street_id!=null">中的street_id,Map中的value对应#{street_id}
<select id="selectHouseByMap" parameterType="map" resultMap="selectHouses">
select * from house 
<where>
<if test="street_id!=null">
and street_id = #{street_id}
</if>
<if test="title!=null">
and title like #{title}
</if>
<if test="type_id!=null">
and type_id = #{type_id}
</if>
<if test="priceDown!=null">
and price >= #{priceDown}
</if>
<if test="priceTop!=null">
and price &lt; #{priceTop}
</if>
<if test="floorDown!=null">
and floorage >= #{floorDown}
</if>
<if test="floorTop!=null">
and floorage &lt; #{floorTop}
</if>
</where>
</select>
<insert id="addHouse" parameterType="House">
insert into house values (null,#{users.ID},#{types.ID},#{TITLE},#{DESCRIPTION},#{PRICE},#{PUBDATE},#{FLOORAGE},#{CONTACT},#{street.ID},#{IMAGE})
</insert>
</mapper> 


参照模型:


<?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="dao.HouseDao">
<resultMap type="House" id="selectHouses">
<result column="TITLE" property="TITLE"/>
<result column="DESCRIPTION" property="DESCRIPTION"/>
<result column="PRICE" property="PRICE"/>
<result column="PUBDATE" property="PUBDATE"/>
<result column="FLOORAGE" property="FLOORAGE"/>
<result column="CONTACT" property="CONTACT"/>
<result column="IMAGE" property="IMAGE"/>
<association property="users" column="USER_ID" select="dao.UsersDao.selectUsersById"/>
<association property="types" column="TYPE_ID" select="dao.TypesDao.selectTypesById"/>
<association property="street" column="STREET_ID" select="dao.StreetDao.selectStreetById"/>
</resultMap>

<select id="selectHouseByIndex" parameterType="PageUtil" resultMap="selectHouses">
select * from house limit #{pageIndex},#{everyPageMessage}
</select>
<select id="selectMessageCount" resultType="int">
select count(*) from house
</select>
<update id="updateByHouse" parameterType="House" >
update house set type_id=#{types.ID},title=#{TITLE},price=#{PRICE},pubdate=#{PUBDATE},floorage=#{FLOORAGE},contact=#{CONTACT},street_id=#{street.ID},image=#{IMAGE} where id=#{ID}
</update>
<delete id="deleteByHouseId" parameterType="int">
delete from house where id = #{houseId}
</delete>
<select id="selectByUsersId" parameterType="int" resultMap="selectHouses">
select * from house where user_id = #{usersId}
</select>
<select id="selectById" parameterType="int" resultMap="selectHouses" >
select * from house where id = #{id}
</select>
<select id="selectAllHouse" resultMap="selectHouses">
select * from house
</select>
<select id="selectHouseByMap" parameterType="map" resultMap="selectHouses">
select * from house 
<where>
<if test="street_id!=null">
and street_id = #{street_id}
</if>
<if test="title!=null">
and title like #{title}
</if>
<if test="type_id!=null">
and type_id = #{type_id}
</if>
<if test="priceDown!=null">
and price >= #{priceDown}
</if>
<if test="priceTop!=null">
and price &lt; #{priceTop}
</if>
<if test="floorDown!=null">
and floorage >= #{floorDown}
</if>
<if test="floorTop!=null">
and floorage &lt; #{floorTop}
</if>
</where>
</select>
<insert id="addHouse" parameterType="House">
insert into house values (null,#{users.ID},#{types.ID},#{TITLE},#{DESCRIPTION},#{PRICE},#{PUBDATE},#{FLOORAGE},#{CONTACT},#{street.ID},#{IMAGE})
</insert>
</mapper> 

</mapper> 


参考模型2:

<?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="接口所在的路径o">
<select id="跟接口的方法名相同" parameterType="传入值类型" resultType="实体类对象">
select * from users where id = #{id}
</select>
</mapper> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值