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
结束!
- 浏览: 487899 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (241)
- Jscript笔记 (23)
- sql (8)
- jQuery (23)
- css (2)
- jQuery.form (2)
- struts2 (3)
- 数据库字符集 (3)
- Java (15)
- Oracle (2)
- DB2 (4)
- ResourceBundle (1)
- Exception (7)
- Windows (2)
- 数据库 (18)
- JSP (2)
- jstree (3)
- datatable (9)
- bootstrap-switch (1)
- select2 (3)
- Java.tree (1)
- JSF2 (1)
- Bootstrap (1)
- Eclipse (13)
- SVN (3)
- Eclipse插件 (5)
- myeclipse (1)
- ajax (1)
- JBOSS (2)
- data storage (1)
- Apache tiles (1)
- springMVC (15)
- mybatis (6)
- AngularJS (13)
- checkbox (1)
- J2EE (1)
- SecureCRT (7)
- tomcat (4)
- Maven (3)
- log4j (3)
- Xshell (1)
- 工具应用 (5)
- spring (12)
- Apache (5)
- books (1)
- fastjson (1)
- Standard (1)
- Mysql (1)
- MAC (1)
- Charles (1)
- LINUX (1)
- 日常开发ISSUE (0)
最新评论
-
yuchangcheng:
直接把代码 上传上来吧 这样看的很吃力呀
mybatis generator 的复合查询 -
zzd0058:
我表示报400错误
SpringMVC接收复杂集合参数 -
lihaiming:
影非弦 写道没有这个页面详细的文件么?文档地址:https:/ ...
select2.js使用 -
影非弦:
没有这个页面详细的文件么?
select2.js使用 -
影非弦:
这个select.min.css是select2提供的样式文件 ...
select2.js使用
发表评论
-
MyBatis批量插入数据
2016-04-13 20:48 977在程序中封装了一个List集合对象,然后需要把该集合中的 ... -
iBATIS sql(XML)中的大于、小于、like等符号写法
2015-11-26 20:38 1780其实就是xml的特殊符号,因为它的配置就是xml,所以可以用 ... -
myBatis的SQL注入问题
2015-10-30 19:46 1384sqlMap中尽量不要使用$;$使用的是Statemen ... -
Mybatis中的like查询
2015-10-22 16:07 861今天要做一个模糊查询 用的Mybatis 开始写的是: ... -
mybatis No enum const class org.apache.ibatis.type.JdbcType.Date 坑爹的配置
2015-10-10 13:51 3483在ibatis中不需要关注这些参数 而转到mybatis后 ...
相关推荐
MybatisGenerator本身是没有提供查询指定字段的,例如数据库有5个字段,我想查询其中3个字段的全部记录,这是做不到的。经过研究MybatisGenerator实现原理,在保证原有功能的基础上,实现了生成查询指定字段的相关...
mybatis generator 是根据已创建的数据库数据表生成相映的 entity ,dao ,daoImpl ,sqlmap。 标签:mybatis
mybatis generator eclipse插件的安装
mybatis generator相关资源 包含配置文件,jar包等 补充
eclipse 插件,使用MyBatis Generator 可自动生成数据库对应的bean(有中文注释),mapper.xml和mapper dao 接口文件,可直接使用查询数据库,此插件在eclipse mars.2 版本下亲测可用,
赠送jar包:mybatis-generator-core-1.3.7.jar; 赠送原API文档:mybatis-generator-core-1.3.7-javadoc.jar; 赠送源代码:mybatis-generator-core-1.3.7-sources.jar; 赠送Maven依赖信息文件:mybatis-generator-...
myBatisGenerator = new MyBatisGenerator(config, callback, warnings); } catch (InvalidConfigurationException e) { e.printStackTrace(); } try { myBatisGenerator.generate(null); } catch ...
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,Mybatis-Generator可以用来帮我们自动生成这些文件。
MyBatis Generator自动创建代码文件和jar包,参考http://blog.csdn.net/zhshulin/article/details/23912615
1、这是Eclipse MyBatis generator 1.3.7插件的核心包 2、首先到Eclipse中下载 MyBatis Generator 1.3.7插件,下载完按步骤进行安装 打开Help > Eclipse Marketplace > Search > 输入框输入 MyBatis Generator ...
mybatis-generator版本:mybatis-generator-core-1.3.7.jar 关于逆向工程自动生成dao层代码的功能,网上很多介绍的都是mysql版本的,而且介绍的都是在开发环境idea或eclipse中怎么使用,这里整理的是PostgerSQL版本...
mybatis generator Myeclipse插件 含详细说明和mysql oracle的依赖包
自动生成映射类 Eclipse Mybatis Generator 自动生成映射类 Eclipse Mybatis Generator
MyBatis Generator是一个可以用来生成Mybatis dao,entity,Mapper文件的一个工具,在项目的过程中可以省去很多重复的工作,我们只要在MyBatis Generator的配置文件中配置好要生成的表名与包名,然后运行一条命令就会...
1、下载mybatis生成架包工具MyBatis_Generator_1.3.1.zip,解压架包把features、plugins文件夹下的架包分别拷贝到eclipse安装目录下的features、plugins文件夹。重启eclipse就行。 解压后图片如下: Eclipse路径...
eclipse mybatis generator插件
Mybatis Generator 结合Lombok使用,不生成get,set,生成数据库注释 自定定义 MODEL 类 数据库 使用提前:项目相关的jar 都引用了, 资源最组要是要有 mybatis-generator-core jar包 下载下来, 1 如果不改直接...
mybatis generator生成器
mybatis-generator-1.3.5.zip
使用MyBatis Generator生成DAO