1、XML映射文件定义规范:
XML文件的名称与Mapper接口名一致,并且放置在相同包下(同包同名)。
XML文件的namespace属性为Mapper接口全限定名一致。
XML文件中sql语句的id与Mapper接口中的方法名一致。
动态sql:
item:遍历出来的元素。
sparator:分隔符。
open:遍历开始前拼接的sql片段。
close:遍历结束后拼接的sql片段。
sql片段:
mybatis-config.xml文件配置
- <configuration>
- <settings>
- <!-- 输出mybatis的日志-->
- <setting name="logImpl" value="STDOUT_LOGGING"/>
- <!-- 开启数据库下划线与java驼峰的自动映射-->
- <setting name="mapUnderscoreToCamelCase" value="true"></setting>
- </settings>
- <typeAliases>
- <!-- 配置别名-->
- <typeAlias type="com.example.demo.pojo.Emp" alias="Emp"/>
- <package name="com.example.demo.pojo"/>
- </typeAliases>
- </configuration>
xml映射文件:
- <mapper namespace="com.example.demo.mapper.EmpMapper">
- <!-- 列名和属性名的映射-->
- <resultMap id="EmpResultMap" type="Emp">
- <id property="id" column="id" />
- <result property="username" column="username"/>
- <result property="password" column="password"/>
- <result property="name" column="name"/>
- <result property="image" column="image"/>
- <result property="job" column="job"/>
- <result property="entrydate" column="entrydate"/>
- <result property="deptId" column="dept_id"/>
- <result property="createTime" column="create_time"/>
- <result property="updateTime" column="update_time"/>
- </resultMap>
- <sql id="insert">
- insert into emp(username,name,gender,image,job,entrydate,dept_id,create_time,update_time) values
- </sql>
- <update id="update" parameterType="Emp">
- update emp
- <set>
- <if test="username !=null">
- username=#{username},
- </if>
- <if test="name!=null">
- name=#{name},
- </if>
- <if test="image!=null">
- image=#{image},
- </if>
- <if test="gender!=null">
- gender=#{gender}
- </if>
- </set>
- where id=#{id}
- </update>
- <select id="selectAll" resultMap="EmpResultMap">
- select * from emp
- </select>
- <!-- 查找-->
- <select id="selectByCondition" resultMap="EmpResultMap">
- select username,name,gender,image,job from emp
- <where>
- <if test="name!=null">
- name like concat('%',#{name},'%')
- </if>
- <if test="gender!=null">
- and gender=#{gender}
- </if>
- <if test="startTime!=null and endTime!=null">
- and entrydate between #{startTime} and #{endTime}
- </if>
- </where>
- order by entrydate desc
- </select>
- <!-- 批量删除-->
- <delete id="deleteById">
- delete from emp where id in
- <foreach collection="list" item="id" separator="," open="(" close=")">
- #{id}
- </foreach>
- </delete>
- <!-- 批量插入-->
- <insert id="insertValues" parameterType="list">
- <include refid="insert"></include>
- <foreach item="emp" collection="list" separator=",">
- (#{emp.username},#{emp.name},#{emp.gender},#{emp.image},#{emp.job}
- ,#{emp.entrydate},#{emp.deptId},now(),now())
- </foreach>
- </insert>
-
- </mapper>
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Emp {
- private Integer id;
- private String username;
- private String password;
- private String name;
- private Short gender;
- private String image;
- private Short job;
- private LocalDate entrydate;
- private Integer deptId;
- private LocalDateTime createTime;
- private LocalDateTime updateTime;
- }
mapper接口文件:
- @Mapper
- public interface EmpMapper {
- public int update(Emp emp);
- public List
selectAll() ; - public List
selectByCondition(String name, Short gender, Date startTime,Date endTime) ; - public void deleteById(List
ids ); - public void insertValues(List
emps ); - }