2. mybatis框架进阶

3、MyBatis的各种查询功能

3.1、单条件查询,一个参数

1) 字面量类型参数

UserMapper.java 接口:

User  getUserById( int  id );

UserMapper.xml文件:

<select id="getUserById"  resultType="User">
    select * from t_user where id = #{value}
</select>

2) 注解传参

User getUserById(@Param("uid") int id);
<select id="getUserById"  resultType="User">
  select * from t_user where id = #{uid}
</select>

3)MyBatis获取参数值的两种方式:${}和#{}

<select id="queryStudentList2" resultType="Student">
    select  *  from student 
    where stuName like concat(#{stuName},'%')
    and  age>#{age}
</select>

<select id="queryStudentList2" resultType="Student">
    select  *  from student 
    where stuName like '${stuName}%'
    and  age>#{age}
</select>

${}的本质就是字符串拼接,

#{}的本质就是占位符赋值

${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时, 可以自动添加单引号

eg:  select id,name,age from student where name=#{name}   -- name='cy'

       select id,name,age from student where name=${name}    -- name=c

3.2、多条件传参,多个参数

1)封装对象

<select id="findEmpByEmp" resultType="Emp">
    select *  from emp where empno=#{empNo}  and  ename =#{ename}
</select>

2)注解

 public List<Emp> queryBookList(@Param("start") int start , @Param("size")int size);
 <select id="queryEmpList"  resultType="Emp" >
     select  *  from emp  limit  #{start} , #{size}
 </select>

3.3、返回类型

1)返回单个值

<select id="countAll" resultType="int">
    select count(*) from emp
</select>

接口中:

public int  selectCount();

2)返回对象类型

<select id="findEmpByEmpNo" resultType="Emp">
    select * from emp where empno=#{value}
</select>

3) 返回结果集

<select id="findEmpList" resultType="Emp">
    select * from emp
</select>

3.4、复杂的查询

resultMap实现结果映射

1) 解决列名和实体属性名不一样的问题

<resultMap type="emp" id="empMap">
    <id property="empno" column="empno"/>
    <result property="empName" column="ename"/>
    <result property="job" column="job"/>
</resultMap>

<select id="findEmpByEmpJob" parameterType="string"  resultMap="empMap">
    select empno,ename,job from emp where job=#{value}
</select>

2)复杂关系映射 

2.1. association

比如:查询id=1001的员工信息,并显示员工所在部门信息

association 映射到javabean的某个复杂类型属性,属于复杂类型的关联,但仅处理一对一的关联关系。

1)实体类        

Class Emp{

    int eno;

    String ename;
    
    Dept dept; //多对一

}

2 ) 配置文件

EmpMapper.xml

<resultMap type="emp" id="empMap">

    <id property="empNo" column="empno"/>

    <result property="ename" column="ename"/>

    <association property="dept"  javaType="Dept">

        <id property="deptNo" column="deptno"/>

        <result property="dname" column="dname"/>

    </association>

</resultMap>

3)sql语句:

  查询员工信息并显示部门名称:

<select id="countEmp"  resultMap="empMap">

    select e.*,d.dname from emp e

    left join dept d
    
    on e.dno = d.deptno

</select>

  2.2. collection

比如查询部门号=20的部门信息,并显示此部门下所有员工信息

Collection 属性是一个集合列表,适合一对多

1)实体类

Class Dept{

    int dno;

    String dname;

    List<Emp>  empList; //一对多

}

2)配置文件 : 两表主键名ID不能相同

<resultMap type="com.bdqn.mybatis.po.Dept" id="deptMap">

    <id property="dno" column="deptno"/>

    <result property="dname" column="dname"/>

    <collection property="empList" ofType="emp">

          <id column="id" property="eid" jdbcType="integer" />

          <result  column="name" property="ename" jdbcType="varchar" />

    </collection>

</resultMap>

3)sql语句

<select id="getDeptEmp" resultMap="deptMap">

    select d.*,e.* from dept d

    inner join emp e

    on e.deptno=d.deptno

    where d.deptno=20

</select>

注意:二个配置文件尽量不要同时进行resultMap设置

4、MyBatis的增删改查

4.1、增加

接口:

public  int insertUser(User u);

配置文件:

<insert id="insertUser" >

insert into smbms_user (userCode,userName,userPassword,userRole,creationDate)

values (#{userCode},#{userName},#{userPassword},#{userRole},#{creationDate})

</insert>

4.2、更新

<update id="updateUser">

update smbms_user set userCode=#{userCode},userName=#{userName} Where id = #{id}

</update >

4.3、删除

<delete  id="delUser" >

    delete from smbms_user where id=#{id}

</delete>

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

静待花开A

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

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

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

打赏作者

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

抵扣说明:

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

余额充值