mybatis_day02

2.映射器Mapper

相当于dao,不用写实现类(mybatis底层会采用动态代理模式 会跟我生成实现)

步骤:

(1) 创建项目 配置mybatis-config.xml 和昨天一样

(2) 创建接口 ProductMapper (XXXXMapper) 里面定义一个方法findAll -->以前dao层/mapper层

(3) 在对应的ProductMapper.xml namespace 和

标签里面id (namespace+id) == (ProductMapper包路径+方法)

<select id="findAll"  resultType="product">

        select * from product

</select>

3.高级查询

3.1 like查询

方式一(存在sql注入的问题)

 

<if test="productName != null">

                and productName like '%${productName}%'

</if>

方式二:

<if test="productName != null">

                and productName like concat('%',#{productName},'%')

</if>

3.2特殊符号转译

方式一:

 

 

Eq <if test="minPrice != null and  maxPrice != null">

                and salePrice > #{minPrice} and salePrice < #{maxPrice}

            </if>

方式二(CDATA)

<![CDATA[ ]]>

Eq:<if test="minPrice != null and  maxPrice != null">

       <![CDATA[

           and salePrice > #{minPrice} and salePrice <= #{maxPrice}

       ]]>

   </if>

4.结果映射

处理表字段和domain的对象字段不一致的情况

4.1 使用别名

<select id="findAll"  resultType="product">

            select id,productName pName,salePrice,costPrice,cutoff from product

 </select>

4.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>

5.关系处理

5.1 对一方处理

嵌套结果: 只发送一条sql

嵌套查询: 发送 1+n条sql

<?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="cn.itsource._03_manytoone.mapper.ProductMapper">

 

   <!-- <resultMap id="productMap" type="product">

        <id column="id" property="id"></id>

        <result column="productName" property="pName"></result>

        <result column="salePrice" property="salePrice"></result>

        <result column="costPrice" property="costPrice"></result>

        <result column="cutoff" property="cutoff"></result>

        <!– 处理一方–>

        <association property="dir" javaType="productDir">

            <id column="did" property="id"></id>

            <result column="dname" property="dirName"></result>

        </association>

 

    </resultMap>-->

    <!-- (1)嵌套结果 发送一条sql语句-->

   <!-- <select id="findAll"  resultMap="productMap">

       SELECT p.id, p.productName, p.salePrice, p.costPrice, p.cutoff, dir.id did, dir.dirName  dname

        FROM

            product p

        JOIN productDir dir ON p.dir_id = dir.id

    </select>-->

    <!-- 嵌套查询:发送多sql语句(1+nsql)-->

 

    <select id="findAll"  resultMap="productMap">

        SELECT

        p.id,p.productName,p.salePrice, p.costPrice,p.cutoff,p.dir_id

        FROM product p

    </select>

    <resultMap id="productMap" type="product">

        <id column="id" property="id"></id>

        <result column="productName" property="pName"></result>

        <result column="salePrice" property="salePrice"></result>

        <result column="costPrice" property="costPrice"></result>

        <result column="cutoff" property="cutoff"></result>

        <!--根据上面查询dir_id 在去查询 分类对象-->

        <association property="dir" column="dir_id" javaType="productDir" select="selectDir">

        </association>

    </resultMap>

    <select id="selectDir" parameterType="long" resultType="ProductDir">

        select * from productDir where id = #{dir_id}

    </select>

</mapper>

5.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="cn.itsource._04_onetomany.mapper.ProductDirMapper">

 

    <!-- 嵌套结果-->

   <!-- <select id="findAll" resultMap="productDirMap">

        select dir.id ,dir.dirName,

                p.id pid,p.productName pName,p.salePrice,p.costPrice,p.cutoff

        from productDir dir join product p

        on dir.id = p.dir_id

    </select>

    <resultMap id="productDirMap" type="productDir">

        <id property="id" column="id"></id>

        <result property="dirName" column="dirName"></result>

        <collection property="products" ofType="product">

            <id property="id" column="pid"></id>

            <result property="pName" column="pName"></result>

            <result property="salePrice" column="salePrice"></result>

            <result property="costPrice" column="costPrice"></result>

        </collection>

    </resultMap>-->

    <!-- 嵌套查询-->

    <select id="findAll" resultMap="productDirMap">

        select dir.id ,dir.dirName from productDir dir

    </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>

</mapper>

6.SSM整合

6.1 整合步骤

(1)创建项目--web项目 (maven/普通web)

(2)导入三个框架的jar包

(3)配置文件

applicationContext.xml --配置spring+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>

posted @ 2019-12-28 11:33  dyier  阅读(144)  评论(0编辑  收藏  举报