使用Hibernate开发租房系统(10)

mybatis处理多表查询结果OR 聚合函数分组查询
a.建立和查询结果映射匹配的实体类对象 (假设遇到同名的列 一定要更换别名)
b.类似于hibernate的多对一解决
需要在mapper文件中配置resultMap
注意同名列
c.类似于hibernate的一对多解决
需要在mapper文件中配置resultMap
注意同名列

双向关联 无需再配置关联对象的自身

transactionManager:事务管理
dataSource:数据源

keyProperty:对应主键的属性
resultType:主键的数据java类型
order="BEFORE": 首先在执行保存语句前 先执行selectKey中的内容 将结果返回到对应的keyProperty的属性上

如果返回是一个游标 那么mybatis需要 完成resultMap配置 mybatis作为orm映射框架
最好将select标签换成别的
mybatis底层执行有一个bug,类似于死循环的形式呈现
解决方法:
①select换成update
②换jar包

关联属性的映射 多对一:association

如果完成类似级联操作 主表中的id主键 生成策略不是由程序指定 而是依赖数据库完成
①修改主表对象的Mapper文件
②在保存功能语句中添加一个标签

 

 

Mybatis详细配置(个人亲自实验整理,不一定适用全体):

1、首先,我们需要建一个大的项目,比如mybatis
右击 New→Other(或者快捷键Ctrl+N)
选择Web 下面的 Dynamic Web Project
下面是重点:
Project name填项目名 比如mybatis
Target runtime 选6.0
version 选2.4
还有一个是配置tomcat的,注意一下
WebContent→WEB-INF WEB-INF 下面lib里面的jar包不要忘了


2、建表
实体类
dao层
Mapper配置步骤:
前提条件:
Window→Preferences→xml→XML Catalog
1.http://mybatis.org/dtd/mybatis-3-config.dtd config DTD
2.http://mybatis.org/dtd/mybatis-3-mapper.dtd MAPPER dtd

把两个dtd文件分别放进去
注意点是 Key type: URI,其他没有 Key就复制上面的1,2就行了,不要有空格


接下来:
src目录下 右击 选择New→Other
输入xml 选XML File mybatis-config.xml
(
<configuration>
<!-- environments default:默认的环境名 -->
<environments default="oracle">
<!-- id:随便取一个名字 -->
<environment id="oracle">
<!-- transactionManager:事务管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- dataSource:数据源 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="system"/>
<property name="password" value="accp"/>
</dataSource>

</environment>
</environments>
<mappers>
<mapper resource="com/zb/dao/CustomerMapper.xml"/>
</mappers>
</configuration>
)

 

以上是config文件的配置,接下来是mapper文件的配置


前面的步骤基本相同,不同的是
右击 比如CustomerDao 选择。。。

(
<mapper namespace="com.zb.dao.CustomerDao">
<!-- id:对应dao层的方法名
parameterType:参数类型
-->
<insert id="saveCustomer" parameterType="com.zb.entity.Customer">
insert into customer values(#{id},#{customername},#{address},#{age})
</insert>

<select id="findById" parameterType="java.lang.Integer" resultType="com.zb.entity.Customer">
select * from customer where id = #{id}
</select>

<update id="updateCustomer" parameterType="com.zb.entity.Customer">
update customer set age=#{age},address=#{address},customername=#{customername} where id=#{id}
</update>

<delete id="deleteCustomer" parameterType="java.lang.Integer">
delete from customer where id=#{id}
</delete>

<select id="findAll" resultType="com.zb.entity.Customer">
select * from customer
</select>


<select id="findByCondition1" parameterType="java.lang.Integer" resultType="com.zb.entity.Customer">
select * from customer where age>#{minage}
</select>


<select id="findByCondition2" parameterType="java.util.Map" resultType="com.zb.entity.Customer">
<![CDATA[
select * from customer where age>#{minage} and age<#{maxage}
]]>
</select>

<select id="findByCondition3" parameterType="java.util.Map" resultType="com.zb.entity.Customer">

select * from customer where 1=1
<if test="minage!=null">
and age > #{minage}

</if>
<if test="maxage!=null">
<![CDATA[
and age <#{maxage}
]]>
</if>
<if test="eqage!=null">
and age=#{eqage}
</if>

</select>
</mapper>
)

 

posted on 2017-03-31 00:53  凌雨轩林  阅读(540)  评论(0编辑  收藏  举报

导航