一.数据交互
传入参数时不同参数传递的映射文件设置
下面列出了不同参数个数的传参设置:
1.单个参数的传递设置:
<!-- #{}的大括号中可以自定义参数名-->
<delete id="delete" parameterType="int">
delete from dept where deptno=#{deptno}
</delete>
2.多个参数的设置:
方式一:
映射文件配置:
<!-- #{}的大括号中可以使用#{arg0},#{arg1}...也可以是--#{param0},#{param1}...-->
<insert id="insert2" >
insert into dept(deptno,dname) values(#{agr0},#{arg1})
</insert>
接口文件方法:
public int insert2(Integer deptno,String dname);
测试方法中调用:
mapper.insert2(44, "hehe");
方式二:
映射文件配置:
<!-- #{}的大括号中可以使用自定义的参数名,但要在接口方法的参数前加上注解.-->
<insert id="insert2" >
insert into dept(deptno,dname) values(#{deptno},#{dname})
</insert>
接口文件的方法:
public int insert2(@Param("deptno")Integer deptno,@Param("dname")String dname);
测试方法中调用:
mapper.insert2(44, "hehe");
3.参数是包装类:
(1.)定义的两个pojo:
package com.sb.pojo;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Dept() {
super();
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
public Dept(Integer deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
package com.sb.pojo;
public class DeptWapper {
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public DeptWapper(Dept dept) {
super();
this.dept = dept;
}
public DeptWapper() {
super();
}
}
(2.)接口文件的方法:
public int insert3(DeptWapper deptWapper);
(3.)映射文件的配置:
<insert id="insert3" parameterType="com.sb.pojo.DeptWapper">
insert into dept(deptno,dname) values(#{dept.deptno},#{dept.dname})
</insert>
(4.)测试方法:
Dept dept = new Dept();
dept.setDeptno(33);
dept.setDname("123");
int insert3 = mapper.insert3(new DeptWapper(dept));
System.out.println(insert3);
返回参数时映射文件设置
1.resultType
对于一般简单的的数据类型,返回的数据直接对应一个java类,我们直接用resultType属性设置即可
映射文件配置:
<select id="query" resultType="com.sb.pojo.Dept">
select * from dept
</select>
2.resultMap
resultMap主要用来解决属性名和字段名不一致以及一对多、一对一查询等问题
例如数据库中查询到的字段名与java类属性名不一致,则可以在映射文件中做如下设置:
<!-- colume中为数据库的字段名,property中为java类的属性 -->
<resultMap type="dept" id="basedept">
<id column="deptno" property="deptno"/>
<result column="dname" property="name"/>
<result column="loc" property="loc"/>
</resultMap>
<select id="query" resultType="basedept">
select * from dept
</select>
java类的属性如下:
二.动态sql语句
动态sql的设置,主要是针对传参时的不同数据类型及具体的参数拼接成动态的sql语句来实现不同的操作,下面列出了常用标签的设置:
1.if标签:可用于判断传入的参数是否为空,常和其他标签混合使用:
<select id="query" resultType="Dept" parameterType="Dept">
select * from dept
where 1=1
<if test="deptno != null">
and deptno=#{deptno}
</if>
</select>
当调用时,若未传入deptno参数,sql语句是:
select * from dept WHERE 1=1
若传入了deptno参数,sql语句是:
select * from dept WHERE 1=1 and deptno=?
2.where标签:上个例子可以看出,为了使sql语句语法正常,需要加一句where1=1
这里利用where标签对其改进
<select id="query" resultType="Dept" parameterType="Dept">
select * from dept
<where>
<if test="deptno != null">
and deptno=#{deptno}
</if>
</where>
</select>
3.choose, when, otherwise组合标签:类似与java中的switch语句,当前一条件执行时,后面的不会执行
<select id="query1" resultType="Dept" parameterType="Dept">
select * from dept where deptno=55
<choose>
<when test="dname!=null">
and dname=#{dname}
</when>
<when test="loc!=null">
and loc=#{loc}
</when>
<otherwise>
and country=usa
</otherwise>
</choose>
</select>
只有dname参数时sql语句:select * from dept where deptno=55 and dname=?
只有loc参数时sql语句:select * from dept where deptno=55 and loc=?
无参时:select * from dept where deptno=55 and country=usa
同时有dname和loc时:select * from dept where deptno=55 and dname=?
4.set标签:主要应用在update语句上
<update id="update" parameterType="Dept">
update dept
<set>
<if test="dname != null">dname=#{dname},</if>
<if test="loc != null">loc=#{loc},</if>
</set>
where deptno=#{deptno}
</update>
调用时,只传入了dname,sql语句是:
update dept set dname=? where deptno=?
调用时,只传入了loc,sql语句是:
update dept set loc=? where deptno=?
调用时,传入了dname,loc,那么sql语句是:
update dept set dname=?,loc=? where deptno=?
5.trim标签
利用trim改写上述if标签和set标签的内容
改写if:
<select id="query" resultType="Dept" parameterType="Dept">
select * from dept
<!-- <where>
<if test="deptno != null">
and deptno=#{deptno}
</if>
</where> -->
<!-- 利用trim代替 -->
<trim prefix="where" prefixOverrides="and">
<if test="deptno != null">
and deptno=#{deptno}
</if>
</trim>
</select>
改写set:
<update id="update" parameterType="Dept">
update dept
<!-- <set>
<if test="dname != null">dname=#{dname},</if>
<if test="loc != null">loc=#{loc},</if>
</set> -->
<!-- 利用trim代替 设置前缀set 去掉最后一个占位符的","-->
<trim prefix="set" suffixOverrides=",">
<if test="dname != null">dname=#{dname},</if>
<if test="loc != null">loc=#{loc},</if>
</trim>
where deptno=#{deptno}
</update>
6.freach标签: 用来遍历,遍历的对象可以是数组,也可以是集合。
<select id="querybyids" resultType="Dept">
select * from dept where deptno in
<foreach collection="ids" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
</select>
接口方法
public List<Dept> querybyids(@Param("ids")List<Integer> ids);
方法调用:
@Test
public void test4() throws IOException {
SqlSessionFactory ss = DbUtil.getSqlSession();
SqlSession session = ss.openSession(true);
IDeptDao mapper = session.getMapper(IDeptDao.class);
List<Dept> querybyids = mapper.querybyids(Arrays.asList(10,20,30));
for (Dept dept : querybyids) {
System.out.println(dept);
}
session.close();
}
结果: