トッカンソフトウェア

SpringFrameworkでMyBatisを使ってみる(Javaアプリケーション)③

今回は、MyBatis GeneratorというDBアクセス関連のファイルを自動生成してくれるツールを使ってみます。
このツールはMyBatisの公式ページ(http://www.mybatis.org/generator/ )で公開されています。


ダウンロード

公式ページよりリンクを辿り、Zipファイルをダウンロードし、適当なフォルダに解凍します。
http://blog.mybatis.org/p/products.html


上記のリンクをクリックすると以下の画面に遷移します。

https://github.com/mybatis/generator/releases

上記、Zipファイルをダウンロードし、適当なフォルダに解凍します。

設定ファイルの作成

接続先DBや対象テーブルなどを指定する設定ファイルを作成します。
				
<?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="C:\Program Files (x86)\PostgreSQL\pgJDBC\postgresql-9.4.1208.jar"/>

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

		<jdbcConnection driverClass="org.postgresql.Driver"
		 connectionURL="jdbc:postgresql://localhost:5432/postgres"
		  userId="postgres"
		   password="ps" />

		<javaModelGenerator targetPackage="spring.test"
		 targetProject="C:\workspace\springApp\src" />
		<sqlMapGenerator targetPackage="spring.test"
		 targetProject="C:\workspace\springApp\src" />
		<javaClientGenerator targetPackage="spring.test"
		 targetProject="C:\workspace\springApp\src" type="XMLMAPPER" />

		<table tableName="m_user" />
	</context>
</generatorConfiguration>

			
今回は以下のテーブルより自動生成してみます。
				
create table public.m_user (
  id character varying(8) not null
  , name character varying(16)
  , primary key (id)
);

			

自動生成ファイルの生成

Javaアプリケーションとして実行できます。
				
cd C:\work\hp\mybatis-generator-core-1.3.3
java -cp lib/mybatis-generator-core-1.3.3.jar org.mybatis.generator.api.ShellRunner -configfile generatorConfig.xml

↑画面表示が切れて行末まで見れない場合、以下となっています
java -cp lib/mybatis-generator-core-1.3.3.jar (改行)
 org.mybatis.generator.api.ShellRunner (改行)
 -configfile generatorConfig.xml

			
上記以外にも、Ant、Maven、Eclipseのプラグインなど様々な方法で実行することができますので、
興味があれば、公式ページなどで確認してください。

作成ファイル

上記までの操作で自動生成されたファイルは以下になります。

SQL設定ファイル(MUserMapper.xml)
モデル(MUser.java)
条件クラス(MUserExample.java)
マッパークラス(MUserMapper.java)

SQL設定ファイル(MUserMapper.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="spring.test.MUserMapper">
  <resultMap id="BaseResultMap" type="spring.test.MUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <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 close=")" collection="criterion.value" item="listItem" open="(" 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.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <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 close=")" collection="criterion.value" item="listItem" open="(" 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.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    id, name
  </sql>
  <select id="selectByExample" parameterType="spring.test.MUserExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from m_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    select 
    <include refid="Base_Column_List" />
    from m_user
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    delete from m_user
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="spring.test.MUserExample">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    delete from m_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="spring.test.MUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    insert into m_user (id, name)
    values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="spring.test.MUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    insert into m_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="spring.test.MUserExample" resultType="java.lang.Integer">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    select count(*) from m_user
    <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.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    update m_user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.name != null">
        name = #{record.name,jdbcType=VARCHAR},
      </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.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    update m_user
    set id = #{record.id,jdbcType=VARCHAR},
      name = #{record.name,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="spring.test.MUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    update m_user
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="spring.test.MUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 18 22:45:58 JST 2016.
    -->
    update m_user
    set name = #{name,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
</mapper>

			

モデル(MUser.java)

				
package spring.test;

public class MUser {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column m_user.id
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    private String id;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column m_user.name
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    private String name;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column m_user.id
     *
     * @return the value of m_user.id
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public String getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column m_user.id
     *
     * @param id the value for m_user.id
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column m_user.name
     *
     * @return the value of m_user.name
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public String getName() {
        return name;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column m_user.name
     *
     * @param name the value for m_user.name
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public void setName(String name) {
        this.name = name;
    }
}

			

条件クラス(MUserExample.java)

				
package spring.test;

import java.util.ArrayList;
import java.util.List;

public class MUserExample {
    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    protected String orderByClause;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    protected boolean distinct;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    protected List<Criteria> oredCriteria;

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public MUserExample() {
        oredCriteria = new ArrayList<Criteria>();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public String getOrderByClause() {
        return orderByClause;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public boolean isDistinct() {
        return distinct;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public List<Criteria> getOredCriteria() {
        return oredCriteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public void or(Criteria criteria) {
        oredCriteria.add(criteria);
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public Criteria or() {
        Criteria criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public Criteria createCriteria() {
        Criteria criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    protected Criteria createCriteriaInternal() {
        Criteria criteria = new Criteria();
        return criteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public void clear() {
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
    }

    /**
     * This class was generated by MyBatis Generator.
     * This class corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    protected abstract static class GeneratedCriteria {
        protected List<Criterion> criteria;

        protected GeneratedCriteria() {
            super();
            criteria = new ArrayList<Criterion>();
        }

        public boolean isValid() {
            return criteria.size() > 0;
        }

        public List<Criterion> getAllCriteria() {
            return criteria;
        }

        public List<Criterion> getCriteria() {
            return criteria;
        }

        protected void addCriterion(String condition) {
            if (condition == null) {
                throw new RuntimeException("Value for condition cannot be null");
            }
            criteria.add(new Criterion(condition));
        }

        protected void addCriterion(String condition, Object value, String property) {
            if (value == null) {
                throw new RuntimeException("Value for " + property + " cannot be null");
            }
            criteria.add(new Criterion(condition, value));
        }

        protected void addCriterion(String condition, Object value1, Object value2, String property) {
            if (value1 == null || value2 == null) {
                throw new RuntimeException("Between values for " + property + " cannot be null");
            }
            criteria.add(new Criterion(condition, value1, value2));
        }

        public Criteria andIdIsNull() {
            addCriterion("id is null");
            return (Criteria) this;
        }

        public Criteria andIdIsNotNull() {
            addCriterion("id is not null");
            return (Criteria) this;
        }

        public Criteria andIdEqualTo(String value) {
            addCriterion("id =", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotEqualTo(String value) {
            addCriterion("id <>", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdGreaterThan(String value) {
            addCriterion("id >", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdGreaterThanOrEqualTo(String value) {
            addCriterion("id >=", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdLessThan(String value) {
            addCriterion("id <", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdLessThanOrEqualTo(String value) {
            addCriterion("id <=", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdLike(String value) {
            addCriterion("id like", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotLike(String value) {
            addCriterion("id not like", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdIn(List<String> values) {
            addCriterion("id in", values, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotIn(List<String> values) {
            addCriterion("id not in", values, "id");
            return (Criteria) this;
        }

        public Criteria andIdBetween(String value1, String value2) {
            addCriterion("id between", value1, value2, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotBetween(String value1, String value2) {
            addCriterion("id not between", value1, value2, "id");
            return (Criteria) this;
        }

        public Criteria andNameIsNull() {
            addCriterion("name is null");
            return (Criteria) this;
        }

        public Criteria andNameIsNotNull() {
            addCriterion("name is not null");
            return (Criteria) this;
        }

        public Criteria andNameEqualTo(String value) {
            addCriterion("name =", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameNotEqualTo(String value) {
            addCriterion("name <>", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameGreaterThan(String value) {
            addCriterion("name >", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameGreaterThanOrEqualTo(String value) {
            addCriterion("name >=", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameLessThan(String value) {
            addCriterion("name <", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameLessThanOrEqualTo(String value) {
            addCriterion("name <=", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameLike(String value) {
            addCriterion("name like", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameNotLike(String value) {
            addCriterion("name not like", value, "name");
            return (Criteria) this;
        }

        public Criteria andNameIn(List<String> values) {
            addCriterion("name in", values, "name");
            return (Criteria) this;
        }

        public Criteria andNameNotIn(List<String> values) {
            addCriterion("name not in", values, "name");
            return (Criteria) this;
        }

        public Criteria andNameBetween(String value1, String value2) {
            addCriterion("name between", value1, value2, "name");
            return (Criteria) this;
        }

        public Criteria andNameNotBetween(String value1, String value2) {
            addCriterion("name not between", value1, value2, "name");
            return (Criteria) this;
        }
    }

    /**
     * This class was generated by MyBatis Generator.
     * This class corresponds to the database table m_user
     *
     * @mbggenerated do_not_delete_during_merge Thu Aug 18 22:45:58 JST 2016
     */
    public static class Criteria extends GeneratedCriteria {

        protected Criteria() {
            super();
        }
    }

    /**
     * This class was generated by MyBatis Generator.
     * This class corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    public static class Criterion {
        private String condition;

        private Object value;

        private Object secondValue;

        private boolean noValue;

        private boolean singleValue;

        private boolean betweenValue;

        private boolean listValue;

        private String typeHandler;

        public String getCondition() {
            return condition;
        }

        public Object getValue() {
            return value;
        }

        public Object getSecondValue() {
            return secondValue;
        }

        public boolean isNoValue() {
            return noValue;
        }

        public boolean isSingleValue() {
            return singleValue;
        }

        public boolean isBetweenValue() {
            return betweenValue;
        }

        public boolean isListValue() {
            return listValue;
        }

        public String getTypeHandler() {
            return typeHandler;
        }

        protected Criterion(String condition) {
            super();
            this.condition = condition;
            this.typeHandler = null;
            this.noValue = true;
        }

        protected Criterion(String condition, Object value, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.typeHandler = typeHandler;
            if (value instanceof List<?>) {
                this.listValue = true;
            } else {
                this.singleValue = true;
            }
        }

        protected Criterion(String condition, Object value) {
            this(condition, value, null);
        }

        protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.secondValue = secondValue;
            this.typeHandler = typeHandler;
            this.betweenValue = true;
        }

        protected Criterion(String condition, Object value, Object secondValue) {
            this(condition, value, secondValue, null);
        }
    }
}

			

マッパークラス(MUserMapper.java)

				
package spring.test;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import spring.test.MUser;
import spring.test.MUserExample;

public interface MUserMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int countByExample(MUserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int deleteByExample(MUserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int deleteByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int insert(MUser record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int insertSelective(MUser record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    List<MUser> selectByExample(MUserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    MUser selectByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int updateByExampleSelective(@Param("record") MUser record, @Param("example") MUserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int updateByExample(@Param("record") MUser record, @Param("example") MUserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int updateByPrimaryKeySelective(MUser record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table m_user
     *
     * @mbggenerated Thu Aug 18 22:45:58 JST 2016
     */
    int updateByPrimaryKey(MUser record);
}


			

動作確認

前々回に作成したプロジェクトの一部を修正し、動作確認を行います。

pom.xml、HelloWorldTest.javaはそのまま使用します。SpringTest.xml、SpringBean.javaは以下のように変更して下さい。

Spring用の設定ファイル(SpringTest.xml)

				
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.2.xsd
	">

	<bean id="helloWorld" class="spring.test.SpringBean">
	</bean>

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="org.postgresql.Driver" />
		<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
		<property name="username" value="postgres" />
		<property name="password" value="ps" />
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations"
			value="C:\\workspace\\springApp\\src\\spring\\test\\MUserMapper.xml" />
	</bean>

	<bean id="mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="spring.test.MUserMapper" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>
</beans>

			

SpringBean.java

				

package spring.test;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

public class SpringBean {

	@Autowired
	MUserMapper mapper;

	public void show() {

		List<MUser> users = mapper.selectByExample(null);
		for (MUser user : users) {
			System.out.println(user.getId() + "-" + user.getName());
		}
	}
}

			

プロジェクトイメージ


実行イメージ


ページのトップへ戻る
ウェア All Rights Reserved.