关于实体类,DAO层,Mapper我想说点话

实体类中常用注解:
@Table
@Column
@Id

DAO层传递参数:
@Param

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="com.qfedu.fmmall.dao.CategoryMapper">

    <resultMap id="BaseResultMap" type="com.qfedu.fmmall.entity.Category">
        <id column="category_id" jdbcType="INTEGER" property="categoryId"/>
        <result column="category_name" jdbcType="VARCHAR" property="categoryName"/>
        <result column="category_level" jdbcType="INTEGER" property="categoryLevel"/>
        <result column="parent_id" jdbcType="INTEGER" property="parentId"/>
        <result column="category_icon" jdbcType="VARCHAR" property="categoryIcon"/>
        <result column="category_slogan" jdbcType="VARCHAR" property="categorySlogan"/>
        <result column="category_pic" jdbcType="VARCHAR" property="categoryPic"/>
        <result column="category_bg_color" jdbcType="VARCHAR" property="categoryBgColor"/>
    </resultMap>

    <resultMap id="categoryVOMap" type="com.qfedu.fmmall.entity.CategoryVO">
        <id column="category_id1" jdbcType="INTEGER" property="categoryId"/>
        <result column="category_name1" jdbcType="VARCHAR" property="categoryName"/>
        <result column="category_level1" jdbcType="INTEGER" property="categoryLevel"/>
        <result column="parent_id1" jdbcType="INTEGER" property="parentId"/>
        <result column="category_icon1" jdbcType="VARCHAR" property="categoryIcon"/>
        <result column="category_slogan1" jdbcType="VARCHAR" property="categorySlogan"/>
        <result column="category_pic1" jdbcType="VARCHAR" property="categoryPic"/>
        <result column="category_bg_color1" jdbcType="VARCHAR" property="categoryBgColor"/>
        <collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVO">
            <id column="category_id2" jdbcType="INTEGER" property="categoryId"/>
            <result column="category_name2" jdbcType="VARCHAR" property="categoryName"/>
            <result column="category_level2" jdbcType="INTEGER" property="categoryLevel"/>
            <result column="parent_id2" jdbcType="INTEGER" property="parentId"/>
            <collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVO">
                <id column="category_id3" jdbcType="INTEGER" property="categoryId"/>
                <result column="category_name3" jdbcType="VARCHAR" property="categoryName"/>
                <result column="category_level3" jdbcType="INTEGER" property="categoryLevel"/>
                <result column="parent_id3" jdbcType="INTEGER" property="parentId"/>
            </collection>
        </collection>

    </resultMap>

    <select id="selectAllCategories" resultMap="categoryVOMap">
        select c1.category_id       'category_id1',
               c1.category_name     'category_name1',
               c1.category_level    'category_level1',
               c1.parent_id         'parent_id1',
               c1.category_icon     'category_icon1',
               c1.category_slogan   'category_slogan1',
               c1.category_pic      'category_pic1',
               c1.category_bg_color 'category_bg_color1',
               c2.category_id       'category_id2',
               c2.category_name     'category_name2',
               c2.category_level    'category_level2',
               c2.parent_id         'parent_id2',
               c3.category_id       'category_id3',
               c3.category_name     'category_name3',
               c3.category_level    'category_level3',
               c3.parent_id         'parent_id3'
        from category c1
                 inner join category c2
                            on c2.parent_id = c1.category_id
                 left join category c3
                           on c3.parent_id = c2.category_id
        where c1.category_level = 1
    </select>

    <resultMap id="categoryVOMap2" type="com.qfedu.fmmall.entity.CategoryVO">
        <id column="category_id" jdbcType="INTEGER" property="categoryId"/>
        <result column="category_name" jdbcType="VARCHAR" property="categoryName"/>
        <result column="category_level" jdbcType="INTEGER" property="categoryLevel"/>
        <result column="parent_id" jdbcType="INTEGER" property="parentId"/>
        <result column="category_icon" jdbcType="VARCHAR" property="categoryIcon"/>
        <result column="category_slogan" jdbcType="VARCHAR" property="categorySlogan"/>
        <result column="category_pic" jdbcType="VARCHAR" property="categoryPic"/>
        <result column="category_bg_color" jdbcType="VARCHAR" property="categoryBgColor"/>
        <collection property="categories" column="category_id"
                    select="com.qfedu.fmmall.dao.CategoryMapper.selectAllCategories2"/>
    </resultMap>

    <!-- 根据父级分类的id查询子级分类  -->
    <select id="selectAllCategories2" resultMap="categoryVOMap2">
        select category_id,
               category_name,
               category_level,
               parent_id,
               category_icon,
               category_slogan,
               category_pic,
               category_bg_color
        from category
        where parent_id = #{parentId}
    </select>



    <resultMap id="categoryVOMap3" type="com.qfedu.fmmall.entity.CategoryVO">
        <id column="category_id" jdbcType="INTEGER" property="categoryId"/>
        <result column="category_name" jdbcType="VARCHAR" property="categoryName"/>
        <result column="category_level" jdbcType="INTEGER" property="categoryLevel"/>
        <result column="parent_id" jdbcType="INTEGER" property="parentId"/>
        <result column="category_icon" jdbcType="VARCHAR" property="categoryIcon"/>
        <result column="category_slogan" jdbcType="VARCHAR" property="categorySlogan"/>
        <result column="category_pic" jdbcType="VARCHAR" property="categoryPic"/>
        <result column="category_bg_color" jdbcType="VARCHAR" property="categoryBgColor"/>
        <collection property="products" select="com.qfedu.fmmall.dao.ProductMapper.selectTop6ByCategory" column="category_id"/>
    </resultMap>

    <select id="selectFirstLevelCategories" resultMap="categoryVOMap3">
        select category_id,
               category_name,
               category_level,
               parent_id,
               category_icon,
               category_slogan,
               category_pic,
               category_bg_color
        from category where category_level=1
    </select>

</mapper>

使用tkmapper需要的generatorConfig.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!-- 引入数据库连接配置 -->
    <!--    <properties resource="jdbc.properties"/>-->

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 配置 GeneralDAO -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.qfedu.fmmall.general.GeneralDAO"/>
        </plugin>

        <!-- 配置数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/fmmall"
                        userId="root" password="root">
        </jdbcConnection>

        <!-- 配置实体类存放路径 -->
        <javaModelGenerator targetPackage="com.qfedu.fmmall.entity" targetProject="src/main/java"/>

        <!-- 配置 XML 存放路径 -->
        <sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mappers"/>

        <!-- 配置 DAO 存放路径 -->
        <javaClientGenerator targetPackage="com.qfedu.fmmall.dao" targetProject="src/main/java" type="XMLMAPPER"/>

        <!-- 配置需要指定生成的数据库和表,% 代表所有表 -->
        <table tableName="%">
            <!-- mysql 配置 -->
            <!--            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>-->
        </table>
        <!--        <table tableName="tb_roles">-->
        <!--            &lt;!&ndash; mysql 配置 &ndash;&gt;-->
        <!--            <generatedKey column="roleid" sqlStatement="Mysql" identity="true"/>-->
        <!--        </table>-->
        <!--        <table tableName="tb_permissions">-->
        <!--            &lt;!&ndash; mysql 配置 &ndash;&gt;-->
        <!--            <generatedKey column="perid" sqlStatement="Mysql" identity="true"/>-->
        <!--        </table>-->
    </context>
</generatorConfiguration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值