xml开发mybatis

1、XML映射文件定义规范:

XML文件的名称与Mapper接口名一致,并且放置在相同包下(同包同名)。

XML文件的namespace属性为Mapper接口全限定名一致。

XML文件中sql语句的id与Mapper接口中的方法名一致。

动态sql:

<if>:用于判断调价是否成立。使用test属性进行条件判断,如果条件为true,则拼接SQL。

<where>:where元素只会在子元素有内容的情况下才插入where子句。而且会自动去除子句的开头的AND或OR。

<set>:动态的在行首插入set关键字,并会删掉额外的逗号。(用在update语句中)

<foreach>:属性:collection:遍历的集合。

item:遍历出来的元素。

sparator:分隔符。

open:遍历开始前拼接的sql片段。

close:遍历结束后拼接的sql片段。

sql片段:

<sql>:定义可重用的sql片段。

<include>:通过属性refid ,指定包含的sql片段。

mybatis-config.xml文件配置

复制代码
<configuration>
    <settings>
<!--        输出mybatis的日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--        开启数据库下划线与java驼峰的自动映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"></setting>
    </settings>
    <typeAliases>
<!--    配置别名-->
        <typeAlias type="com.example.demo.pojo.Emp" alias="Emp"/>
        <package name="com.example.demo.pojo"/>
    </typeAliases>
</configuration>

xml映射文件:

复制代码
<mapper namespace="com.example.demo.mapper.EmpMapper">
<!--    列名和属性名的映射-->
    <resultMap id="EmpResultMap" type="Emp">
        <id property="id" column="id" />
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="name" column="name"/>
        <result property="image" column="image"/>
        <result property="job" column="job"/>
        <result property="entrydate" column="entrydate"/>
        <result property="deptId" column="dept_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <sql id="insert">
        insert into emp(username,name,gender,image,job,entrydate,dept_id,create_time,update_time) values
    </sql>
    <update id="update" parameterType="Emp">
        update emp
        <set>
            <if test="username !=null">
                username=#{username},
            </if>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="image!=null">
                image=#{image},
            </if>
            <if test="gender!=null">
                gender=#{gender}
            </if>
        </set>
        where id=#{id}
    </update>
    <select id="selectAll" resultMap="EmpResultMap">
        select * from emp
    </select>
<!--    查找-->
    <select id="selectByCondition" resultMap="EmpResultMap">
        select username,name,gender,image,job from emp
        <where>
            <if test="name!=null">
                name like concat('%',#{name},'%')
            </if>
            <if test="gender!=null">
                and gender=#{gender}
            </if>
            <if test="startTime!=null and endTime!=null">
                and entrydate between #{startTime} and #{endTime}
            </if>
        </where>
        order by entrydate desc
    </select>
<!--    批量删除-->
    <delete id="deleteById">
        delete from emp where id in
        <foreach collection="list" item="id" separator="," open="("  close=")">
            #{id}
        </foreach>
    </delete>
<!--    批量插入-->
    <insert id="insertValues" parameterType="list">
        <include refid="insert"></include>
        <foreach item="emp" collection="list" separator=",">
            (#{emp.username},#{emp.name},#{emp.gender},#{emp.image},#{emp.job}
            ,#{emp.entrydate},#{emp.deptId},now(),now())
        </foreach>
    </insert>

</mapper>

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Short gender;
    private String image;
    private Short job;
    private LocalDate entrydate;
    private Integer deptId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

mapper接口文件:

复制代码
@Mapper
public interface EmpMapper {
    public int update(Emp emp);
    public List<Emp> selectAll();
    public List<Emp> selectByCondition(String name, Short gender, Date startTime,Date endTime);
    public void deleteById(List<Integer> ids);
    public void insertValues(List<Emp> emps);
}
相关推荐
自强的小白14 分钟前
重点!起始时间和结束时间与创建时间和修改时间的区别(localdate与LocaldateTIME区别)以及增删改查什么时候设置目前时间(添加和更新员工时)
java
雪的季节25 分钟前
Python 线程同步与异步编程 全解析
java·开发语言
吃饱了得干活37 分钟前
缓存与数据库一致性:从理论到实战
java·后端·面试
520拼好饭被践踏44 分钟前
JAVA+Agent学习day22
java·开发语言·后端·学习
笨蛋不要掉眼泪1 小时前
Java虚拟机:堆的参数配置
java·开发语言·jvm
霸道流氓气质1 小时前
SpringBoot中使用JasperReports 报表引擎 — 介绍、原理与使用实践
java·spring boot·后端
花生了什么事o1 小时前
DDD 分层架构:六层分层架构
java·架构·ddd
前网易架构师-高司机1 小时前
带标注的扑克牌识别数据集,识别率99.5%,3083张图,支持yolo,coco json,voc xml,文末有模型训练代码
xml·yolo·json·数据集·数字·扑克牌·纸牌
Y3815326621 小时前
3 种 SERP 数据处理方案对比:流式 / 批处理 / 缓存
java·spring·缓存
qq_150841992 小时前
SQL的insert和update二合一指令
java·数据库·sql