mybatis---动态

1. 以student为例,首先为映射文件StudentMapper.xml如下

(实体类已写,mybatis-config.xml映射已写)

<!--
将数据库插入后生成的id值,同步到java对象上
    useGeneratedKeys="是否使用由数据库生成的主键"
     keyColumn="主键列的名称"
     keyProperty="主键要存入哪个属性"
-->
<insert id="insert" parameterType="com.westos.entity.Student" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
    insert into student (id,name,age) values (null,#{name},#{age})
</insert>

<!-- list (1,2,3)
collection="要遍历的集合"
item="临时变量名称"
open="循环之前的符号"
close="循环之后的符号"
separator="每个元素的分隔符"
delete from student where id in (1, 2, 3)
delete from student where id in (1, 2, 3)
-->
<delete id="delete" parameterType="int" >
  delete from student where id in
  <foreach collection="list" item="i" open="(" close=")" separator=",">
      #{i}
  </foreach>
</delete>

<!-- 利用 if标签解决多条件组合查询 -->
<!--<select id="selectByCondition" parameterType="map" resultType="com.westos.entity.Student">
    select * from student where 1=1
    <if test="name != null">
        and name like #{name}
    </if>
    <if test="minAge != null and maxAge != null">
        and age between #{minAge} and #{maxAge}
    </if>
</select>-->

<select id="selectByCondition" parameterType="map" resultType="com.westos.entity.Student">
    select * from student
    <where> <!-- 会添加where关键字,并去掉多余的and关键字 -->
        <if test="name != null">
            and name like #{name}
        </if>
        <if test="minAge != null and maxAge != null">
            and age between #{minAge} and #{maxAge}
        </if>
    </where>
</select>

<update id="update" parameterType="com.westos.entity.Student">
    update student
    <set>
        <if test="name != null">
            name = #{name},
        </if>
        <if test="age != 0">
            age = #{age},
        </if>
    </set>
    where id = #{id}
</update>

<!-- map
    .put("m", 下标);
    .put("n", 数量);
-->
<select id="selectByPage" parameterType="map" resultType="com.westos.entity.Student">
    select * from student limit #{m}, #{n}
</select>

<!-- 逻辑分页 -->
<select id="selectLogical" resultType="com.westos.entity.Student">
    select * from student
</select>

</mapper>

2.Java代码

@Test
public void test1() {
    SqlSession sqlSession = factory.openSession();
    Student stu = new Student();
    stu.setName("赵同学");
    stu.setAge(26);
    System.out.println("插入之前:" + stu.getId());
    sqlSession.insert("com.westos.mapper.StudentMapper.insert", stu);
    sqlSession.commit();
    sqlSession.close();
    System.out.println("插入之后:" + stu.getId());
}

@Test
public void test2() {
    SqlSession sqlSession = factory.openSession();
    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
//        list.add(3);
//        list.add(4);
    sqlSession.delete("com.westos.mapper.StudentMapper.delete", list);
    sqlSession.commit();
    sqlSession.close();
}

@Test
public void test3() {
    SqlSession sqlSession = factory.openSession();
    Map<String,Object> map = new HashMap<>();
     map.put("name", "张%");
     map.put("minAge", 10);
     map.put("maxAge", 20);
    List<Student> list = sqlSession.selectList("com.westos.mapper.StudentMapper.selectByCondition", map);
    for (Student student : list) {
        System.out.println(student);
    }
}

@Test
public void test4() {
    SqlSession sqlSession = factory.openSession();
    Student stu = new Student();
    stu.setId(3);
    stu.setName("张三");
    stu.setAge(28);
    sqlSession.update("com.westos.mapper.StudentMapper.update", stu);
    sqlSession.commit();
    sqlSession.close();
}

@Test
public void test5() {
    SqlSession sqlSession = factory.openSession();
    Map<String,Object> map = new HashMap<>();
    map.put("m", 0);
    map.put("n", 5);
    List<Student> list = sqlSession.selectList("com.westos.mapper.StudentMapper.selectByPage", map);
    for (Student student : list) {
        System.out.println(student);
    }
}

@Test
public void test6() {
    SqlSession sqlSession = factory.openSession();
    // new RowBounds(0, 5) 代表第一页 每页5条
    // new RowBounds(5, 5) 代表第二页 每页5条
    // new RowBounds(10, 5) 代表第三页 每页5条 ...
 List<Student> list = sqlSession.selectList("com.westos.mapper.StudentMapper.selectLogical",null,new RowBounds(0,5)
          );
    for (Student student : list) {
        System.out.println(student);
    }
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值