您现在的位置是:首页 > 个人日记个人日记

mybatis-10-自动代码生成

2019-02-03 18:34:32【个人日记】535人已围观

简介不用再写简单的sql语句了,解放双手

自动代码生成

项目结构

代码生成器

==红色则为自动生成==

当生成好了记得在mybatis-config.xml里配置映射文件

配置文件generatorConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!--导入属性配置-->
  7. <properties resource="jdbc.properties"></properties>
  8. <context id="mysql" targetRuntime="MyBatis3">
  9. <!-- 注释 -->
  10. <commentGenerator>
  11. <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
  12. <property name="suppressDate" value="true"/><!-- 是否生成注释代时间戳 -->
  13. </commentGenerator>
  14. <!--jdbc的数据库连接 -->
  15. <jdbcConnection
  16. driverClass="${driverClassName}"
  17. connectionURL="${url}"
  18. userId="${username}"
  19. password="${password}">
  20. </jdbcConnection>
  21. <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
  22. <javaTypeResolver>
  23. <property name="forceBigDecimals" value="false"/>
  24. </javaTypeResolver>
  25. <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
  26. targetPackage 指定生成的model生成所在的包名
  27. targetProject 指定在该项目下所在的路径
  28. -->
  29. <javaModelGenerator targetPackage="com.huangxin.order.model"
  30. targetProject="src/main/java">
  31. </javaModelGenerator>
  32. <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
  33. <sqlMapGenerator targetPackage="mapper"
  34. targetProject="src/main/resources">
  35. </sqlMapGenerator>
  36. <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
  37. type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
  38. type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
  39. type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
  40. -->
  41. <javaClientGenerator targetPackage="com.huangxin.order.mapper"
  42. targetProject="src/main/java" type="XMLMAPPER">
  43. <property name="enableSubPackages" value="true"/>
  44. </javaClientGenerator>
  45. <!-- 数据表名及实体类名称 -->
  46. <table tableName="user" domainObjectName="User"
  47. enableCountByExample="false" enableUpdateByExample="false"
  48. enableDeleteByExample="false" enableSelectByExample="false"
  49. selectByExampleQueryId="false">
  50. </table>
  51. </context>
  52. </generatorConfiguration>

这是比较简洁的版本,网上可以搜索到各种配置以及说明

==注意信息填写,一定要对应==

需要的==自动生成的插件==

配置pom.xml文件,导入插件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>mybatis-06</groupId>
  7. <artifactId>mybatis06</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!--log记录-->
  11. <dependency>
  12. <groupId>log4j</groupId>
  13. <artifactId>log4j</artifactId>
  14. <version>1.2.17</version>
  15. </dependency>
  16. <!--MySQL驱动-->
  17. <dependency>
  18. <groupId>mysql</groupId>
  19. <artifactId>mysql-connector-java</artifactId>
  20. <version>5.1.46</version>
  21. </dependency>
  22. <!-- ibatis.jar -->
  23. <dependency>
  24. <groupId>org.mybatis</groupId>
  25. <artifactId>mybatis</artifactId>
  26. <version>3.4.6</version>
  27. </dependency>
  28. <!-- junit.jar -->
  29. <dependency>
  30. <groupId>junit</groupId>
  31. <artifactId>junit</artifactId>
  32. <version>4.12</version>
  33. <scope>test</scope>
  34. </dependency>
  35. <!-- mybatis-generator自动生成代码插件 -->
  36. <dependency>
  37. <groupId>org.mybatis.generator</groupId>
  38. <artifactId>mybatis-generator-core</artifactId>
  39. <version>1.3.6</version>
  40. </dependency>
  41. </dependencies>
  42. <!-- 在build中加入以下插件 -->
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.mybatis.generator</groupId>
  47. <artifactId>mybatis-generator-maven-plugin</artifactId>
  48. <version>1.3.6</version>
  49. <configuration>
  50. <!--这是自动代码生成器的配置文件路径-->
  51. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  52. <verbose>true</verbose>
  53. <overwrite>true</overwrite>
  54. </configuration>
  55. <dependencies>
  56. <dependency>
  57. <!--依赖的sql版本-->
  58. <groupId>mysql</groupId>
  59. <artifactId>mysql-connector-java</artifactId>
  60. <version>5.1.46</version>
  61. <scope>runtime</scope>
  62. </dependency>
  63. </dependencies>
  64. </plugin>
  65. </plugins>
  66. </build>
  67. </project>

==注意配置文件的路径==

Mapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.huangxin.order.mapper.UserMapper">
  4. <resultMap id="BaseResultMap" type="com.huangxin.order.model.User">
  5. <id column="id" jdbcType="INTEGER" property="id"/>
  6. <result column="user_name" jdbcType="VARCHAR" property="userName"/>
  7. <result column="user_telephone" jdbcType="VARCHAR" property="userTelephone"/>
  8. <result column="user_sex" jdbcType="VARCHAR" property="userSex"/>
  9. </resultMap>
  10. <sql id="Base_Column_List">
  11. id, user_name, user_telephone, user_sex
  12. </sql>
  13. <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  14. select
  15. <include refid="Base_Column_List"/>
  16. from user
  17. where id = #{id,jdbcType=INTEGER}
  18. </select>
  19. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  20. delete from user
  21. where id = #{id,jdbcType=INTEGER}
  22. </delete>
  23. <insert id="insert" parameterType="com.huangxin.order.model.User">
  24. insert into user (id, user_name, user_telephone,
  25. user_sex)
  26. values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userTelephone,jdbcType=VARCHAR},
  27. #{userSex,jdbcType=VARCHAR})
  28. </insert>
  29. <insert id="insertSelective" parameterType="com.huangxin.order.model.User">
  30. insert into user
  31. <trim prefix="(" suffix=")" suffixOverrides=",">
  32. <if test="id != null">
  33. id,
  34. </if>
  35. <if test="userName != null">
  36. user_name,
  37. </if>
  38. <if test="userTelephone != null">
  39. user_telephone,
  40. </if>
  41. <if test="userSex != null">
  42. user_sex,
  43. </if>
  44. </trim>
  45. <trim prefix="values (" suffix=")" suffixOverrides=",">
  46. <if test="id != null">
  47. #{id,jdbcType=INTEGER},
  48. </if>
  49. <if test="userName != null">
  50. #{userName,jdbcType=VARCHAR},
  51. </if>
  52. <if test="userTelephone != null">
  53. #{userTelephone,jdbcType=VARCHAR},
  54. </if>
  55. <if test="userSex != null">
  56. #{userSex,jdbcType=VARCHAR},
  57. </if>
  58. </trim>
  59. </insert>
  60. <update id="updateByPrimaryKeySelective" parameterType="com.huangxin.order.model.User">
  61. update user
  62. <set>
  63. <if test="userName != null">
  64. user_name = #{userName,jdbcType=VARCHAR},
  65. </if>
  66. <if test="userTelephone != null">
  67. user_telephone = #{userTelephone,jdbcType=VARCHAR},
  68. </if>
  69. <if test="userSex != null">
  70. user_sex = #{userSex,jdbcType=VARCHAR},
  71. </if>
  72. </set>
  73. where id = #{id,jdbcType=INTEGER}
  74. </update>
  75. <update id="updateByPrimaryKey" parameterType="com.huangxin.order.model.User">
  76. update user
  77. set user_name = #{userName,jdbcType=VARCHAR},
  78. user_telephone = #{userTelephone,jdbcType=VARCHAR},
  79. user_sex = #{userSex,jdbcType=VARCHAR}
  80. where id = #{id,jdbcType=INTEGER}
  81. </update>
  82. </mapper>

==因为是机器自动生成的,所以看起来比较凌乱,如需加东西可以再次配置==

Tags: JavaWeb  

评论区

    2024-11-13 01:49:57

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我