集成与使用

1. mybatis集成

1.1 新建SpringBoot项目

1.2 在pom文件中添加依赖

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>2.1.0</version>
</dependency>

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>

1.3 在 application.yml配置datasource

spring:
  datasource:
    username: root
    password: zsz123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://120.79.200.111:3306/mybatisdemo?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

2.mybatis-generator 集成

2.1 pom文件下添加插件

<build>
   <plugins>
      <plugin>
         <groupId>org.mybatis.generator</groupId>
         <artifactId>mybatis-generator-maven-plugin</artifactId>
         <version>1.3.5</version>
         <configuration>
            <configurationFile>src/main/resources/config/mybatis-generator.xml</configurationFile>
            <overwrite>true</overwrite>
            <verbose>true</verbose>
         </configuration>
      </plugin>

   </plugins>
</build>

2.2 添加配置文件

在resource的config目录下添加 mybatis-generator.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>
    <!--数据库驱动-->
    <!--注意要保证运行MyBatis Generator的时候能够找到mysql的connector-->
    <!-- location   The full path name of a JAR/ZIP file to add to the classpath, or a directory to add to the classpath.-->
    <classPathEntry location="/Users/zhangshengzhong/Project/jar/mysql-connector-java-5.1.7-bin.jar"/>
    <!-- A unique identifier for this context. This value will be used in some error messages. -->
    <context id="mysql2java" targetRuntime="MyBatis3">
        <!-- http://www.mybatis.org/generator/configreference/commentGenerator.html -->
        <!-- 阻止生成自动注释 -->
        <commentGenerator>
            <property name="javaFileEncoding" value="UTF-8"/>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://120.79.200.111:3306/mybatisdemo" userId="root" password="zsz123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成Model类存放位置 -->
        <javaModelGenerator targetPackage="com.zszdevelop.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件存放位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="me.zszdevelop.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--配置MBG对哪个数据表进行生成工作, 对于MYSQL数据库来说, 因为没有SCHEMA这个概念, 必须使用catalog来指定到底用哪个数据库, 否则如果多个数据库有同名表, MYBATIS会自己选一个-->
        <table tableName="sys_user" domainObjectName="SysUser"></table>

    </context>
</generatorConfiguration>

2.3 生成mapping 文件

在idea 的右边mavenprojects 中的plugs中找到mybatis-generator

点击mybatis-generator就生成了

results matching ""

    No results matching ""