mybatis第二天
mapper高级查询之符号处理
可以用 > < 等进行转义,也可以用CDATA,例如
<![CDATA[
and salePrice > #{minPrice} and salePrice <= #{maxPrice}
]]>
结果映射处理
当查询列字段和domain字段不一样时:
(1)可以使用别名解决
<select id="findAll" resultType="product">
select id,productName pName,salePrice,costPrice,cutoff from product
</select>
(2)使用Map方式 解决 类里面字段和数据库查询出来字段不一致
<select id="findAll" resultMap="productMap">
select id,productName,salePrice,costPrice,cutoff from product
</select>
<resultMap id="productMap" type="product">
<!-- id:主键
property:类里面的属性
column:查询列名称
-->
<id property="id" column="id"></id>
<result property="pName" column="productName"></result>
<result property="salePrice" column="salePrice"></result>
</resultMap>
关系处理
多对一:
<resultMap id="productMap" type="product">
<id column="id" property="id"></id>
<result column="productName" property="pName"></result>
<!-- 处理一方-->
<!--<association property="dir" javaType="productDir">
<id column="did" property="id"></id>
<result column="dname" property="dirName"></result>
</association>-->
<!-- 扩展1-->
<result property="dir.id" column="did"></result>
<result property="dir.dirName" column="dname"></result>
</resultMap>
<!--(1)嵌套结果 发送一条sql语句-->
<select id="findAll" resultMap="productMap">
SELECT
p.id,
p.productName,
dir.id did,
dir.dirName dname
FROM
product p
JOIN productDir dir ON p.dir_id = dir.id
</select>
一对多
<select id="findAll" resultMap="productDirMap">
select dir.id ,dir.dirName from productDir dir limit 0,2
</select>
<resultMap id="productDirMap" type="productDir">
<id property="id" column="id"></id>
<result property="dirName" column="dirName"></result>
***<collection property="products" column="id" ofType="product" select="selectProducts">
</collection>***
</resultMap>
<select id="selectProducts" parameterType="long" resultType="product">
select id,productName pName,salePrice,costPrice,cutoff
from product where dir_id = #{dir_id}
</select>
ssm整合
ssm – springmvc+spring+mybatis 中型项目/大型项目
第一步:
(1)创建项目–web项目 (maven/普通web)
(2)导入三个框架的jar包
(3)配置文件 applicationContext.xml --配置spring+mybatis
applicationCotnext-mvc.xml --配置springmvc
db.properties --配置库
web.xml --配置web的内容
log4j.properties
applicationContext 之 mybatis部分:
<!--sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"></property>
<property name="typeAliasesPackage">
<value>
cn.itsource.ssm.domain
</value>
</property>
</bean>
<!-- 把产生mapper 交给 spring-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.ssm.mapper"></property>
</bean>
扩展
什么是序列化:
把对象转换成二进制的信息 这个过程
为什么需要序列化:
用在网络传输