1、Maven的pom.xml
2、配置文件
2.1、db.properties
2.2、mybatis.xml
2.3、log4j.xml
3、MybatisUtil工具类
4、Mapper映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.mcs.mapper.EmployeeMapper"> 4 <resultMap id="employeeResultMap" type="com.mcs.entity.Employee"> 5 <id column="id" property="id" jdbcType="INTEGER" /> 6 <result column="name" property="name" jdbcType="VARCHAR" /> 7 <result column="sex" property="sex" jdbcType="VARCHAR" /> 8 <result column="birthday" property="birthday" jdbcType="DATE" /> 9 <result column="email" property="email" jdbcType="VARCHAR" /> 10 <result column="telephone" property="telephone" jdbcType="VARCHAR" /> 11 <result column="cellphone" property="cellphone" jdbcType="VARCHAR" /> 12 <result column="address" property="address" jdbcType="VARCHAR" /> 13 <result column="department_id" property="departmentId" jdbcType="INTEGER" /> 14 </resultMap> 15 16 <!-- 新增职员,并返回插入后的ID值 --> 17 <insert id="add" keyColumn="id" keyProperty="id" useGeneratedKeys="true" parameterType="Employee"> 18 insert into t_employee 19 ( name, sex, birthday, email, telephone, cellphone, address, department_id ) 20 values 21 ( #{name}, #{sex}, #{birthday}, #{email}, #{telephone}, #{cellphone}, #{address}, #{departmentId} ) 22 </insert> 23 24 <update id="updateById" parameterType="Employee"> 25 update t_employee 26 set name = #{name,jdbcType=VARCHAR}, 27 sex = #{sex,jdbcType=VARCHAR}, 28 birthday = #{birthday,jdbcType=DATE}, 29 email = #{email,jdbcType=VARCHAR}, 30 telephone = #{telephone,jdbcType=VARCHAR}, 31 cellphone = #{cellphone,jdbcType=VARCHAR}, 32 address = #{address,jdbcType=VARCHAR}, 33 department_id = #{departmentId,jdbcType=INTEGER} 34 where id = #{id,jdbcType=INTEGER} 35 </update> 36 37 <delete id="deleteById" parameterType="Integer"> 38 delete from t_employee 39 where id = #{id} 40 </delete> 41 42 <select id="findById" parameterType="Integer" resultMap="employeeResultMap"> 43 select 44 id,name, sex, birthday, email, telephone, cellphone, address, department_id 45 from t_employee 46 where id = #{id} 47 </select> 48 49 <!-- 基本字段 --> 50 <sql id="baseColumn"> 51 id,name, sex, birthday, email, telephone, cellphone, address, department_id 52 </sql> 53 54 55 <sql id="whereParam"> 56 <where> 57 <if test="id!=null"> 58 id = #{id} 59 </if> 60 <if test="name!=null"> 61 name like #{name} 62 </if> 63 <if test="sex!=null"> 64 sex = #{sex} 65 </if> 66 <if test="departmentId!=null"> 67 department_id = #{departmentId} 68 </if> 69 </where> 70 </sql> 71 <!-- 动态查询与分页 --> 72 <select id="findListByDynamic" parameterType="EmployeeCustom" resultMap="employeeResultMap"> 73 select 74 <include refid="baseColumn"></include> 75 from t_employee 76 <include refid="whereParam"></include> 77 <if test="pageNo!=null"> 78 <if test="pageSize!=null"> 79 limit #{pageNo}, #{pageSize} 80 </if> 81 </if> 82 </select> 83 <select id="findListByDynamicCount" parameterType="EmployeeCustom" resultType="Long"> 84 select count(id) totalNumber 85 from t_employee 86 <include refid="whereParam"></include> 87 </select> 88 89 <!-- 动态更新 --> 90 <update id="dynamicUpdateById" parameterType="Employee"> 91 update t_employee 92 <!-- set标签自动判断哪个是最后一个字段,会自动去掉最后一个,号 --> 93 <set> 94 <if test="name!=null"> 95 name = #{name}, 96 </if> 97 <if test="sex!=null"> 98 sex = #{sex}, 99 </if> 100 <if test="birthday!=null"> 101 birthday = #{birthday}, 102 </if> 103 <if test="email!=null"> 104 email = #{email}, 105 </if> 106 <if test="telephone!=null"> 107 telephone = #{telephone}, 108 </if> 109 <if test="cellphone!=null"> 110 cellphone = #{cellphone}, 111 </if> 112 <if test="address!=null"> 113 address = #{address}, 114 </if> 115 <if test="departmentId!=null"> 116 department_id = #{departmentId}, 117 </if> 118 </set> 119 where id = #{id} 120 </update> 121 122 <!-- 动态批量删除,参数:Integer[] ids delete from t_employee where id in (10,12,13) --> 123 <delete id="dynamicDeleteByArray"> 124 delete from t_employee where id in 125 <!-- foreach用于迭代数组元素 open表示开始符号 close表示结束符合 separator表示元素间的分隔符 item表示迭代的数组,属性值可以任意,但提倡与方法的数组名相同 #{ids}表示数组中的每个元素值 --> 126 <foreach collection="array" open="(" close=")" separator="," item="ids"> 127 #{ids} 128 </foreach> 129 </delete> 130 131 <!-- 动态批量删除,参数:List<Integer> ids delete from t_employee where id in (10,12,13) --> 132 <delete id="dynamicDeleteByList"> 133 delete from t_employee where id in 134 <foreach collection="list" open="(" close=")" separator="," item="ids"> 135 #{ids} 136 </foreach> 137 </delete> 138 139 140 <sql id="key"> 141 <!-- 去掉最后一个, --> 142 <trim suffixOverrides=","> 143 <if test="name!=null"> 144 name, 145 </if> 146 <if test="sex!=null"> 147 sex, 148 </if> 149 <if test="birthday!=null"> 150 birthday, 151 </if> 152 <if test="email!=null"> 153 email, 154 </if> 155 <if test="telephone!=null"> 156 telephone, 157 </if> 158 <if test="cellphone!=null"> 159 cellphone, 160 </if> 161 <if test="address!=null"> 162 address, 163 </if> 164 <if test="departmentId!=null"> 165 department_id, 166 </if> 167 </trim> 168 </sql> 169 <sql id="value"> 170 <!-- 去掉最后一个, --> 171 <trim suffixOverrides=","> 172 <if test="name!=null"> 173 #{name}, 174 </if> 175 <if test="sex!=null"> 176 #{sex}, 177 </if> 178 <if test="birthday!=null"> 179 #{birthday}, 180 </if> 181 <if test="email!=null"> 182 #{email}, 183 </if> 184 <if test="telephone!=null"> 185 #{telephone}, 186 </if> 187 <if test="cellphone!=null"> 188 #{cellphone}, 189 </if> 190 <if test="address!=null"> 191 #{address}, 192 </if> 193 <if test="departmentId!=null"> 194 #{departmentId}, 195 </if> 196 </trim> 197 </sql> 198 <!-- 动态增加 --> 199 <insert id="dynamicInsert" parameterType="Employee"> 200 insert into t_employee(<include refid="key"/>) values(<include refid="value"/>) 201 </insert> 202 203 204 205 </mapper>