idea生成mybatis代码模板

Mybatis参考

http://www.mybatis.org/mybatis-3/zh/index.html

 

Idea

idea ultimate  2018.2

创建Maven工程

 

编辑pom.xml添加生成插件

<plugin>

  <groupId>org.mybatis.generator</groupId>

  <artifactId>mybatis-generator-maven-plugin</artifactId>

  <version>1.3.2</version>

  <configuration>

    <verbose>true</verbose>

    <overwrite>true</overwrite>

  </configuration>

</plugin>

 

 

创建资源文件夹resources

创建生成规则配置文件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>
  <!--导入属性配置 -->
 
<properties resource="generator.properties"></properties>

  <!--指定特定数据库的jdbc驱动jar包的位置 -->
 
<classPathEntry location="${jdbc.driverLocation}"/>

  <context id="default" targetRuntime="MyBatis3">


    <!-- optional,旨在创建class时,对注释进行控制 -->
   
<commentGenerator>
      <property name="suppressDate" value="true" />
    </commentGenerator>


    <!--jdbc的数据库连接 -->
   
<jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}">
    </jdbcConnection>



    <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
   
<javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
        targetPackage     指定生成的model生成所在的包名
        targetProject     指定在该项目下所在的路径
    -->
   
<javaModelGenerator targetPackage="com.mybatisprj.po" targetProject="src/main/java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
   
<sqlMapGenerator targetPackage="com.mybatisprj.domain" targetProject="src/main/java">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>


    <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
            type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
            type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
            type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
    -->
   
<javaClientGenerator targetPackage="com.mybatisprj.dao" targetProject="src/main/java" type="MIXEDMAPPER">
      <property name="enableSubPackages" value="true" />

    </javaClientGenerator>



    <table schema="ncdb" tableName="zp" domainObjectName="Zp" >
      <property name="useActualColumnNames" value="true"/>

    </table>
  </context>
</generatorConfiguration>

连接数据库信息generator.properties

jdbc.driverLocation=F:\\mysql-connector-java-5.1.12-bin.jar

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.connectionURL=jdbc:mysql://localhost:3306/ncdb

jdbc.userId=root

jdbc.password=

 

配置生成

运行生成模板

 

运行工程

将创建数据库连接工程放到相应的包MyBatisDAOUtil.java

package com.mybatisprj.dao;



import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;



import java.io.IOException;

import java.io.Reader;



/**

 * @author karthy

 *

 */

public class MyBatisDAOUtil {



   private static SqlSessionFactory factory;



   private MyBatisDAOUtil() {

   }



   static {

      Reader reader = null;

      try {

         reader = Resources.getResourceAsReader("mybatis-config.xml");

      } catch (IOException e) {

         throw new RuntimeException(e.getMessage());

      }

      factory = new SqlSessionFactoryBuilder().build(reader);

   }



   public static SqlSessionFactory getSqlSessionFactory() {

      return factory;

   }

}

 

将相关配置放到resources下

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" />

   <typeAliases>

      <!--<typeAlias type="com.ibatis.model.User" alias="User"></typeAlias>-->

   </typeAliases>

   <environments default="development">

      <environment id="development">

         <transactionManager type="JDBC" />

         <dataSource type="POOLED">

            <property name="driver" value="${jdbc.driverClassName}" />

            <property name="url" value="${jdbc.url}" />

            <property name="username" value="${jdbc.username}" />

            <property name="password" value="${jdbc.password}" />

         </dataSource>

      </environment>

   </environments>

   <mappers>



      <mapper resource="./ZpMapper.xml" />

   </mappers>

</configuration>

db.properties

 
jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ncdb

jdbc.username=root

jdbc.password=
 

ZpMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.mybatisprj.dao.ZpMapper" >

  <resultMap id="BaseResultMap" type="com.mybatisprj.po.Zp" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    <id column="xh" property="xh" jdbcType="INTEGER" />

    <result column="xm" property="xm" jdbcType="CHAR" />

    <result column="xs" property="xs" jdbcType="REAL" />

    <result column="xb" property="xb" jdbcType="CHAR" />

    <result column="nl" property="nl" jdbcType="INTEGER" />

  </resultMap>

  <sql id="Example_Where_Clause" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    <where >

      <foreach collection="oredCriteria" item="criteria" separator="or" >

        <if test="criteria.valid" >

          <trim prefix="(" suffix=")" prefixOverrides="and" >

            <foreach collection="criteria.criteria" item="criterion" >

              <choose >

                <when test="criterion.noValue" >

                  and ${criterion.condition}

                </when>

                <when test="criterion.singleValue" >

                  and ${criterion.condition} #{criterion.value}

                </when>

                <when test="criterion.betweenValue" >

                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}

                </when>

                <when test="criterion.listValue" >

                  and ${criterion.condition}

                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >

                    #{listItem}

                  </foreach>

                </when>

              </choose>

            </foreach>

          </trim>

        </if>

      </foreach>

    </where>

  </sql>

  <sql id="Update_By_Example_Where_Clause" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    <where >

      <foreach collection="example.oredCriteria" item="criteria" separator="or" >

        <if test="criteria.valid" >

          <trim prefix="(" suffix=")" prefixOverrides="and" >

            <foreach collection="criteria.criteria" item="criterion" >

              <choose >

                <when test="criterion.noValue" >

                  and ${criterion.condition}

                </when>

                <when test="criterion.singleValue" >

                  and ${criterion.condition} #{criterion.value}

                </when>

                <when test="criterion.betweenValue" >

                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}

                </when>

                <when test="criterion.listValue" >

                  and ${criterion.condition}

                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >

                    #{listItem}

                  </foreach>

                </when>

              </choose>

            </foreach>

          </trim>

        </if>

      </foreach>

    </where>

  </sql>

  <sql id="Base_Column_List" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    xh, xm, xs, xb, nl

  </sql>

  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.mybatisprj.po.ZpExample" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    select

    <if test="distinct" >

      distinct

    </if>

    <include refid="Base_Column_List" />

    from zp

    <if test="_parameter != null" >

      <include refid="Example_Where_Clause" />

    </if>

    <if test="orderByClause != null" >

      order by ${orderByClause}

    </if>

  </select>

  <delete id="deleteByExample" parameterType="com.mybatisprj.po.ZpExample" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    delete from zp

    <if test="_parameter != null" >

      <include refid="Example_Where_Clause" />

    </if>

  </delete>

  <insert id="insertSelective" parameterType="com.mybatisprj.po.Zp" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    insert into zp

    <trim prefix="(" suffix=")" suffixOverrides="," >

      <if test="xh != null" >

        xh,

      </if>

      <if test="xm != null" >

        xm,

      </if>

      <if test="xs != null" >

        xs,

      </if>

      <if test="xb != null" >

        xb,

      </if>

      <if test="nl != null" >

        nl,

      </if>

    </trim>

    <trim prefix="values (" suffix=")" suffixOverrides="," >

      <if test="xh != null" >

        #{xh,jdbcType=INTEGER},

      </if>

      <if test="xm != null" >

        #{xm,jdbcType=CHAR},

      </if>

      <if test="xs != null" >

        #{xs,jdbcType=REAL},

      </if>

      <if test="xb != null" >

        #{xb,jdbcType=CHAR},

      </if>

      <if test="nl != null" >

        #{nl,jdbcType=INTEGER},

      </if>

    </trim>

  </insert>

  <select id="countByExample" parameterType="com.mybatisprj.po.ZpExample" resultType="java.lang.Integer" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    select count(*) from zp

    <if test="_parameter != null" >

      <include refid="Example_Where_Clause" />

    </if>

  </select>

  <update id="updateByExampleSelective" parameterType="map" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    update zp

    <set >

      <if test="record.xh != null" >

        xh = #{record.xh,jdbcType=INTEGER},

      </if>

      <if test="record.xm != null" >

        xm = #{record.xm,jdbcType=CHAR},

      </if>

      <if test="record.xs != null" >

        xs = #{record.xs,jdbcType=REAL},

      </if>

      <if test="record.xb != null" >

        xb = #{record.xb,jdbcType=CHAR},

      </if>

      <if test="record.nl != null" >

        nl = #{record.nl,jdbcType=INTEGER},

      </if>

    </set>

    <if test="_parameter != null" >

      <include refid="Update_By_Example_Where_Clause" />

    </if>

  </update>

  <update id="updateByExample" parameterType="map" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    update zp

    set xh = #{record.xh,jdbcType=INTEGER},

      xm = #{record.xm,jdbcType=CHAR},

      xs = #{record.xs,jdbcType=REAL},

      xb = #{record.xb,jdbcType=CHAR},

      nl = #{record.nl,jdbcType=INTEGER}

    <if test="_parameter != null" >

      <include refid="Update_By_Example_Where_Clause" />

    </if>

  </update>

  <update id="updateByPrimaryKeySelective" parameterType="com.mybatisprj.po.Zp" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

    -->

    update zp

    <set >

      <if test="xm != null" >

        xm = #{xm,jdbcType=CHAR},

      </if>

      <if test="xs != null" >

        xs = #{xs,jdbcType=REAL},

      </if>

      <if test="xb != null" >

        xb = #{xb,jdbcType=CHAR},

      </if>

      <if test="nl != null" >

        nl = #{nl,jdbcType=INTEGER},

      </if>

    </set>

    where xh = #{xh,jdbcType=INTEGER}

  </update>

</mapper>

运行代码

public class App 

{

    public static void main( String[] args )

    {

        System.out.println("Hello World!");


        SqlSession sqlSession = MyBatisDAOUtil.getSqlSessionFactory()

                .openSession();

        ZpExample zpExmp=new ZpExample();

        ZpExample.Criteria c= zpExmp.createCriteria();

        c.andXhBetween(1005, 1006);

        c.andNlEqualTo(20);

        System.out.println( sqlSession.selectList("com.mybatisprj.dao.ZpMapper.selectByExample", zpExmp).size() );

    }

}
 

 

 

猜你喜欢

转载自blog.csdn.net/caststudy/article/details/85261427
今日推荐