mybatis generator使用

#2020.02.22
#今天踩的坑。
先付一个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\8.0.19\mysql-connector-java-8.0.19.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3" defaultModelType="flat">
	<!-- 指定自己的注释器 -->
		<commentGenerator type="com.capp.demo.utils.MySQLCommentGenerator">
		</commentGenerator>
		<!--  <commentGenerator>
			<property name="addRemarkComments" value="true"/>
		</commentGenerator> -->
		<!-- 数据库连接串 -->
		<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
			connectionURL="jdbc:mysql://127.0.0.1:3306/wujin?serverTimezone=Hongkong&amp;useUnicode=true&amp;useSSL=true&amp;characterEncoding=utf8"
			userId="root" password="654321">
			<!-- 可以显示表的注释 -->
			<property name="useInformationSchema" value="true" />
		</jdbcConnection>

		<javaTypeResolver>
			 <!--是否使用bigDecimal,默认falsefalse,把JDBC DECIMALNUMERIC 类型解析为 Integer
                true,把JDBC DECIMALNUMERIC 类型解析为java.math.BigDecimal-->
            <property name="forceBigDecimals" value="false"/>
            <!--默认false
                false,将所有 JDBC 的时间类型解析为 java.util.Date
                true,将 JDBC 的时间类型按如下规则解析
                    DATE	                -> java.time.LocalDate
                    TIME	                -> java.time.LocalTime
                    TIMESTAMP               -> java.time.LocalDateTime
                    TIME_WITH_TIMEZONE  	-> java.time.OffsetTime
                    TIMESTAMP_WITH_TIMEZONE	-> java.time.OffsetDateTime
                -->
            <!--<property name="useJSR310Types" value="false"/>-->
		</javaTypeResolver>

		<!-- java bean的放置位置 -->
		<javaModelGenerator targetPackage="com.capp.demo.po" targetProject="src/main/java">
			<!-- 是否创建完之后末尾跟着schema名称。-->
			<property name="enableSubPackages" value="true" />
			<!-- getter方法是否要再加个trim() -->
			<property name="trimStrings" value="false" />
		</javaModelGenerator>
		<!-- mapper xml文件的位置 -->
		<sqlMapGenerator targetPackage="com.capp.demo.mapper" targetProject="src/main/java">
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		
		<!-- mapper的java文件位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.capp.demo.mapper" targetProject="src/main/java">
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>

		<!-- schema为数据库名,oracle需要配置,mysql不需要配置。
		tableName为对应的数据库表名
             domainObjectName 是要生成的实体类名(可以不指定,默认按帕斯卡命名法将表名转换成类名)
             enableXXXByExample 默认为 true, 为 true 会生成一个对应Example帮助类,帮助你进行条件查询,不想要可以设为false -->
		<table schema="wujin" tableName="product" domainObjectName="Product"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="true"
			selectByExampleQueryId="false">
		</table>
		<table schema="wujin" tableName="sub_trade" domainObjectName="SubTrade"
			enableCountByExample="true" enableUpdateByExample="true"
			enableDeleteByExample="false" enableSelectByExample="true"
			selectByExampleQueryId="false">
		</table>
	</context>
</generatorConfiguration>   

这里:
1、mybatis generator使用的xml配置文件中,xmlmapper位置不要随意更改,最好和mapper接口文件放在一起,不然业务层调用接口方法会报错,从数据库中找不到数据。

2、吹个example的彩虹屁,单表操作真的很方便。
当除了用主键查询时,指定其他查询条件时候,可以创建对应表的example类。
比如:

SubTradeExample example = new SubTradeExample();
/// 创建好这个类之后,生成条件,再进行相应的比较。
example.createCriteria().andTidEqualTo(tid);

//并且这个Criteria(条件)可以串联。但是只能用AND!!
Criteria.andTidEqualTo(tid).andStsEqualTo(1);

可以直接OR().

(因为mysql等数据库性能等原因,尽量避免使用,因为or可能无法使用索引导致全库扫描)

example.or().andCntEqualTo(1);

或者先and再or.

example.createCriteria().andTidEqualTo(tid);
example.or(example.createCriteria().andTidEqualTo(tid));

3、自己指定comment如何生成。还能自己指定属性啥的。

<commentGenerator type="com.capp.demo.utils.MySQLCommentGenerator"></commentGenerator>
public class MySQLCommentGenerator extends EmptyCommentGenerator {
    private Properties properties;
    public MySQLCommentGenerator() {
        properties = new Properties();
    }
    @Override
    public void addConfigurationProperties(Properties properties) {
        // 获取自定义的 properties
        this.properties.putAll(properties);
    }
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String author = properties.getProperty("author","wujin");
		String dateFormat = properties.getProperty("dateFormat", "yyyy.MM.dd");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        // 获取表注释
        String remarks = introspectedTable.getRemarks();

        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * " + remarks);
        topLevelClass.addJavaDocLine(" *");
        topLevelClass.addJavaDocLine(" * @author " + author);
        topLevelClass.addJavaDocLine(" * @date " + formatter.format(LocalDateTime.now()));
        topLevelClass.addJavaDocLine(" */");
    }
    .....
}

里面可以设置的内容很多,比如getter/setter方法、javaFile,field等都可以添加注释,大家发挥想象写就行了,这就是写个备忘。
其中:EmptyCommentGenerator 是对CommentGenerator接口的空实现,就不贴代码了。

4、git其中rebase和merge的区别。

仅个人理解:

  1. rebase:将不同的两分支串行在一起。自己的项目的话,最好就是这个。自己开一个分支,和master合并也最好用这个。
  2. merge:将不同的分支并行糅杂在一起(需要你手工比对代码)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis Generator 是一个代码生成器,它可以根据数据库表结构自动生成 MyBatis 的 Mapper 文件、Java 实体类和 XML 映射文件等代码。使用 MyBatis Generator 可以大大减少开发人员的工作量,提高开发效率。 下面是使用 MyBatis Generator 的步骤: 1. 配置 generatorConfig.xml 文件,该文件用于配置生成代码的详细信息,包括数据库连接信息、生成的文件类型、生成的表等。 2. 运行 MyBatis Generator,生成代码。 3. 在项目中引入生成的代码,例如 Mapper 接口、实体类等。 以下是一个简单的 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="mysql-connector-java-5.1.39-bin.jar" /> <context id="MysqlTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password=""> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.mybatis.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.mybatis.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="user" domainObjectName="User" > </table> </context> </generatorConfiguration> ``` 这个示例配置文件会生成一个 UserMapper 接口、一个 User 实体类和一个 user.xml 文件。其中,User 实体类的包路径为 com.mybatis.example.model,UserMapper 接口和 user.xml 文件的包路径为 com.mybatis.example.mapper。 在运行 MyBatis Generator 之后,可以在项目中引入生成的代码,例如: ``` @Autowired private UserMapper userMapper; ``` 以上代码会注入一个 UserMapper 对象,可以通过该对象来操作数据库表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值