mybatis的 mapper.xml 书写

在application.yml文件中添加:

mybatis:
  type-aliases-package: com.auggie.student_server.entity
  mapper-locations: classpath:mapper/*.xml
  

Select单表查询

第一步:

在Mapper文件里写 抽象方法

List<CourseTeacherInfo> findByStudentId(@Param("sid") Integer sid,
                                                   @Param("term") String term);

第二步:

在resource里面写mapper文件的具体实现

注意 在namespace里要写具体实现的哪个mapper文件

<?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.auggie.student_server.mapper.StudentCourseTeacherMapper">

    写SQL。。。。

</mapper>

再具体实现中:

id 为 方法名字,用于识别具体是那个方法的实现

resultType 是结果集映射形式

结果集映射形式分为两种:

(1)自动映射 resultType

(2)手动映射 resultMap

自动映射较为简单
resultType 可以为 po的实体类
也可以是与前端交互的V0类(可以看做是po的一部分字段)


<select id="findByStudentId" resultType="CourseTeacherInfo">
        SELECT c.cid, c.cname, t.tid, t.tname, sct.grade, c.ccredit
        FROM studentms.sct sct INNER JOIN studentms.t t ON sct.tid = t.tid
                INNER JOIN studentms.c c ON sct.cid = c.cid
        <where>
            <if test="sct.sid != null">
                sct.sid = #{sid}
            </if>
            <if test="sct.term != null">
                and sct.term = #{term}
            </if>
        </where>
</select>

写的时候注意:

要有Select 标签:

然后where语句最好 写在Where标签里面
动态SQL 可以用 在 test 里加参数

如果想要在参数都为空的时候想返回全部数据
那么可以在前面加上 where 1=1(但是不太建议这么写)

Select 多表查询

这是mybatis——puls的重头戏

往往在多表查询的时候,越会体现出xml文件的重要性

多表联结往往涉及很多表的字段,yici


<select id="findBySearch" resultType="SCTInfo">
        SELECT c.cid, c.cname, t.tid, t.tname, s.sid, s.sname, sct.grade, sct.term
        FROM studentms.sct sct 
                INNER JOIN studentms.s s ON sct.sid = s.sid
                INNER JOIN studentms.t t ON sct.tid = t.tid
                INNER JOIN studentms.c c ON sct.cid = c.cid
        <where>
            <if test="sid != null">
                s.sid = #{sid}
            </if>
            <if test="tid != null">
                AND t.tid = #{tid}
            </if>
            <if test="cid != null">
                AND c.cid = #{cid}
            </if>
            <if test="sname != null">
                <if test="sFuzzy == 0">
                    AND s.sname = #{sname}
                </if>
                <if test="sFuzzy == 1">
                    AND s.sname LIKE CONCAT('%', #{sname}, '%')
                </if>
            </if>
            <if test="tname != null">
                <if test="tFuzzy == 0">
                    AND t.tname = #{tname}
                </if>
                <if test="tFuzzy == 1">
                    AND t.tname LIKE CONCAT('%', #{tname}, '%')
                </if>
            </if>
            <if test="cname != null">
                <if test="cFuzzy == 0">
                    AND c.cname = #{cname}
                </if>
                <if test="cFuzzy == 1">
                    AND c.cname LIKE CONCAT('%', #{cname}, '%')
                </if>
            </if>
            <if test="term != null">
                AND sct.term = #{term}
            </if>
            <if test="lowBound != null">
                AND sct.grade >= #{lowBound}
            </if>
            <if test="highBound != null">
                AND #{highBound} >= sct.grade
            </if>
        </where>
    </select>
    

mapper 文件接口定义:


public List<SCTInfo> findBySearch(@Param("sid") Integer sid,
                                      @Param("sname") String sname,
                                      @Param("sFuzzy") Integer sFuzzy,
                                      @Param("cid") Integer cid,
                                      @Param("cname") String cname,
                                      @Param("cFuzzy") Integer cFuzzy,
                                      @Param("tid") Integer tid,
                                      @Param("tname") String tname,
                                      @Param("tFuzzy") Integer tFuzzy,
                                      @Param("lowBound") Integer lowBound,
                                      @Param("highBound") Integer highBound,
                                      @Param("term") String term);
                                      
                                      

基于注解的接口定义

@Select(" ${参数} ")


@Select("SELECT DISTINCT sct.term FROM studentms.sct sct")
    public List<String> findAllTerm();
    

这种方式对于简单的SQL的话,可以使用,但是多表查询并不建议使用这个

因为SQL一旦发生改动,那么就需要重新编译。浪费时间

更 删 改 mapper操作


<update id="updateById">
        UPDATE studentms.c SET
                               cname = #{course.cname},
                               ccredit = #{course.ccredit}
        WHERE cid = #{course.cid};
</update>



<insert id="insertCourse">
        INSERT INTO studentms.c (cname, ccredit) VALUES (#{course.cname}, #{course.ccredit})
</insert>




<delete id="deleteById">
        DELETE FROM studentms.c WHERE cid = #{cid}
</delete>




基于注解的方式

@Insert("INSERT INTO studentms.ct (cid, tid, term) VALUES (#{cid}, #{tid}, #{term})")
    public boolean insertCourseTeacher(@Param("cid") Integer cid,
                                       @Param("tid") Integer tid,
                                       @Param("term") String term);
                                       
                                       
                    
@Delete("DELETE FROM studentms.ct WHERE cid = #{c.cid} AND tid = #{c.tid}")
    public boolean deleteById(@Param("c") CourseTeacher courseTeacher);   
    
    
@Update("Update from studentms.ct set cname = ${course.cname},ccredit = ${course.ccredit} WHERE cid = ${course.cid};")

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你们卷的我睡不着QAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值