mybatis生成器插件


生成器
会自动帮我们生成 XxxMapper.java 接口文件
                 XxxMapper.xml  接口映射文件

1、建立maven 项目

2、打开项目的pom.xml 文件
	<?xml version="1.0" encoding="UTF-8"?>
	<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	    <modelVersion>4.0.0</modelVersion>

	    <groupId>com.fz</groupId>
	    <artifactId>my10</artifactId>
	    <packaging>jar</packaging>
	    <version>1.0</version>
	    <dependencies>
		<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.12</version>
		    <scope>test</scope>
		</dependency>
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis</artifactId>
		    <version>3.4.6</version>
		</dependency>
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>8.0.11</version>
		</dependency>
		<dependency>
		    <groupId>org.projectlombok</groupId>
		    <artifactId>lombok</artifactId>
		    <version>1.16.20</version>
		</dependency>
		<!-- logback -->
		<dependency>
		    <groupId>ch.qos.logback</groupId>
		    <artifactId>logback-core</artifactId>
		    <version>1.2.3</version>
		</dependency>
		<dependency>
		    <groupId>ch.qos.logback</groupId>
		    <artifactId>logback-classic</artifactId>
		    <version>1.2.3</version>
		</dependency>
		<dependency>
		    <groupId>org.slf4j</groupId>
		    <artifactId>slf4j-api</artifactId>
		    <version>1.7.25</version>
		</dependency>
	    </dependencies>

	    <build>
		<finalName>${project.artifactId}</finalName>
		<testSourceDirectory>src/test/java</testSourceDirectory>
		<sourceDirectory>src/main/java</sourceDirectory>
		<!-- 处理无法加载资源配置文件 -->
		<resources>
		    <resource>
			<directory>src/main/java</directory>
			<includes>
			    <include>**/*.xml</include>
			</includes>
		    </resource>
		    <resource>
			<directory>src/main/resources</directory>
			<includes>
			    <include>**/*.xml</include>
			    <include>**/*.properties</include>
			</includes>
		    </resource>
		</resources>
		<plugins>
			
	        <!-- 生成器插件 -->
		    <plugin>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-maven-plugin</artifactId>
			<version>1.3.5</version>
			<configuration>
			    <!-- 如果此行不配置,则生成器自动找 src/main/resources/generatorConfig.xml -->
			    <configurationFile>src/main/resources/gc.xml</configurationFile>
			    <verbose>true</verbose>
			    <overwrite>true</overwrite>
			</configuration>

			<!-- 如果下边依赖数据库驱动不配置,则在src/main/resources/generatorConfig.xml 中配置如下 -->
			<dependencies>
			    <dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>8.0.11</version>
			    </dependency>
			</dependencies>
		    </plugin>
		</plugins>
	    </build>
	</project>

3、在src/main/resouces/建立
	db.properties
		jdbc.driver=com.mysql.jdbc.Driver
		jdbc.url=jdbc:mysql://localhost:3306/db?useSSL=true&useUnicode=true&characterEncoding=UTF8
		jdbc.username=root
		jdbc.password=root


	mybatis-config.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"/>
		    <!-- 全局配置参数 -->
		    <settings>
			<!-- 延迟加载总开关 -->
			<setting name="lazyLoadingEnabled" value="true"/>
			<!-- 设置按需加载 -->
			<setting name="aggressiveLazyLoading" value="false"/>

			<setting name="logPrefix" value="dao."/>
		    </settings>
		    <typeAliases>
			<package name="com.fz.entity"/>
		    </typeAliases>
		    <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>
			<package name="com.fz.mapper"/>
		    </mappers>
		</configuration>

gc.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>
    <properties resource="db.properties"/>
    <classPathEntry location="d:/mysql-connector-java-8.0.11.jar"/>
    <context id="mysql" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}"/>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.fz.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

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

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.fz.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="student" domainObjectName="Student"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>

        <table tableName="book" domainObjectName="Book"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true"/>
    </context>
</generatorConfiguration>

	logback.xml 日志
		<?xml version="1.0"?>
		<configuration>
		    <!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
		    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
			<encoder>
			    <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
			</encoder>
		    </appender>
		    <!-- 日志级别 -->
		    <root>
			<level value="error" />
			<appender-ref ref="console" />
		    </root>
		    <logger name="dao" level="DEBUG"/>
		</configuration>

4、打开idea  中的maven面板  Maven Project 
	找到执行生成




解决<?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">  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值