mybatis generator可以自动生成sqlmap文件和查询语句,但是只能完成单表的查询,自己改了下,可以实现复合查询,感觉能比自己写查询语句能方便一些吧。
下面就讲讲怎么改的,和主要区别。
先说下表结构,主表stockclothestbl,副表有很多,那其中一个colortbl表举例。
正常通过generator可以自动生成StockclothestblExample.java,ColortblExample.java和StockclothestblMapper.xml,ColortblMapper.xml,4个文件。StockclothestblExample.java和ColortblExample.java文件用于查询。新建StockclothesExample.java。用于包含2个查询对象。
public class StockclothesExample {
public StockclothestblExample stockclothestblExample;
public ColortblExample colortblExample;
public StockclothestblExample getStockclothestblExample() {
return stockclothestblExample;
}
public void setStockclothestblExample(StockclothestblExample stockclothestblExample) {
this.stockclothestblExample = stockclothestblExample;
}
public ColortblExample getColortblExample() {
return colortblExample;
}
public void setColortblExample(ColortblExample colortblExample) {
this.colortblExample = colortblExample;
}
}
然后开始修改StockclothestblMapper.xml和ColortblMapper.xml文件
StockclothestblMapper.xml 文件里,添加sql,仿照自动生成的单表查询。
<!-- 联合查询 -->
<select id="selectStockclothes" resultMap="selectStockclothesResult" parameterType="cn.com.shopdepot.javaModel.custom.StockclothesExample">
select
<if test="stockclothestblExample.distinct" >
distinct
</if>
<include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.Base_Column_List" />
,
<include refid="cn.com.shopdepot.javaClient.RegisterdepottblMapper.Base_Column_List" />
,
<include refid="cn.com.shopdepot.javaClient.StyletblMapper.Base_Column_List" />
,
<include refid="cn.com.shopdepot.javaClient.KindtblMapper.Base_Column_List" />
,
<include refid="cn.com.shopdepot.javaClient.ClotheskindtblMapper.Base_Column_List" />
,
<include refid="cn.com.shopdepot.javaClient.ColortblMapper.Base_Column_List" />
,
<include refid="cn.com.shopdepot.javaClient.SizetblMapper.Base_Column_List"/>
from stockclothestbl stockclothestbl
left join registerdepottbl registerdepottbl on registerdepottbl.REGISTERDEPOT_SERNO=stockclothestbl.REGISTERDEPOT_SERNO
left join styletbl styletbl on styletbl.STYLE=stockclothestbl.STYLE
left join kindtbl kindtbl on kindtbl.KIND=stockclothestbl.KIND
left join clotheskindtbl clotheskindtbl on clotheskindtbl.CLOTHESKIND_CODE=stockclothestbl.CLOTHESKIND_CODE
left join colortbl colortbl on colortbl.COLOR_SERNO=stockclothestbl.COLOR_SERNO
left join sizetbl sizetbl on sizetbl.SIZE_CODE=stockclothestbl.SIZE_CODE
and styletbl.DEL_FLAG='0'
and kindtbl.DEL_FLAG='0'
and clotheskindtbl.DEL_FLAG='0'
and colortbl.DEL_FLAG='0'
and sizetbl.DEL_FLAG='0'
<if test="_parameter != null" >
<where>
<trim prefix="(" suffix=")" prefixOverrides="and" >
<include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.Example_Where_Clause_Complex" />
<include refid="cn.com.shopdepot.javaClient.ColortblMapper.Example_Where_Clause_Complex"/>
</trim>
</where>
</if>
<if test="stockclothestblExample.orderByClause != null" >
order by ${stockclothestblExample.orderByClause}
</if>
<include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.mysqlDialect_Complex" />
</select>
解释下这个sql
id="selectStockclothes",是以后java端调用的方法名。
resultMap="selectStockclothesResult" 这个是返回结果,引用selectStockclothesResult,下面是selectStockclothesResult
<resultMap type="cn.com.shopdepot.javaModel.custom.Stockclothes" id="selectStockclothesResult">
<association property="stockclothestbl" resultMap="cn.com.shopdepot.javaClient.StockclothestblMapper.BaseResultMap"></association>
<association property="registerdepottbl" resultMap="cn.com.shopdepot.javaClient.RegisterdepottblMapper.BaseResultMap"></association>
<association property="styletbl" resultMap="cn.com.shopdepot.javaClient.StyletblMapper.BaseResultMap"></association>
<association property="kindtbl" resultMap="cn.com.shopdepot.javaClient.KindtblMapper.BaseResultMap"></association>
<association property="clotheskindtbl" resultMap="cn.com.shopdepot.javaClient.ClotheskindtblMapper.BaseResultMap"></association>
<association property="colortbl" resultMap="cn.com.shopdepot.javaClient.ColortblMapper.BaseResultMap"></association>
<association property="sizetbl" resultMap="cn.com.shopdepot.javaClient.SizetblMapper.BaseResultMap"></association>
</resultMap>
其中 <association property="colortbl" resultMap="cn.com.shopdepot.javaClient.ColortblMapper.BaseResultMap"></association>
就是ColortblMapper.xml文件中最上边的命名空间+ColortblMapper.xml文件中BaseResultMap的id。
继续上边,parameterType="cn.com.shopdepot.javaModel.custom.StockclothesExample" 参数就是我们自己添加的StockclothesExample.java文件的全路径。
<if test="stockclothestblExample.distinct" >,这个地方要修改,原来自动生成的是<if test="distinct" >修改成
<if test="stockclothestblExample.distinct" >。这样才能反射找到是stockclothestblExample对象里的distinct。
stockclothestblExample---是我们自己添加StockclothesExample类里的成员
<include refid="cn.com.shopdepot.javaClient.ColortblMapper.Base_Column_List" />,这个同理resultMap的,查询sql的字段,colortbl表的字段。
下面修改的相对比较多
<if test="_parameter != null" >
<where>
<trim prefix="(" suffix=")" prefixOverrides="and" >
<include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.Example_Where_Clause_Complex" />
<include refid="cn.com.shopdepot.javaClient.ColortblMapper.Example_Where_Clause_Complex"/>
</trim>
</where>
</if>
自动生成的单表查询的以前的下面
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
添加了colortbl的查询 <include refid="cn.com.shopdepot.javaClient.ColortblMapper.Example_Where_Clause_Complex"/>
以前的refid="Example_Where_Clause"------变成Example_Where_Clause_Complex,Example_Where_Clause是自动生成的,
Example_Where_Clause_Complex是我自己添加的,仿照Example_Where_Clause。下面是2个表的Example_Where_Clause_Complex。
分别写在2个自己的xml中。
StockclothestblMapper.xml添加的Example_Where_Clause_Complex
<sql id="Example_Where_Clause_Complex" >
<foreach collection="stockclothestblExample.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</if>
</foreach>
</sql>
StockclothestblMapper.xml自动生成的Example_Where_Clause
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
比较看下,红色的去修改的,去掉where和trim,因为2个要在最外层加,上边画的蓝色部分。
collection="oredCriteria"变成collection="stockclothestblExample.oredCriteria",因为你的指定到你的类的具体对象。
同上
ColortblMapper.xml 添加的Example_Where_Clause_Complex
<sql id="Example_Where_Clause_Complex" >
<foreach collection="colortblExample.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</if>
</foreach>
</sql>
ColortblMapper.xml自动生成的Example_Where_Clause
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
继续上边的解释
<if test=" stockclothestblExample.orderByClause != null" >
order by ${stockclothestblExample.orderByClause}
</if>
也是指定到具体对象的排序,绿色部分。
sqlmap基本就改完了,剩下没解释的,是分页limit,就不解释了,贴一下代码
<sql id="mysqlDialect_Complex" >
<if test="stockclothestblExample.limitStart > -1">
limit ${stockclothestblExample.limitStart}, ${stockclothestblExample.limitEnd}
</if>
</sql>
以上基本就完成了。
然后在自动生成的StockclothestblMapper.java,添加方法
/**
* sqlmap id=selectStockclothes
*
* @author wang
* @param example
* @return
*/
List<Stockclothes> selectStockclothes(StockclothesExample example);
这样就可以使用了。大概写下
StockclothesExample example = new StockclothesExample();
StockclothestblExample stockclothestblExample = new StockclothestblExample();
StockclothestblExample.Criteria stockclothestbl_criteria = stockclothestblExample.createCriteria();
ColortblExample colortblExample = new ColortblExample();
ColortblExample.Criteria colortbl_criteria = colortblExample.createCriteria();
if (null != selBrand && !"".equals(selBrand)) {
stockclothestbl_criteria.andBrandLike("%" + selBrand + "%");
}
if (null != selColor && !"".equals(selColor)) {
colortbl_criteria.andColorLike("%" + selColor + "%");
}
example.setStockclothestblExample(stockclothestblExample);
example.setColortblExample(colortblExample);
..... 中间省略.......
List <Stockclothes>list=stockclothestblMapper.selectStockclothes(example)
Stockclothes类封装的genertor自动生成的tbl
结束!
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com