敖胤

绳锯木断,水滴石穿;聚沙成塔,集腋成裘。

导航

Mybatis SQL的两种映射方式

1、XML映射文件

1.1、mapper
public interface EmpMapper {
   Emp queryByEmpNo(String empno);
}
1.2、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="org.silence.spring.springbootdemo.mapper.EmpMapper">
    <select id="queryByEmpNo" resultType="Emp" >
        select * from EMP where EMPNO = #{empno}
    </select>
</mapper>
1.3、test
@Test
void queryByEmpNo() {
    Emp emp = mapper.queryByEmpNo("7499");
    System.out.println(emp);
}

2、注解(不需要xml文件)

2.1、mapper
public interface DeptMapper {
    @Select("select * from dept")
    List<Dept> queryAll();

    @Select("select * from dept where deptno = #{deptno}")
    Dept queryByDeptno(String deptno);
}
2.2、test
@Test
void queryByDeptno() {
    Dept dept = mapper.queryByDeptno("20");
    System.out.println(dept);
}

posted on 2021-03-03 14:42  敖胤  阅读(109)  评论(0编辑  收藏  举报