mybatis根据数据库生成mapper

命令行:mvn mybatis-generator:generate


<?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="/Users/develop/mavenjar-landz/com/oracle/ojdbc6/11.2.0.3/ojdbc6-11.2.0.3.jar" />
	<!-- <classPathEntry location="/Users/Downloads/sqljdbc4-4.0.jar"/> -->
	<!-- <classPathEntry location="/Users/develop/mavenjars/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" /> -->
	
	<context id="DB2Tables" targetRuntime="MyBatis3">
	
		
		<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
          <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
        </plugin>
        
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		
		
		
		<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
			connectionURL="jdbc:oracle:thin:@10.10.124.17:1521/dg1"
			userId="lzhdic" password="test123">
		</jdbcConnection> 
		
		<!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
			connectionURL="jdbc:oracle:thin:@ip:1521/lzhse"
			userId="" password="">
		</jdbcConnection>  -->
		<!-- <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
			connectionURL="jdbc:sqlserver://ip:1433;databaseName=landzshr"
			userId="" password="">
		</jdbcConnection> -->
		
		<!--  <jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://127.0.0.1:3306/landzmydb?zeroDateTimeBehavior=convertToNull&characterEncoding=utf-8"
			userId="root" password="">
		</jdbcConnection> -->
		
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

        <!-- targetPackage="com.mybatis.model" model包名 -->
		<javaModelGenerator targetPackage="com.landz.owner.model.entity" 
			targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
			<!-- <property name="trimStrings" value="true" /> --><!-- 去除空格 -->
		</javaModelGenerator>

		<sqlMapGenerator targetPackage="com.landz.owner.mapper.mapping" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- targetPackage="com.mybatis.mapper" mapper包名 -->
		<javaClientGenerator type="XMLMAPPER" 
		      targetPackage="com.landz.owner.mapper" 
		      targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		
		
		<!-- 要生成得表  注意 会覆盖前面生成的 文件 -->
		<table tableName="%" modelType="flat" schema="OWNER" 
		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
		    <!-- <generatedKey column="pkid" sqlStatement="SELECT LAST_INSERT_ID()" type="post" identity="true"/> -->
		    <!-- <ignoreColumn column="creattime"/> -->
		</table>
		
		
		<!-- <generatedKey column="pkid" sqlStatement="SELECT LAST_INSERT_ID()" type="post" identity="true"/>
		    <ignoreColumn column="creattime"/>
		</table> -->
		
		<!--enableDeleteByPrimaryKey="false"  enableUpdateByPrimaryKey="false" enableInsert="false" delimitAllColumns="false" 
		delimitIdentifiers="false"  --> 
		
	</context>

</generatorConfiguration>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis可以通过MyBatis Generator(MBG)工具自动生成Mapper接口。 MBG是一个用于生成Java代码的插件,它可以根据数据库中的表结构自动生成Java Bean和Mapper接口。 为了使用MBG,你需要在MyBatis的配置文件中添加以下插件配置: ``` <plugins> <plugin type="org.mybatis.generator.plugins.MapperPlugin"> <property name="mappers" value="org.apache.ibatis.annotations.Mapper"/> </plugin> </plugins> ``` 然后,在MBG配置文件中,你需要指定数据库连接信息、生成的Java Bean和Mapper接口的包名、表名等信息。例如: ``` <?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.34-bin.jar"/> <context id="MyBatis3"> <!-- 生成Java Bean的包名 --> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成Mapper接口的包名 --> <javaClientGenerator targetPackage="com.example.mapper" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123456"> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!-- 表名 --> <table tableName="user"></table> </context> </generatorConfiguration> ``` 最后,你可以通过命令行或者IDEA插件来运行MBG,生成Java Bean和Mapper接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值