<?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"> <!--提供基本版,依据业务情况,酌情添加-->
<!--author XiJun.Gong--> <mapper namespace="com.qunar.qexam2.course.dao.CourseDao"> <insert id="insertCourse" parameterType="com.qunar.qexam2.course.model.CourseDomain"
useGeneratedKeys="true" keyProperty="id">
INSERT INTO
course
(
classification_id,
course_name,
create_time,
create_user,
update_time,
update_user,
is_delete
)
VALUES
(
#{classificationId},
#{name},
#{createTime},
#{creatorTalkId},
#{modifyTime},
#{menderTalkId},
#{isDelete}
)
</insert>
<!--批量插入数据-->
<insert id="insertCourseBatch" parameterType="com.qunar.qexam2.course.model.CourseDomain">
INSERT INTO
course
(
classification_id ,
course_name ,
create_time ,
create_user,
update_time,
update_user,
is_delete
)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.classificationId},
#{item.name} ,
#{item.createTime} ,
#{item.creatorTalkId},
#{item.modifyTime},
#{item.menderTalkId},
#{item.isDelete}
)
</foreach> </insert> <!--物理删除-->
<delete id="deleteCourse"> DELETE course1 , question1
FROM course course1
JOIN question question1
ON course1.id = question1.course_id
AND question1.use_count = 0 WHERE
course1.id = #{CourseId}; </delete> <update id="updateCourse" parameterType="com.qunar.qexam2.course.model.CourseDomain"> UPDATE course
<set> <if test="classificationId != null and classificationId !='' ">
course.classification_id = #{classificationId} ,
</if> <if test=" name != null and name != '' ">
course.course_name = #{name} ,
</if> <if test="createTime != null and createTime !='' ">
course.create_time = #{createTime} ,
</if> <if test="creatorTalkId != null and creatorTalkId!='' ">
course.create_user = #{creatorTalkId} ,
</if> <if test="modifyTime != null and modifyTime != '' ">
course.update_time = #{modifyTime} ,
</if> <if test="menderTalkId != null and menderTalkId !='' ">
course.update_user = #{menderTalkId} ,
</if> <if test="isDelete != null and isDelete != '' ">
course.is_delete = #{isDelete}
</if> </set>
WHERE course.id =#{id} </update> <!--逻辑删除-->
<update id="updateCourseStatus">
UPDATE course AS course1
JOIN question AS question1
ON course1.id = question1.course_id
AND question1.use_count = 0
SET
course1.is_delete = 1,
question1.is_delete = 1
WHERE
course1.id = #{CourseId} </update> <!--分类逻辑删除-->
<!--逻辑删除-->
<update id="updateBatchCourseStatus"> UPDATE course As course1
JOIN question As question1
ON
course1.id = question1.course_id
AND question1.use_count = 0
SET course1.is_delete = 1,
question1.is_delete = 1
WHERE
course.classification_id = #{classificationId}
</update> <!--逻辑添加-->
<update id="updateCourseOnline">
UPDATE course
SET course.is_delete = 0
WHERE
course.id = #{CourseId}
</update> <select id="selectCourses" resultType="com.qunar.qexam2.course.model.CourseDomain"> SELECT
course.id as id ,
course.classification_id as classificationId ,
course.course_name as name ,
course.create_time as createTime ,
course.create_user as creatorTalkId ,
course.update_time as modifyTime ,
course.update_user as menderTalkId ,
course.is_delete as isDelete FROM course <where> <if test="classificationId != null and classificationId != '' ">
AND
course.classification_id = #{classificationId}
</if>
<if test="isDelete != null and isDelete != '' ">
AND
course.is_delete = #{isDelete}
</if> </where>
order by course.create_time desc
</select> <select id="selectCourseVoAll" resultType="com.qunar.qexam2.course.vo.CourseVo">
SELECT
course.id AS id,
course.course_name AS name
FROM course
WHERE
course.is_delete = 0
ORDER BY course.create_time DESC
</select> <select id="CountCourses" resultType="com.qunar.qexam2.course.model.CourseDomain">
SELECT COUNT(*)
FROM course
<where>
<if test="classificationId != null and classificationId != '' ">
AND
course.classification_id = #{classificationId}
</if>
AND
course.is_delete = 0
</where>
</select> <!--统计未进行逻辑删除的课程-->
<select id="CountCoursesByCourseId" resultType="java.lang.Integer">
SELECT count(*)
FROM course
WHERE course.is_delete = 0
</select> <!--统计多个分类下的课程数目-->
<select id="CountCoursesByCategoryId" resultType="Integer">
SELECT COUNT(*)
FROM course
WHERE
course.is_delete = 0
AND course.classification_id IN
<foreach item="classificationId" index="index" collection="classificationIdList"
open="(" separator="," close=")">
#{classificationId}
</foreach>
</select> <select id="selectCourseByName" resultType="com.qunar.qexam2.course.model.CourseDomain"> SELECT
course.id as id ,
course.classification_id as classificationId ,
course.course_name as name ,
course.create_time as createTime ,
course.create_user as creatorTalkId ,
course.update_time as modifyTime ,
course.update_user as menderTalkId ,
course.is_delete as isDelete FROM course
<where>
<if test="CourseName != null and CourseName != '' ">
AND
course.course_name like #{CourseName}
</if>
<if test="isDelete != null and isDelete != '' ">
AND
course.is_delete = #{isDelete}
</if>
</where> order by course.create_time desc
</select> <select id="selectCourse" resultType="com.qunar.qexam2.course.model.CourseDomain"> SELECT
course.id as id ,
course.classification_id as classificationId ,
course.course_name as name ,
course.create_time as createTime ,
course.create_user as creatorTalkId ,
course.update_time as modifyTime ,
course.update_user as menderTalkId ,
course.is_delete as isDelete FROM course
<where>
<if test="CourseId != null and CourseId != '' ">
AND
course.id = #{CourseId}
</if>
<if test="isDelete != null and isDelete !='' ">
AND
course.is_delete = #{isDelete}
</if>
</where>
order by course.create_time desc
</select> <select id="selectCourseWithoutLimit"
resultType="com.qunar.qexam2.course.model.CourseDomain"> SELECT
course.id AS id,
course.classification_id AS classificationId,
course.course_name AS name,
course.create_time AS createTime,
course.create_user AS creatorTalkId,
course.update_time AS modifyTime,
course.update_user AS menderTalkId,
course.is_delete AS isDelete
FROM course WHERE course.is_delete = 0
ORDER BY course.create_time DESC
</select> <select id="selectCourseByAuthor" resultType="com.qunar.qexam2.course.model.CourseDomain"> SELECT
course.id as id ,
course.classification_id as classificationId ,
course.course_name as name ,
course.create_time as createTime ,
course.create_user as creatorTalkId ,
course.update_time as modifyTime ,
course.update_user as menderTalkId ,
course.is_delete as isDelete FROM course
<where>
<if test="creatorTalkId != null and creatorTalkId != '' ">
AND
course.create_user = #{creatorTalkId}
</if> <if test="isDelete != null and isDelete != '' ">
AND
course.is_delete = #{isDelete}
</if>
</where>
order by course.create_time desc
</select> <select id="selectClassificationName" resultType="java.lang.String">
SELECT classification.tag_name
FROM course
LEFT JOIN classification
ON course.classification_id = classification.id
<where>
<if test="courseId != null and courseId !='' ">
AND
course.id = courseId
</if>
</where>
</select> <select id="queryCourseVoByCategoryId" resultType="com.qunar.qexam2.course.vo.CourseVo">
SELECT
course.id AS id ,
course.course_name AS name,
FROM course
<where>
<if test="classificationId != null and classificationId != '' ">
AND
course.classification_id = #{classificationId}
</if>
</where>
</select> <!--依照分类来返回课程列表信息-->
<select id="queryCourseInfVoByCategoryId" resultType="com.qunar.qexam2.course.vo.CourseInfoVo"> SELECT
course.id AS id ,
course.course_name AS courseName,
course.create_user AS createUser,
course.create_time As createTime
FROM course <where>
<if test="classificationId != null and classificationId != '' ">
AND
course.classification_id = #{classificationId}
</if>
</where> </select> <!--依照分类来返回课程列表信息-->
<select id="queryCourseInfVoByCategoryIdList" resultType="com.qunar.qexam2.course.vo.CourseInfoVo">
SELECT
course.id AS id ,
course.course_name AS courseName,
course.create_user AS createUser,
course.create_time As createTime
FROM course
WHERE course.classification_id IN
<foreach item="classificationId" index="index" collection="classificationIdList"
open="(" separator="," close=")">
#{classificationId}
</foreach>
</select> <!--
&lt;!&ndash;给予课程Id查询课程所属的一二级部门部门&ndash;&gt;
<select id="queryCourseAffiliation" resultType="com.qunar.qexam2.course.vo.CourseAffiliation"> SELECT
course1.course_name AS courseName ,
category1.tag_name As firstDepart ,
category2.tag_name AS secondDepart FROM
course course1 JOIN classification category2 ON
category2.id = course1.classification_id JOIN classification category1 ON
category1.id = category2.parent_id WHERE course1.id = #{courseId} </select>
--> <!--依照分类来返回课程列表信息-->
<select id="queryCategoryIdByCourseId" resultType="java.lang.Integer">
SELECT
course.classification_id AS classificationId
FROM course
WHERE course.id IN
<foreach item="courseId" index="index" collection="courseIdList"
open="(" separator="," close=")">
#{courseId}
</foreach>
</select> <!--依照分类Id来返回课程Id列表信息-->
<select id="queryCourseIdByCategoryId" resultType="java.lang.Integer">
SELECT
course.id
FROM course
WHERE course.classification_id IN
<foreach item="classificationId" index="index" collection="categoryIdList"
open="(" separator="," close=")">
#{classificationId}
</foreach>
</select> </mapper>

mybatis中crud操作范例的更多相关文章

  1. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  2. 05 Mybatis的CRUD操作和Mybatis连接池

    1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...

  3. 【mybatis】mybatis中insert操作,返回自增id

    需求是这样的: mybatis中insert操作,返回自增id,因为这个自增id需要给后续业务用到. 原本是这样的: 将insert语句传入,正常执行insert操作,返回int永远是 0[失败] 或 ...

  4. MVC3和MVC4中CRUD操作

    MVC3中EF实现的CRUD操作 public class HomeController : Controller { // // GET: /Home/ CarModelContainer db = ...

  5. MyBatis学习01(初识MyBatis和CRUD操作实现)

    1.初识MyBatis 环境说明: jdk 8 + MySQL 5.7.19 maven-3.6.1 IDEA 学习前需要掌握: JDBC MySQL Java 基础 Maven Junit 什么是M ...

  6. Mybatis:CRUD操作

    提示: Mapper配置文件的命名空间为对应接口包名+接口名字,这个经常会忘记和搞错的!! select标签 在接口中编写三个查询方法 //获取全部用户List<User> selectU ...

  7. MyBatis的CRUD操作

    MyBatis的两个主要配置文件 mytatis.xml:放在src目录下,常见的配置如下 <?xml version="1.0" encoding="UTF-8& ...

  8. Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作

    开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...

  9. java之mybatis之使用mybatis实现crud操作

    目录结构: 1.封装 mybatis 的工具类: MybatisUtil.java public class MybatisUtil { private static SqlSessionFactor ...

随机推荐

  1. java连接mysql步骤

    转自:http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html JDBC连接mysql数据库   •创建一个以JDBC连接数据库的程 ...

  2. web项目 log4j2的路径问题

    项目中用到log4j2记录日志,结果运行的时候总也不见log文件的产生. 查看官方文档得知,在web项目中使用log4j2需要加入log4j-web包 log4j2.xml <?xml vers ...

  3. django关系对象映射(Object Relational Mapping,简称ORM)

    Model 创建数据库,设计表结构和字段 django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表 from django.db import models clas ...

  4. 利用反射+AOP,封装Basehandler

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点, ...

  5. ML-分类与逻辑回归

    布尔分类(binary classification)问题: 训练集:$S=\{(x^{(i)}, y^{(i)})\}$ 输入:特征向量$x$ 期望输出:$y\in\{0, 1\}$ 这里使用的假设 ...

  6. [poj2182] Lost Cows (线段树)

    线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...

  7. Boost学习笔记(六) progress_display注意事项

    progress_display可以用作基本的进度显示,但它有个固有的缺陷:无法把进度显示输出与程序的输出分离. 这是因为progress_display和所有C++程序一样,都向标准输出(cout) ...

  8. Node.js开发利器

    开发工具 WebStorm,毫无疑问非他莫属,跨平台,强大的代码提示,支持Nodejs调试,此外还支持vi编辑模式,这点我很喜欢. 做些小型项目用Sublime Text. Browserify:将你 ...

  9. CVE-2016-4758: UXSS in Safari's showModalDialog

    I would like to share about details of Safari's UXSS bug(CVE-2016-4758). This bug was fixed in Safar ...

  10. Java GUI编程

    ----基础 // 创建一个窗体对象        JFrame frame = new JFrame();        // 设置窗口大小        frame.setSize(300, 20 ...