myelicpes怎么mysql_MyEclipse中MyBatisGenerator反转引擎插件的安装使用

安装

1、手动下载MyBatisGenerator插件

提取码:u2gs

2、在Myclispse安装目录\MyEclipse 2017 CI\dropins下创建myBatisgenerator文件夹

f2d39769cc34f4ebed96a190eb008654.png

3、将插件压缩包中的两个文件夹移动到mybatisgenerator文件夹中

37508f7279b1c640fe64ca583b6faa74.png

4、启动MyElicpse,选择File-New-Other,出现下图说明插件安装成功

df2e25904ee4d832df3e2899dcaea881.png

配置

1、创建Java Project工程MyBatisGeneratorDemo,导入mybatis和mysql依赖

158752f46cc67215a51ba63fddcfb5c1.png

2、在src目录下新建配置文件 generatorMapper.xml

/p>

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

connectionURL="jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf-8" userId="root" password="123456">

targetProject="MyBatisGeneratorDemo/src">

targetProject="MyBatisGeneratorDemo/src">

targetPackage="am.dao" targetProject="MyBatisGeneratorDemo/src">

enableUpdateByExample="true" enableDeleteByExample="true"

enableSelectByExample="true" selectByExampleQueryId="true">

enableUpdateByExample="true" enableDeleteByExample="true"

enableSelectByExample="true" selectByExampleQueryId="true">

需要修改的地方

将MySQL连接驱动的location修改为本地mysql的jar包位置

将context的id修改为项目工程的名称

在数据库连接中设置mysql的驱动类、url、用户名和密码

在实体配置、映射xml配置和Dao接口配置中,在targetPackage中分别设置存放实体类、映射文件、dao接口的包名,并将targetProject改为项目名称/src

在表配置中,输入使用的表名和实体名

完成后,右键运行

aa5cb0f51408f7889799acce81bc4b2f.png

控制台显示如下,说明成功。可能遇到的问题

b607ef5b3bcc6c438f1605d65bebad6d.png

此时,在工程目录下自动生成三个包和相应的代码

db08c4898e9d423c2bcdfbc9bab22675.png

在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文件位置

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

使用

创建测试类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 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 list = productMapper.selectByExample(productExample);

for (Product product : list)

System.out.println(product.toString());

}

}

运行测试

a1321bad47b702b8970a8216fcc99d43.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值