如何在eclipse 安装 MyBatis Generator 并使用

本文介绍了如何在Eclipse中安装MyBatis Generator,详细步骤包括下载插件并解压到Eclipse路径,创建Java项目,配置generatorConfig.xml文件,并添加MySQL连接依赖。同时,文章讲解了MyBatis Generator生成的example如何进行简单混合查询,包括支持的查询条件写法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 下载工具

1.先下载 MyBatis Generator 地址 :http://download.youkuaiyun.com/download/sinat_27710009/10115546

2.将下载的插件解压到 eclipse 路径下

3.验证插件是否安装成功


二. 在Eclipse中创建java项目

1. 创建java项目 src目录下,新建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="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>    
	<context id="context1">
		<commentGenerator>    
            <property name="suppressDate" value="true"/>    
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->    
            <property name="suppressAllComments" value="true"/>    
        </commentGenerator>    
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/subscriber"
			userId="root" password="123456" />
		<javaModelGenerator targetPackage="com.sharelin.spring.entity"
			targetProject="springmvc Maven Webapp/src/main/java" />
		<sqlMapGenerator targetPackage="com.sharelin.spring.mapper"
			targetProject="springmvc Maven Webapp/src/main/java" />
		<javaClientGenerator targetPackage="com.sharelin.spring.mapper"
			targetProject="springmvc Maven Webapp/src/main/java" type="XMLMAPPER" />
		<table tableName="sub_customer">
			<!-- <columnOverride column="???" property="???" /> -->
		</table>
	</context>
</generatorConfiguration>
对应 路径根据自己 的实际情况填写即可

2.maven添加 mysql 连接jar包 引用到 上面的 XML路径,也可以其他方式下载了 填写路径

<!-- 导入Mysql数据库链接jar包 -->    
        <dependency>    
            <groupId>mysql</groupId>    
            <artifactId>mysql-connector-java</artifactId>    
            <version>5.1.30</version>    
        </dependency>   
3. 右键generatorConfig.xml文件,点击在菜单中点击Generator MyBatis/iBatis Artifacts(若右键菜单中没有此菜单,说明第一步骤中插件没有安装成功),自动生成4个文件。


三.MyBatis Generator 生成的example 如何使用 and or 简单混合查询

写法1:

查询条件1:a=? and (b=? or c=?) 不支持

查询条件2:(a=? And b=?) or (a=? And c=?) 支持

DemoExample example=new DemoExample();  

DemoExample.Criteria criteria1=example.createCriteria();  
criteria1.andAEqualTo(?).andBEqualTo(?);  
          
DemoExample.Criteria criteria2=example.createCriteria();  
criteria2.andAEqualTo(?).andCEqualTo(?);  

example.or(criteria2);  

SqlSession sqlSession = MyBatisUtil.openSession();
DemoMapper m = sqlSession.getMapper(DemoMapper.class);
m.countByExample(example);  
//生成的sql语句
select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )


写法2:


DemoExample example=new DemoExample();  

example.or().andAEqualTo(?).andBEqualTo(?);
example.or().andAEqualTo(?).andCEqualTo(?); 

SqlSession sqlSession = MyBatisUtil.openSession();
DemoMapper m = sqlSession.getMapper(DemoMapper.class);
m.countByExample(example);  
//生成的sql语句
select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )

查询条件3:(a=? and (b=? or c=?)) 支持

假设两个搜索项,A项搜索,可搜索b,c(bc或关系),B项搜索可搜索a,B项搜索与A项搜索是与关系。

修改DemoExample.java文件,新增方法

public Criteria andOrDemo(String value){
         addCriterion("(b = \""+value+"\" or c = \""+value+"\")");
         return (Criteria) this;
}

DemoExample example=new DemoExample();  
Criteria criteria = example.createCriteria();
criteria.andAEqualTo(?).andOrDemo(?);

SqlSession sqlSession = MyBatisUtil.openSession();
DemoMapper m = sqlSession.getMapper(DemoMapper.class);
m.countByExample(example);  
//生成的sql语句
select count(*) from demo WHERE ( a = ? and ( b = ? or c = ? ))





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值