在IDEA创建文件模板——以创建MyBatis的mapper.xml文件模板为例

1、本篇博客解决的问题

我们在编码的过程中,经常会需要写一些配置文件。这些配置文件往往都有固定的格式或者说固定必须有的内容。例如XML文件中需要有的文件头,用来控制这个XML文件中能够写的标签和标签之间的嵌套先后关系。

我们经常写,当我们需要创建一个这样的文件的时候,可以直接将别的类似的文件复制粘贴过来,然后改吧改吧就可以用了。或者到官网或者其他的网站搜索一下,然后复制过来。

以上的两种方法都是可以使用的,这里再介绍一种解决方法。如果我们使用IDEA编码,在创建文件的时候,可以直接将这个文件的模板创建出来不就可以了吗?这样我们也不需要复制粘贴了,直接在模板上面修改填充就可以了。

那么我们怎么在IDEA中创建这个模板呢?

2、在IDEA中创建文件模板的步骤

我本人使用的IDEA版本是2021.1.2。不同的版本之间,在操作上可能会有点差别。

操作步骤如下所示:

1、点击IDEA上方导航栏的File,然后在下拉菜单中点击Settings  进入到如下界面中。在如下界面中选择Editor,再选择File and Code Templates(文件和代码模板),然后点击  File 。(因为我们要添加的是一个文件模板)。然后再点击File下面的    +  

 2、点击  +  以后进入到如下界面

 然后给这个模板起一个名字,也就是上图中的Name文本框,例如可以是 MyBatis-mapper。然后是Extension文本框,需要我们填写这个文件的扩展名,例如是xml。

3、以上完成以后,可以将我们这个模板的内容写到下方的空白处即可。最后点击右下角的Apply(应用)和OK

3、MyBatis中的mapper.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.ruoyi.project.exceptSolve.medicate.mapper.MedicateExceptSolveMapper">
    <resultMap id="MedicateExceptResult" type="MedicateExcept">
        <result property="time" column="time"></result>
        <result property="medicateLevel"    column="medicate_level"    />
        <result property="flowmeter"    column="flowmeter"    />
        <result property="reactionLevel"    column="reaction_level"    />
        <result property="reactionTurbidity"    column="reaction_turbidity"    />
    </resultMap>


    <select id="selectMedicateExceptSolveList" parameterType="MedicateExceptSolve" resultMap="MedicateExceptSolveResult">
        select * from medicate_except_solve
        <where>
            <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and time between #{params.beginTime} and #{params.endTime}</if>
        </where>
    </select>

    <delete id="deleteMedicateExceptSolve">
delete from medicate_except_solve
    </delete>
    <select id="selectMedicateExcept" resultMap="MedicateExceptResult">
select * from medicate_except
    </select>
    <insert id="insertMedicateExceptSolve" parameterType="MedicateExceptSolve">
insert into medicate_except_solve values (#{time},#{solve})
    </insert>

    <!--    下面这条就是查询其中的故障对应的解决措施的  -->
    <select id="selectMethodByFaultId" parameterType="Long" resultType="java.lang.String">
        select method from method where fault_id=#{faultId}
    </select>

    <select id="selectMinStandardid1" resultType="Double">  <!-- 查 制药口液位允许最小值-->
        select min from standard where standard_id=1
    </select>
<!--
select标签表示的是查询操作,里面是查询语句。
id属性的值是SQL语句的唯一标识,是一个自定义的字符串。
按照编码规范,这里统一推荐使用dao接口中对应的方法名称。
resultType属性是表示执行SQL语句后,结果对应那个类型的JAVA对象。resultType的值是某个实体类的全限定名称。
比如:com.dcy.domain.Student
-->
    <select id="selectStudentById" resultType="com.dcy.domain.Student">
<!--select id,name,email,age from student where id=1001-->
select id,name,email,age from student where id=#{studentId}
    </select>
<!--    下面的语句使用了占位符,因为我们传过来的是一个java对象,可以直接使用它的属性名,放在花括号里面,
前面配上#,这就相当于是传参了-->
    <insert id="insertStudent">
        insert into student values (#{id},#{name},#{email},#{age})
    </insert>
<update id="updateUser" parameterType="User">
 		update sys_user
 		<set>
 			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
 			<if test="loginName != null and loginName != ''">login_name = #{loginName},</if>
 			<if test="userName != null and userName != ''">user_name = #{userName},</if>
 			<if test="userType != null and userType != ''">user_type = #{userType},</if>
 			<if test="email != null and email != ''">email = #{email},</if>
 			<if test="phonenumber != null and phonenumber != ''">phonenumber = #{phonenumber},</if>
 			<if test="sex != null and sex != ''">sex = #{sex},</if>
 			<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
 			<if test="password != null and password != ''">password = #{password},</if>
 			<if test="salt != null and salt != ''">salt = #{salt},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
 			<if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
 			<if test="loginDate != null">login_date = #{loginDate},</if>
 			<if test="pwdUpdateDate != null">pwd_update_date = #{pwdUpdateDate},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			<if test="remark != null">remark = #{remark},</if>
 			update_time = sysdate()
 		</set>
 		where user_id = #{userId}
	</update>
	<select id="selectDeptCount" parameterType="Dept" resultType="int">
		select count(1) from sys_dept
		where del_flag = '0'
		<if test="deptId != null and deptId != 0"> and dept_id = #{deptId} </if>
		<if test="parentId != null and parentId != 0"> and parent_id = #{parentId} </if>
	</select>
	<delete id="deleteUserRole" parameterType="Long">
 		delete from sys_user_role where user_id in
 		<foreach collection="array" item="userId" open="(" separator="," close=")">
 			#{userId}
                               </foreach> 
 	</delete>
</mapper>

创建好的截图如下所示:

 最后点击Apply和OK就可以了。

当我们在包里面New一个文件的时候,可选框中会出现这个名字的选项的。图标是对应XML文件的图标。我们只需要填写文件名即可。

猜你喜欢

转载自blog.csdn.net/weixin_46281472/article/details/126035324