这里至于以mysql为例:
studentMapper.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="cn.buaa.mybatis.app4.Student">
<resultMap type="cn.buaa.mybatis.app4.Student" id="studentMap">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
<result property="sal" column="t_sal"/>
</resultMap>
<select id="findAll" parameterType="map" resultMap="studentMap">
select t_id,t_name,t_sal from students
<where>
<if test="pid != null">
and t_id = #{pid}
</if>
<if test="pname!=null">
and t_name = #{pname}
</if>
<if test="psal!=null">
and t_sal = #{psal}
</if>
</where>
</select>
<update id="dynaUpdate" parameterType="map">
update students
<set>
<if test="pname!=null">
t_name=#{pname},
</if>
<if test="psal!=null">
t_sal=#{psal},
</if>
</set>
where t_id = #{pid}
</update>
<delete id="dynaDelete" >
delete from students where t_id in
<foreach collection="array" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete>
<!-- 下面是动态插入,包括sql片段 -->
<insert id="dynaInsert" parameterType="cn.buaa.mybatis.app4.Student">
insert into students (<include refid="key"/>) values (<include refid="value"/>)
</insert>
<!-- sql片段对应字段名,id属性值任意唯一值 -->
<sql id="key">
<!-- 去掉最后一个逗号 -->
<trim suffixOverrides=",">
<if test="id!=null">
t_id,
</if>
<if test="name!=null">
t_name,
</if>
<if test="sal!=null">
t_sal,
</if>
</trim>
</sql>
<!-- sql片段对应的问号,id属性值任意唯一值 -->
<sql id="value">
<!-- 去掉最后一个逗号 -->
<trim suffixOverrides=",">
<if test="id!=null">
#{id},
</if>
<if test="name!=null">
#{name},
</if>
<if test="sal!=null">
#{sal},
</if>
</trim>
</sql>
</mapper>
StudentDao.java
package cn.buaa.mybatis.app4;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import cn.buaa.mybatis.util.MybatisUtil;
/**
* 持久层
*
* @author 梧桐下的茵
*
*/
public class StudentDao {
/**
* 按条件查询所有学生(多个条件)
*/
public List<Student> findAll(Integer id,String name,Double sal) throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("pid", id);
map.put("pname", name);
map.put("psal", sal);
List<Student> studentList = sqlSession.selectList(Student.class.getName() + ".findAll", map);
return studentList;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}
}
/**
* 有条件动态更新
*/
public void dynaUpdate(Integer id,String name,Double sal) throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("pid", id);
map.put("pname", name);
map.put("psal", sal);
sqlSession.update(Student.class.getName() + ".dynaUpdate", map);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}
}
/**
* 根据学生id批量删除
*/
public void dynaDelete(int... ids) throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
sqlSession.delete(Student.class.getName()+".dynaDelete", ids);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}
}
/**
* 根据学生id批量删除
*/
public void dynaInsert(Student student) throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
sqlSession.delete(Student.class.getName()+".dynaInsert", student);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}
}
// 测试
public static void main(String[] args) throws Exception {
StudentDao dao = new StudentDao();
/*List<Student> studentList= dao.findAll(1,"哈哈",null);
for(Student student :studentList){
System.out.println(student.toString());
}*/
/*dao.dynaUpdate(1, null, 9000D);
dao.dynaUpdate(2, "傻逼", 5000D);*/
/*dao.dynaDelete(1,3,5,7);*/
dao.dynaInsert(new Student(7,"烦死了",6500D));
dao.dynaInsert(new Student(3,"愁死了",null));
}
}
Student.java
package cn.buaa.mybatis.app4;
public class Student {
private Integer id;
private String name;
private Double sal;
public Student(Integer id, String name, Double sal) {
super();
this.id = id;
this.name = name;
this.sal = sal;
}
public Student() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sal=" + sal + "]";
}
}