安装
-
1、手动下载MyBatisGenerator插件
提取码:u2gs -
2、在Myclispse安装目录MyEclipse 2017 CIdropins下创建myBatisgenerator文件夹
-
3、将插件压缩包中的两个文件夹移动到mybatisgenerator文件夹中
-
4、启动MyElicpse,选择File-New-Other,出现下图说明插件安装成功
配置
-
1、创建Java Project工程MyBatisGeneratorDemo,导入mybatis和mysql依赖
-
2、在src目录下新建配置文件 generatorMapper.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>
<!--MySQL连接驱动 -->
<classPathEntry location="C:\Users\TR\Desktop\jar\mysql-connector-java-8.0.15.jar" />
<!--数据库链接URL,用户名、密码 -->
<context id="MyBatisGeneratorDemo" targetRuntime="Mybatis3">
<commentGenerator>
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf-8" userId="root" password="123456">
</jdbcConnection>
<!--是否启用java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 实体配置 -->
<javaModelGenerator targetPackage="am.entity"
targetProject="MyBatisGeneratorDemo/src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射xml文件 -->
<sqlMapGenerator targetPackage="am.mapper"
targetProject="MyBatisGeneratorDemo/src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- dao配置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="am.dao" targetProject="MyBatisGeneratorDemo/src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 表的配置 -->
<table tableName="tab_user" domainObjectName="User" enableCountByExample="true"
enableUpdateByExample="true" enableDeleteByExample="true"
enableSelectByExample="true" selectByExampleQueryId="true">
</table>
<table tableName="tab_product" domainObjectName="Product" enableCountByExample="true"
enableUpdateByExample="true" enableDeleteByExample="true"
enableSelectByExample="true" selectByExampleQueryId="true">
</table>
</context>
</generatorConfiguration>
-
需要修改的地方
- 将MySQL连接驱动的location修改为本地mysql的jar包位置
- 将context的id修改为项目工程的名称
- 在数据库连接中设置mysql的驱动类、url、用户名和密码
- 在实体配置、映射xml配置和Dao接口配置中,在targetPackage中分别设置存放实体类、映射文件、dao接口的包名,并将targetProject改为项目名称/src
- 在表配置中,输入使用的表名和实体名
-
完成后,右键运行
-
控制台显示如下,说明成功。可能遇到的问题
-
此时,在工程目录下自动生成三个包和相应的代码
-
在src目录下分别新建mybatis配置文件与数据库配置文件
- db.properties,数据库配置文件
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT jdbc.username=root jdbc.password=123456
- mybatis-config.xml,myBatis配置文件,在mappers中配置映射xml文件位置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="am/mapper/UserMapper.xml"/> <mapper resource="am/mapper/ProductMapper.xml"/> </mappers> </configuration>
使用
- 创建测试类TestProductMapper.java
package am.dao;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import am.entity.Product;
import am.entity.ProductExample;
import am.entity.ProductExample.Criteria;
public class TestProductMapper {
private SqlSession sqlSession;
private ProductMapper productMapper;
@Before
public void Init() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
productMapper = sqlSession.getMapper(ProductMapper.class);
}
@After
public void close() {
if (sqlSession != null)
sqlSession.close();
}
/*
* 查找所有的产品
*/
@Test
public void testFindAllProducts() {
System.out.println("查找所有的产品");
List<Product> list = productMapper.selectByExample(null);
for (Product product : list)
System.out.println(product.toString());
}
/*
* 查找所有名称包含可乐 价格为3的产品
*/
@Test
public void testFindProductByParam() {
System.out.println("查找所有名称包含可乐 价格为3的产品");
ProductExample productExample = new ProductExample();
productExample.createCriteria().andPnameLike("%可乐%").andPriceEqualTo(new BigDecimal(3.0));
List<Product> list = productMapper.selectByExample(productExample);
for (Product product : list)
System.out.println(product.toString());
}
}
- 运行测试