Contributor contributes new data tables to Apache Linkis. When writing Mapper XML, the following specifications must be followed for development.
Method Name | Description | Core Points | Recommendations |
---|---|---|---|
insert | New data | If it is an auto-incrementing primary key, it should return the primary key ID | |
deleteById | Delete data according to the primary key ID | sql adds limit 1 by default to prevent multiple deletion of data | This method is not recommended, it is recommended to logically delete |
updateById | Modify data according to the primary key ID | sql adds limit 1 by default to prevent multiple data modification | |
selectById | Query data by primary key | Query a piece of data | |
selectByIdForUpdate | Query data according to the primary key lock | Query a piece of data by locking, for transaction processing | |
queryListByParam | Query data list according to input conditions | Multi-parameter query list | |
queryCountByParam | The total number of queries based on input conditions | The number of multi-parameter queries |
The java interface must contain @Param, and the XML can not contain parameterType
// java interface User selectUserById(@Param("id") Integer id); // XML file <select id="selectUserById" resultType="userMap"> select id, name from user where id = #{id} </select>
// java interface List<User> userListByIds(@Param("ids") List<Integer> ids); // XML file <select id="userListByIds" resultMap="userMap"> select id, name from user where id in <foreach collection="ids" separator="," open="(" close=")" item="item"> #{item} </foreach> </select>
// java interface User queryByParams(@Param("map") Map<String, Object> parasms); // XML file <select id="queryByParams" resultMap="userMap"> select id, name from user where id = #{map.id} and name = #{map.name} </select>
// java interface User queryByUser(@Param("user") User user); // XML file <select id="queryByUser" resultMap="userMap"> select id, name from user where id = #{user.id} and name = #{user.name} </select>
// java interface User queryByIdAndName(@Param("id") Integer id, @Param("name") String name); // XML file <select id="queryByIdAndName" resultMap="userMap"> select id, name from user where id = #{id} and name = #{name} </select>
Use spaces and indentation reasonably to enhance readability. Examples of various types of SQL statements are as follows
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.apache.linkins.dao.mapper.UserMapper"> -- add a statement <insert id="insert"> insert into user (id, name) values (1, 'z3'); </insert> -- delete statement <delete id = "deleteUserByIdAndName"> delete from user where name = #{name} and id = #{id} </delete> -- modify the statement <update id="updateUserById"> update user set name = #{name} where id = #{id} </update> -- Check for phrases <select id="selectUserByName" resultMap="userMap"> select id, name from user where name = 'z3' </select> -- sql fragment <sql id="user"> id, name </sql> -- Quote <include refid="user"/> -- resultMap <resultMap type="Assets" id="userMap"> <id property="id" column="id" /> <result property="name" column="name" /> </resultMap> -- Quote <select id="queryListByParam" parameterType="map" resultMap="userMap"> do... </select> -- conditional judgment <if test="name != null and name != ''"> name = #{name} </if> -- sub query <select id="selectUserByTeacherIdAndName" resultMap="userMap"> select u.id, u.name from user u where u.name in ( select t.name from teacher t where t.id = 1 and t.name = 'z3' ) and u.id = 2 </select> </mapper>