zoukankan      html  css  js  c++  java
  • mybatis generator的使用

      在使用mybatis时我们需要重复的去创建pojo类、mapper文件以及dao类并且需要配置它们之间的依赖关系,比较麻烦且做了大量的重复工作,mybatis官方也发现了这个问题,因此给我们提供了mybatis generator工具来帮我们自动创建pojo类、mapper文件以及dao类并且会帮我们配置好它们的依赖关系。

      开发工具:STS,项目构建工具:maven,框架:springboot,操作及配置如下:

      1、引入mybatis generator插件和相关依赖

    <dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.mybatis.spring.boot</groupId>
    			<artifactId>mybatis-spring-boot-starter</artifactId>
    			<version>1.3.2</version>
    		</dependency>
    
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<scope>runtime</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		
    		<dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.9</version>
            </dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    			<plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
                        <overwrite>true</overwrite>
                        <verbose>true</verbose>
                    </configuration>
                </plugin>
    		</plugins>
    	</build>
    

      2、编写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="E:
    epositorymysqlmysql-connector-java5.1.35mysql-connector-java-5.1.35.jar"/>
        <context id="DB2Tables"  targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressDate" value="true"/>
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
            <!--数据库链接URL,用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test02" userId="root" password="root">
            </jdbcConnection>
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
            <!-- 生成模型的包名和位置-->
            <javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            <!-- 生成映射文件的包名和位置-->
            <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
                <property name="enableSubPackages" value="true"/>
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置-->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
            <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
            <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        </context>
    </generatorConfiguration>
    

        3、application.yml

    server:
      port: 8080
    
    
    spring:
        datasource:
            name: mysql_test
            type: com.alibaba.druid.pool.DruidDataSource
            #druid相关配置
            druid:
              #监控统计拦截的filters
              filters: stat
              driver-class-name: com.mysql.jdbc.Driver
              #基本属性
              url: jdbc:mysql://127.0.0.1:3306/test02?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
              username: root
              password: root
              #配置初始化大小/最小/最大
              initial-size: 1
              min-idle: 1
              max-active: 20
              #获取连接等待超时时间
              max-wait: 60000
              #间隔多久进行一次检测,检测需要关闭的空闲连接
              time-between-eviction-runs-millis: 60000
              #一个连接在池中最小生存的时间
              min-evictable-idle-time-millis: 300000
              validation-query: SELECT 'x'
              test-while-idle: true
              test-on-borrow: false
              test-on-return: false
              #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
              pool-prepared-statements: false
              max-pool-prepared-statement-per-connection-size: 20
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.example.demo.model
    

       4、在数据库创建对应的表,项目中创建dao、model、mapping文件存放的目录

    DROP TABLE IF EXISTS `t_user`;
    CREATE TABLE `t_user` (
      `user_id` int(11) NOT NULL AUTO_INCREMENT,
      `user_name` varchar(255) NOT NULL,
      `password` varchar(255) NOT NULL,
      `phone` varchar(255) NOT NULL,
      PRIMARY KEY (`user_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

        5、选中项目,右击--->run as---->run configurations 进入如下页面,在main下面的goals中输入mybatis-generator:generate -e,点击run

      控制台输出如下,刷新project,可以看到生成的model、dao及映射文件

      最终整个项目结构如下:

  • 相关阅读:
    The Worm Turns
    Equations
    Snail’s trouble
    WuKong
    Codeforces 369 C Valera and Elections
    POJ 2186 Popular Cows
    Codefroces 366 D Dima and Trap Graph (最短路)
    Codefroces 366 C Dima and Salad(dp)
    Codefroces 374 B Inna and Sequence (树状数组 || 线段树)
    Codeforces 374 C Inna and Dima (DFS)
  • 原文地址:https://www.cnblogs.com/hhhshct/p/9331043.html
Copyright © 2011-2022 走看看