eclipse中mybatis generator插件的安装及使用(已过时)

(1)eclipse中mybatis generator插件的安装及使用:

https://blog.csdn.net/jay_1989/article/details/51983322

(2)mybatis-generator扩展教程系列-自定义sql xml文件:

https://blog.csdn.net/shadowsick/article/details/53664829

(3)mybatis-generator扩展教程系列 -- mapper xml文件增加自定义sql:

https://blog.csdn.net/shadowsick/article/details/53734608

 

/wushopAdm02/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 >
  <classPathEntry location="D:\eclipse_workspace\wushopAdm02\WebContent\WEB-INF\lib\mysql-connector-java-5.1.26-bin.jar"/>
  
  <context id="context1" targetRuntime="MyBatis3">
    <!--注释生成 -->
    <commentGenerator>
		   <!-- 是否取消注释 -->
		   <property name="suppressAllComments" value="true" />
		   <!-- 是否生成注释代时间戳 -->
		   <property name="suppressDate" value="true" />
	</commentGenerator>
	
	<!-- jdbc 连接信息 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" userId="root" password="123456" />
    
    <!-- 指定【java数据模型】生成在哪个项目的哪个包-->
    <javaModelGenerator targetPackage="com.wu.entity" targetProject="wushopAdm02/src">
            <!-- 去除字段前后空格 -->
			<property name="trimStrings" value="true" />
    </javaModelGenerator>
    
    <!-- 指定【mapper接口类】生成在哪个项目的哪个包 -->
    <sqlMapGenerator targetPackage="com.wu.mapper.sysUserMapper" targetProject="wushopAdm02/src" />
    
    <!-- 指定【mapper映射文件】生成在哪个项目的哪个包 -->
    <javaClientGenerator targetPackage="com.wu.mapper.sysUserMapper" targetProject="wushopAdm02/src" type="XMLMAPPER" />
    
    <!-- 指定数据库的【表名】和将要生成的【实体类名】 --><!-- 设置不生成Example类 -->
    <table schema="test" tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    
    
    
  </context>
</generatorConfiguration>

 

 

jar包/class文件如何快速反编译成java文件

(1)反编译工具:jd-gui

(2)用法:

1.点击【file】选择 jar包或者class文件(对压缩文件无效);

2.点击【file】选择 save all sources】,将其保存到相应的目录,保存的文件为一个压缩文件,将其解压即可。

mybatis-generator -- 给mapper xml文件增加自定义sql

1.打开IntrospectedTable.java找到enum InternalAttribute这个枚举定义增加一行我们的sql id

package org.mybatis.generator.api; 
public abstract class IntrospectedTable {                              
         protected enum InternalAttribute {                                             
                   ATTR_SELECT_BY_CONDITION_STATEMENT_ID, //新增一条【sql  id】的枚举:通过条件查询                         
         }  
}

2.IntrospectedTable.java增加【sql id】的【set,get方法】用于之后的读取操作

public void setSelectByConditionStatementId(String s) {  //set方法
        internalAttributes.put(InternalAttribute.ATTR_SELECT_BY_CONDITION_STATEMENT_ID, s);  
    }  
      
    public String getSelectByConditionStatementId() {  //get方法
        return internalAttributes  
                .get(InternalAttribute.ATTR_SELECT_BY_CONDITION_STATEMENT_ID);  
    }  

3.在IntrospectedTable.java的已有方法calculateXmlAttributes()中设置【sql id】的值

protected void calculateXmlAttributes() {
        setSelectByConditionStatementId("select");//调用set方法,设置【sql id】的值
}

 

4.新建一个xml sql生成的实现类SelectByConditionElementGenerator.java

 

package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;  
  
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;  
import org.mybatis.generator.api.IntrospectedColumn;  
import org.mybatis.generator.api.dom.xml.Attribute;  
import org.mybatis.generator.api.dom.xml.TextElement;  
import org.mybatis.generator.api.dom.xml.XmlElement;  
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;  
  

public class SelectByConditionElementGenerator extends  
        AbstractXmlElementGenerator {  
  
    public SelectByConditionElementGenerator() {  
        super();  
    }  
    
    @Override  
    public void addElements(XmlElement parentElement) {  
    	System.out.println("————————————————————————创建Xml元素对象<select></select>————————————————————————");
        XmlElement answer = new XmlElement("select"); 
        
        System.out.println("————————————————————————注入sql id——————————————————————");
        answer.addAttribute(new Attribute("id", introspectedTable.getSelectByConditionStatementId()));  
        
        System.out.println("————————————————————————注入sql resultMap——————————————————————");
        if (introspectedTable.getRules().generateResultMapWithBLOBs()) {  
            answer.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));  
        } else {  
            answer.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));  
        }  
        
        System.out.println("————————————————————————注入sql parameterType——————————————————————");
        String parameterType;  
        if (introspectedTable.getRules().generatePrimaryKeyClass()) {  
            parameterType = introspectedTable.getPrimaryKeyType();  
        } else {   
            if (introspectedTable.getPrimaryKeyColumns().size() > 1) {  
                parameterType = "map"; 
            } else {  
                parameterType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();  
            }  
        }  
  
        answer.addAttribute(new Attribute("parameterType",parameterType));  
        
        System.out.println("————————————————————————往CommentGenerator生成器中添加Xml元素对象<select></select>————————————————————————");
        context.getCommentGenerator().addComment(answer);  
  
        StringBuilder sb = new StringBuilder();  
        sb.append("select ");  
  
        if (stringHasValue(introspectedTable  
                .getSelectByPrimaryKeyQueryId())) {  
            sb.append('\'');  
            sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());  
            sb.append("' as QUERYID,");
        }  
        
        System.out.println("————————————————————————往<select></select>内填充文本元素'select'————————————————————————");
        answer.addElement(new TextElement(sb.toString())); 
        
        System.out.println("————————————————————————往<select></select>内填充BaseColumnListElement————————————————————————");
        answer.addElement(getBaseColumnListElement());  
        
        System.out.println("————————————————————————如果数据表中存在BLOBColumns,则往<select></select>内填充文本元素','————————————————————————");
        if (introspectedTable.hasBLOBColumns()) {  
            answer.addElement(new TextElement(","));
            answer.addElement(getBlobColumnListElement());  
        }  
        
        System.out.println("————————————————————————往<select></select>内填充文本元素'from'————————————————————————");
        sb.setLength(0);  
        sb.append("from "); 
        sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());  
        answer.addElement(new TextElement(sb.toString()));  
        
        
        System.out.println("————————————————————————往<select></select>内填充<where></where>对象————————————————————————");
        XmlElement whereOne = new XmlElement("select")
        answer.addElement(whereOne);
        for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {  
        	System.out.println("————————————————————————往<where></where>内填充<if></if>对象————————————————————————");
        	XmlElement ifXML = new XmlElement("if")
        	whereOne.addElement(ifXML);
        	System.out.println("————————————————————————往<if></if>内填充sql的查询条件————————————————————————");
        	sb.setLength(0);  
            sb.append("and "); 
            
            sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));  
            sb.append(" = ");  
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));  
            whereOne.addElement(new TextElement(sb.toString()));  
        }  
        System.out.println("————————————————————————往parentElement中添加Xml元素对象<select></select>————————————————————————");
        parentElement.addElement(answer);  
    }  
}  

5.在XMLMapperGenerator.java中编写一个方法,该方法调用了【我们上面编写的用于sql语句生成的实现类SelectByConditionElementGenerator.java】

protected void addSelectByConditionElement(XmlElement parentElement) {  
        if (introspectedTable.getRules().generateSelectByPrimaryKey()) {  
            AbstractXmlElementGenerator elementGenerator = new SelectByConditionElementGenerator(); // 创建实现类对象
            initializeAndExecuteGenerator(elementGenerator, parentElement);  //注册实现类对象
        }  
    }  

5.在XMLMapperGenerator.java的已有方法getSqlMapElement()中调用一个方法,该方法就是【我们上面编写的方法addSelectByConditionElement(XmlElement parentElement)】

protected XmlElement getSqlMapElement() {
      addSelectByConditionElement(answer);//注册实现类对象
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值