I only need one .xml file, this Mapper interface is redundant.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

I only need one .xml file, this Mapper interface is redundant.

xanthuim
Is it possible to automatically generate a Mapper interface by simply writing an .xml file without having to declare various Mapper interfaces?

For example, I had to write a variety of Mapper interfaces that map to .xml files. In fact, I feel that writing a Mapper interface is boring and can be dynamically generated when the project starts.

public interface UserMapper {
       
       
List<UserEntity> getAll();
       
       
UserEntity getOne(Long id);

       
void insert(UserEntity user);

       
void update(UserEntity user);

       
void delete(Long id);

}

<mapper namespace="com.neo.mapper.UserMapper" >
   
<resultMap id="BaseResultMap" type="com.neo.entity.UserEntity" >
       
<id column="id" property="id" jdbcType="BIGINT" />
       
<result column="userName" property="userName" jdbcType="VARCHAR" />
       
<result column="passWord" property="passWord" jdbcType="VARCHAR" />
       
<result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum"/>
       
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
   
</resultMap>
   
   
<sql id="Base_Column_List" >
        id, userName, passWord, user_sex, nick_name
   
</sql>

   
<select id="getAll" resultMap="BaseResultMap"  >
       SELECT
       
<include refid="Base_Column_List" />
           FROM users
   
</select>

   
<select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT
       
<include refid="Base_Column_List" />
           FROM users
           WHERE id = #{id}
   
</select>

   
<insert id="insert" parameterType="com.neo.entity.UserEntity" >
       INSERT INTO
                users
                (userName,passWord,user_sex)
        VALUES
                (#{userName}, #{passWord}, #{userSex})
   
</insert>
   
   
<update id="update" parameterType="com.neo.entity.UserEntity" >
       UPDATE
                users
       SET
       
<if test="userName != null">userName = #{userName},</if>
       
<if test="passWord != null">passWord = #{passWord},</if>
        nick_name = #{nickName}
       WHERE
                id = #{id}
   
</update>
   
   
<delete id="delete" parameterType="java.lang.Long" >
       DELETE FROM
                 users
       WHERE
                 id =#{id}
   
</delete>
</mapper>

I only need one .xml file, this Mapper interface is redundant.


Sorry, I am not good at english. I hope to understand what I mean.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I only need one .xml file, this Mapper interface is redundant.

Guy Rouillier-2
Are you familiar with MyBatis Generator?  http://www.mybatis.org/generator/

This popular tool will generate both the interface and XML file for you automatically from the database table.

The issue I see with automatically generating the interface from an XML file is that the mapping is not always obvious.  For example, in your mapper, the select getOne may return one row or multiple rows.  The tag "getOne" is meaningful to people, not to computers.  I suppose with enough scaffolding, you could construct a tool that connects to a database to determine that the id column in the users table is a primary key, and thus deduce that the query returns 1 row.  But that would have to be a pretty clever tool, given the large number of DBMSs in the world and the different ways to define a unique identifier; some DBMSs don't declare a primary key, but instead simply an identity column.

At any rate, no mechanism exists today to generate an interface file from an XML file.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 4/27/2018 11:57:35 PM
Subject: I only need one .xml file, this Mapper interface is redundant.

Is it possible to automatically generate a Mapper interface by simply writing an .xml file without having to declare various Mapper interfaces?

For example, I had to write a variety of Mapper interfaces that map to .xml files. In fact, I feel that writing a Mapper interface is boring and can be dynamically generated when the project starts.

public interface UserMapper {
       
       
List<UserEntity> getAll();
       
       
UserEntity getOne(Long id);

       
void insert(UserEntity user);

       
void update(UserEntity user);

       
void delete(Long id);

}

<mapper namespace="com.neo.mapper.UserMapper" >
   
<resultMap id="BaseResultMap" type="com.neo.entity.UserEntity" >
       
<id column="id" property="id" jdbcType="BIGINT" />
       
<result column="userName" property="userName" jdbcType="VARCHAR" />
       
<result column="passWord" property="passWord" jdbcType="VARCHAR" />
       
<result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum"/>
       
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
   
</resultMap>
   
   
<sql id="Base_Column_List" >
        id, userName, passWord, user_sex, nick_name
   
</sql>

   
<select id="getAll" resultMap="BaseResultMap"  >
       SELECT
       
<include refid="Base_Column_List" />
           FROM users
   
</select>

   
<select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT
       
<include refid="Base_Column_List" />
           FROM users
           WHERE id = #{id}
   
</select>

   
<insert id="insert" parameterType="com.neo.entity.UserEntity" >
       INSERT INTO
                users
                (userName,passWord,user_sex)
        VALUES
                (#{userName}, #{passWord}, #{userSex})
   
</insert>
   
   
<update id="update" parameterType="com.neo.entity.UserEntity" >
       UPDATE
                users
       SET
       
<if test="userName != null">userName = #{userName},</if>
       
<if test="passWord != null">passWord = #{passWord},</if>
        nick_name = #{nickName}
       WHERE
                id = #{id}
   
</update>
   
   
<delete id="delete" parameterType="java.lang.Long" >
       DELETE FROM
                 users
       WHERE
                 id =#{id}
   
</delete>
</mapper>

I only need one .xml file, this Mapper interface is redundant.


Sorry, I am not good at english. I hope to understand what I mean.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I only need one .xml file, this Mapper interface is redundant.

xanthuim
Thank you for your reply.

1. Maybe we can't directly derive from "getOne" if we want to get one or more, then can we add some attributes in the .xml file to define it?
2. Is it possible to start parsing an .xml file? For example, starting from the keywords "namespace", "id", and then dynamically generate the corresponding Mapper interface, methods, etc.?

Maybe I might think too much..

在 2018年4月28日星期六 UTC+8下午12:30:24,Guy Rouillier写道:
Are you familiar with MyBatis Generator?  <a href="http://www.mybatis.org/generator/" style="font-size:12pt" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.mybatis.org%2Fgenerator%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHWkYv7F-eDbeuqv2S9ZXi8QykRpQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.mybatis.org%2Fgenerator%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHWkYv7F-eDbeuqv2S9ZXi8QykRpQ&#39;;return true;">http://www.mybatis.org/generator/

This popular tool will generate both the interface and XML file for you automatically from the database table.

The issue I see with automatically generating the interface from an XML file is that the mapping is not always obvious.  For example, in your mapper, the select getOne may return one row or multiple rows.  The tag "getOne" is meaningful to people, not to computers.  I suppose with enough scaffolding, you could construct a tool that connects to a database to determine that the id column in the users table is a primary key, and thus deduce that the query returns 1 row.  But that would have to be a pretty clever tool, given the large number of DBMSs in the world and the different ways to define a unique identifier; some DBMSs don't declare a primary key, but instead simply an identity column.

At any rate, no mechanism exists today to generate an interface file from an XML file.

--
Guy Rouillier

------ Original Message ------
From: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="8FfN6BdcBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">xant...@...
To: "mybatis-user" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="8FfN6BdcBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@...>
Sent: 4/27/2018 11:57:35 PM
Subject: I only need one .xml file, this Mapper interface is redundant.

Is it possible to automatically generate a Mapper interface by simply writing an .xml file without having to declare various Mapper interfaces?

For example, I had to write a variety of Mapper interfaces that map to .xml files. In fact, I feel that writing a Mapper interface is boring and can be dynamically generated when the project starts.

public interface UserMapper {
       
       
List<UserEntity> getAll();
       
       
UserEntity getOne(Long id);

       
void insert(UserEntity user);

       
void update(UserEntity user);

       
void delete(Long id);

}

<mapper namespace="com.neo.mapper.UserMapper" >
   
<resultMap id="BaseResultMap" type="com.neo.entity.UserEntity" >
       
<id column="id" property="id" jdbcType="BIGINT" />
       
<result column="userName" property="userName" jdbcType="VARCHAR" />
       
<result column="passWord" property="passWord" jdbcType="VARCHAR" />
       
<result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum"/>
       
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
   
</resultMap>
   
   
<sql id="Base_Column_List" >
        id, userName, passWord, user_sex, nick_name
   
</sql>

   
<select id="getAll" resultMap="BaseResultMap"  >
       SELECT
       
<include refid="Base_Column_List" />
           FROM users
   
</select>

   
<select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT
       
<include refid="Base_Column_List" />
           FROM users
           WHERE id = #{id}
   
</select>

   
<insert id="insert" parameterType="com.neo.entity.UserEntity" >
       INSERT INTO
                users
                (userName,passWord,user_sex)
        VALUES
                (#{userName}, #{passWord}, #{userSex})
   
</insert>
   
   
<update id="update" parameterType="com.neo.entity.UserEntity" >
       UPDATE
                users
       SET
       
<if test="userName != null">userName = #{userName},</if>
       
<if test="passWord != null">passWord = #{passWord},</if>
        nick_name = #{nickName}
       WHERE
                id = #{id}
   
</update>
   
   
<delete id="delete" parameterType="java.lang.Long" >
       DELETE FROM
                 users
       WHERE
                 id =#{id}
   
</delete>
</mapper>

I only need one .xml file, this Mapper interface is redundant.


Sorry, I am not good at english. I hope to understand what I mean.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="8FfN6BdcBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: I only need one .xml file, this Mapper interface is redundant.

Jeff Butler
I agree it can seem redundant to have both XML and a matching mapper interface.  But Guy is correct that it would be difficult to derive an interface from an XML mapper.  So let me suggest a different idea - the XML is actually the redundant item.

MyBatis Generator can generate fully functional mappers that have all the function you have listed, but have no XML. These generated mappers use the MyBatis Dynamic SQL library. You don't need to use MyBatis generator if you don't want to - you could create the mappers yourself fairly easily.

You can read about MyBatis Dynamic SQL here: http://www.mybatis.org/mybatis-dynamic-sql/docs/introduction.html

Jeff Butler


On Sat, Apr 28, 2018 at 12:47 AM <[hidden email]> wrote:
Thank you for your reply.

1. Maybe we can't directly derive from "getOne" if we want to get one or more, then can we add some attributes in the .xml file to define it?
2. Is it possible to start parsing an .xml file? For example, starting from the keywords "namespace", "id", and then dynamically generate the corresponding Mapper interface, methods, etc.?

Maybe I might think too much..

在 2018年4月28日星期六 UTC+8下午12:30:24,Guy Rouillier写道:
Are you familiar with MyBatis Generator?  http://www.mybatis.org/generator/

This popular tool will generate both the interface and XML file for you automatically from the database table.

The issue I see with automatically generating the interface from an XML file is that the mapping is not always obvious.  For example, in your mapper, the select getOne may return one row or multiple rows.  The tag "getOne" is meaningful to people, not to computers.  I suppose with enough scaffolding, you could construct a tool that connects to a database to determine that the id column in the users table is a primary key, and thus deduce that the query returns 1 row.  But that would have to be a pretty clever tool, given the large number of DBMSs in the world and the different ways to define a unique identifier; some DBMSs don't declare a primary key, but instead simply an identity column.

At any rate, no mechanism exists today to generate an interface file from an XML file.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 4/27/2018 11:57:35 PM
Subject: I only need one .xml file, this Mapper interface is redundant.

Is it possible to automatically generate a Mapper interface by simply writing an .xml file without having to declare various Mapper interfaces?

For example, I had to write a variety of Mapper interfaces that map to .xml files. In fact, I feel that writing a Mapper interface is boring and can be dynamically generated when the project starts.

public interface UserMapper {
       
       
List<UserEntity> getAll();
       
       
UserEntity getOne(Long id);

       
void insert(UserEntity user);

       
void update(UserEntity user);

       
void delete(Long id);

}

<mapper namespace="com.neo.mapper.UserMapper" >
   
<resultMap id="BaseResultMap" type="com.neo.entity.UserEntity" >
       
<id column="id" property="id" jdbcType="BIGINT" />
       
<result column="userName" property="userName" jdbcType="VARCHAR" />
       
<result column="passWord" property="passWord" jdbcType="VARCHAR" />
       
<result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum"/>
       
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
   
</resultMap>
   
   
<sql id="Base_Column_List" >
        id, userName, passWord, user_sex, nick_name
   
</sql>

   
<select id="getAll" resultMap="BaseResultMap"  >
       SELECT
       
<include refid="Base_Column_List" />
           FROM users
   
</select>

   
<select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT
       
<include refid="Base_Column_List" />
           FROM users
           WHERE id = #{id}
   
</select>

   
<insert id="insert" parameterType="com.neo.entity.UserEntity" >
       INSERT INTO
                users
                (userName,passWord,user_sex)
        VALUES
                (#{userName}, #{passWord}, #{userSex})
   
</insert>
   
   
<update id="update" parameterType="com.neo.entity.UserEntity" >
       UPDATE
                users
       SET
       
<if test="userName != null">userName = #{userName},</if>
       
<if test="passWord != null">passWord = #{passWord},</if>
        nick_name = #{nickName}
       WHERE
                id = #{id}
   
</update>
   
   
<delete id="delete" parameterType="java.lang.Long" >
       DELETE FROM
                 users
       WHERE
                 id =#{id}
   
</delete>
</mapper>

I only need one .xml file, this Mapper interface is redundant.


Sorry, I am not good at english. I hope to understand what I mean.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.