超级简单!!!-Mybatis在IDEA中使用MyBatis Generator****生成pojo,mapper(Mac和Windows都适用)
1.首先在pom文件添加plugin
内容如下: <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
2.在工程的resource中添加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> <!--数据库驱动jar包的真实路径 --> <classPathEntry location="/Users/cuiyunpeng/Downloads/mysql-connector-java-5.6-bin.jar"/> <context id="context" targetRuntime="MyBatis3Simple"> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection userId="root" password="famei" driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/db_test_mybatis"/> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="true"/> </javaTypeResolver> <!--指定包名生成实体类 以及生成的地址 (可以自定义地址,如果路径不存在会自动创建) --> <!--targetProject中.是指当前工程下 --> <javaModelGenerator targetPackage="com.tst.bill.check.domain" targetProject="./src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <javaClientGenerator targetPackage="com.tst.bill.check.dao" type="XMLMAPPER" targetProject="./src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 指定数据库表 tableName:数据库中的表明 domainObjectName:你要生成的pojo的名字--> <table schema="db_bill_check" tableName="t_mall_pay_record_2018_07" domainObjectName="MallPayRecord" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> </context> </generatorConfiguration>
3.找到IDEA右边的MavenProjects中使用Maven命令
或者直接使用maven命令也行(适用于Eclipse)具体方法和下面差不多,所产生和效果上下都一样
(1)点击IDEA右上方的运行命令
(2)在Command line中输入
mybatis-generator:generate
(3)运行即可
4.运行结果如下
注意!!!
(1)因为dao和mapper不在同一文件夹下所以要手添加@Param注释
如果没有@Param注释的提示那么是缺少mybatis的jar包需要再maven文件中引入
如图:
(2)因为 insert和updateByPrimaryKey中传入的是类,所以要在mapper.xml文件中修改
例如:(record是上文注释@Param中的"record")