实体类中常用注解:
@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">-->
<!-- <!– mysql 配置 –>-->
<!-- <generatedKey column="roleid" sqlStatement="Mysql" identity="true"/>-->
<!-- </table>-->
<!-- <table tableName="tb_permissions">-->
<!-- <!– mysql 配置 –>-->
<!-- <generatedKey column="perid" sqlStatement="Mysql" identity="true"/>-->
<!-- </table>-->
</context>
</generatorConfiguration>