mybatis generator快速开发

使用mybatis generator快速生成实体类和mapper

使用eclipse创建一个maven项目

导入generator需要的依赖

  <dependencies>
	  <dependency>
	  	<groupId>org.mybatis.generatorgroupId>
	  	<artifactId>mybatis-generator-coreartifactId>
	  	<version>1.3.7version>
	  dependency>
	  
	<dependency>
	    <groupId>mysqlgroupId>
	    <artifactId>mysql-connector-javaartifactId>
	    <version>8.0.15version>
	dependency>
  dependencies>

安装mybatis generator插件(方便生成generator.xml)

  • 点击Help --> Eclipse Marketplace

mybatis generator快速开发_第1张图片

  • 搜索mybatis generator 点击Installed安装
    mybatis generator快速开发_第2张图片

mybatis generator快速开发_第3张图片

点击finish会弹出一个提示框点击install anyway

mybatis generator快速开发_第4张图片

安装完成重启eclipse

mybatis generator快速开发_第5张图片

在resource目录下创建generatorConfig.xml(这里就体现了插件的好处直接自动生成一下属性)

mybatis generator快速开发_第6张图片
安装插件后点击Other会找到一个mybatis的文件夹下面会有一个generator的配置文件

mybatis generator快速开发_第7张图片

创建配置文件:



<generatorConfiguration>
  <context id="context1" targetRuntime="MyBatis3Simple" defaultModelType="flat">
  
    <commentGenerator >
       <property name="suppressDate" value="true"/>
       <property name="suppressAllComments" value="true"/>
    commentGenerator>
    <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/wxxcx?useSSL=false&serverTimezone=UTC&characterEncoding=utf8" driverClass="com.mysql.cj.jdbc.Driver" password="root" userId="root" />
    <javaModelGenerator targetPackage="com.xht.pojo" targetProject="mybatis01/src/main/java" />
    <sqlMapGenerator targetPackage="com.xht.mapper" targetProject="mybatis01/src/main/resources">sqlMapGenerator>
    <javaClientGenerator targetPackage="com.xht.mapper" targetProject="mybatis01/src/main/java" type="XMLMAPPER" />
    <table  tableName="w_admin">
      
    table>
    
  context>
generatorConfiguration>
  • jdbcConnection这个一看就是数据库连接相关的。()

  • connectionURL:连接数据库的url(版本不同url有所不同:参考文档)

    • driverClass:数据库驱动(版本不同驱动有所差别:参考文档)
  • password:登录密码

    • userId:用户名
  • javaModelGenerator生成的实体类的位置

    • targetPackage:包的位置
    • targetProject:项目位置及src中main下java的位置
  • sqlMapGenerator生成mapper.xml的位置(一般xml在resources目录下不要写成java

  • javaClientGenerator:生成mapper接口的位置(这个是接口不是xml在java下

  • table顾名思义要生成那些表可以写多个 也可以用通配符%表示全部(不建议这样使用会有意想不到的结果

    • table里面有一些属性不做过多的介绍认识到这里做一些简单的使用已经没问题

最后一步运行generatorConfig.xml文件

右键generatorConfig.xml 选择Run–>Run MyBatis Generator

mybatis generator快速开发_第8张图片
查看控制台:可以看到对应的实体类 mapper接口、文件已经自动生成这个包也是他自动创建的
mybatis generator快速开发_第9张图片
实体类对应的属性get和set方法也生成了:

mybatis generator快速开发_第10张图片

**mapper:**一些简单的操作也有了



<mapper namespace="com.xht.mapper.WNewsMapper">
  <resultMap id="BaseResultMap" type="com.xht.pojo.WNews">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="origin" jdbcType="VARCHAR" property="origin" />
    <result column="type_id" jdbcType="INTEGER" property="typeId" />
    <result column="covers" jdbcType="VARCHAR" property="covers" />
    <result column="is_recom" jdbcType="INTEGER" property="isRecom" />
    <result column="is_video" jdbcType="INTEGER" property="isVideo" />
    <result column="content" jdbcType="LONGVARCHAR" property="content" />
  resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from w_news
    where id = #{id,jdbcType=INTEGER}
  delete>
  <insert id="insert" parameterType="com.xht.pojo.WNews">
    insert into w_news (id, create_time, update_time, 
      title, origin, type_id, 
      covers, is_recom, is_video, 
      content)
    values (#{id,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{title,jdbcType=VARCHAR}, #{origin,jdbcType=VARCHAR}, #{typeId,jdbcType=INTEGER}, 
      #{covers,jdbcType=VARCHAR}, #{isRecom,jdbcType=INTEGER}, #{isVideo,jdbcType=INTEGER}, 
      #{content,jdbcType=LONGVARCHAR})
  insert>
  <update id="updateByPrimaryKey" parameterType="com.xht.pojo.WNews">
    update w_news
    set create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      title = #{title,jdbcType=VARCHAR},
      origin = #{origin,jdbcType=VARCHAR},
      type_id = #{typeId,jdbcType=INTEGER},
      covers = #{covers,jdbcType=VARCHAR},
      is_recom = #{isRecom,jdbcType=INTEGER},
      is_video = #{isVideo,jdbcType=INTEGER},
      content = #{content,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=INTEGER}
  update>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select id, create_time, update_time, title, origin, type_id, covers, is_recom, is_video, 
    content
    from w_news
    where id = #{id,jdbcType=INTEGER}
  select>
  <select id="selectAll" resultMap="BaseResultMap">
    select id, create_time, update_time, title, origin, type_id, covers, is_recom, is_video, 
    content
    from w_news
  select>
  <resultMap id="BaseResultMap" type="com.xht.pojo.WNews">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="origin" jdbcType="VARCHAR" property="origin" />
    <result column="type_id" jdbcType="INTEGER" property="typeId" />
    <result column="covers" jdbcType="VARCHAR" property="covers" />
    <result column="is_recom" jdbcType="INTEGER" property="isRecom" />
    <result column="is_video" jdbcType="INTEGER" property="isVideo" />
    <result column="content" jdbcType="LONGVARCHAR" property="content" />
  resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from w_news
    where id = #{id,jdbcType=INTEGER}
  delete>
  <insert id="insert" parameterType="com.xht.pojo.WNews">
    insert into w_news (id, create_time, update_time, 
      title, origin, type_id, 
      covers, is_recom, is_video, 
      content)
    values (#{id,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{title,jdbcType=VARCHAR}, #{origin,jdbcType=VARCHAR}, #{typeId,jdbcType=INTEGER}, 
      #{covers,jdbcType=VARCHAR}, #{isRecom,jdbcType=INTEGER}, #{isVideo,jdbcType=INTEGER}, 
      #{content,jdbcType=LONGVARCHAR})
  insert>
  <update id="updateByPrimaryKey" parameterType="com.xht.pojo.WNews">
    update w_news
    set create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      title = #{title,jdbcType=VARCHAR},
      origin = #{origin,jdbcType=VARCHAR},
      type_id = #{typeId,jdbcType=INTEGER},
      covers = #{covers,jdbcType=VARCHAR},
      is_recom = #{isRecom,jdbcType=INTEGER},
      is_video = #{isVideo,jdbcType=INTEGER},
      content = #{content,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=INTEGER}
  update>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select id, create_time, update_time, title, origin, type_id, covers, is_recom, is_video, 
    content
    from w_news
    where id = #{id,jdbcType=INTEGER}
  select>
  <select id="selectAll" resultMap="BaseResultMap">
    select id, create_time, update_time, title, origin, type_id, covers, is_recom, is_video, 
    content
    from w_news
  select>
  <resultMap id="BaseResultMap" type="com.xht.pojo.WNews">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="origin" jdbcType="VARCHAR" property="origin" />
    <result column="type_id" jdbcType="INTEGER" property="typeId" />
    <result column="covers" jdbcType="VARCHAR" property="covers" />
    <result column="is_recom" jdbcType="INTEGER" property="isRecom" />
    <result column="is_video" jdbcType="INTEGER" property="isVideo" />
    <result column="content" jdbcType="LONGVARCHAR" property="content" />
  resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from w_news
    where id = #{id,jdbcType=INTEGER}
  delete>
  <insert id="insert" parameterType="com.xht.pojo.WNews">
    insert into w_news (id, create_time, update_time, 
      title, origin, type_id, 
      covers, is_recom, is_video, 
      content)
    values (#{id,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{title,jdbcType=VARCHAR}, #{origin,jdbcType=VARCHAR}, #{typeId,jdbcType=INTEGER}, 
      #{covers,jdbcType=VARCHAR}, #{isRecom,jdbcType=INTEGER}, #{isVideo,jdbcType=INTEGER}, 
      #{content,jdbcType=LONGVARCHAR})
  insert>
  <update id="updateByPrimaryKey" parameterType="com.xht.pojo.WNews">
    update w_news
    set create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      title = #{title,jdbcType=VARCHAR},
      origin = #{origin,jdbcType=VARCHAR},
      type_id = #{typeId,jdbcType=INTEGER},
      covers = #{covers,jdbcType=VARCHAR},
      is_recom = #{isRecom,jdbcType=INTEGER},
      is_video = #{isVideo,jdbcType=INTEGER},
      content = #{content,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=INTEGER}
  update>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select id, create_time, update_time, title, origin, type_id, covers, is_recom, is_video, 
    content
    from w_news
    where id = #{id,jdbcType=INTEGER}
  select>
  <select id="selectAll" resultMap="BaseResultMap">
    select id, create_time, update_time, title, origin, type_id, covers, is_recom, is_video, 
    content
    from w_news
  select>
mapper>
package com.xht.mapper;

import com.xht.pojo.WNews;
import java.util.List;

public interface WNewsMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(WNews record);

    WNews selectByPrimaryKey(Integer id);

    List<WNews> selectAll();

    int updateByPrimaryKey(WNews record);
}

大功告成

你可能感兴趣的:(MySQL,mybatis)