Mybatis-XML映射文件
- XML文件的名称与Mapper接口名称一致,并且放置在相同包下(同包同名)
- 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">
- 接下来我们就可以在映射文件中定义sql语句了
在mapper接口中定义一个方法:
package com.csdn.mapper;
import com.csdn.pojo.Emp;
import org.apache.ibatis.annotations.*;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
//根据ID删除数据
@Delete("delete from emp where id =#{id}")
/*public void delete(Integer id);*/
public int delete(Integer id);
//新增员工
@Options(keyProperty = "id", useGeneratedKeys = true)//会自动将生成的主键值,赋值给emp对象的id属性
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time,update_time) " +
"value (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
public void insert(Emp emp);
//更新员工
@Update("update emp\n" +
"set username=#{username},\n" +
" name=#{name},\n" +
" gender=#{gender},\n" +
" image=#{image},\n" +
" job=#{job},\n" +
" entrydate=#{entrydate},\n" +
" dept_id=#{deptId},\n" +
" update_time=#{updateTime}\n" +
"where id = #{id};\n")
public void update(Emp emp);
/* //根据id查询员工
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);*/
//方案一:给字段起别名,让别名与实体类属性一致
/* @Select("select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id=#{id}")
public Emp getById(Integer id);*/
//方案二:通过@Results,@Result注解手动映射封装
/* @Results({
@Result(column = "dept_id",property = "deptId"),
@Result(column = "create_time",property = "createTime"),
@Result(column = "update_time",property = "updateTime")
})
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);*/
//方案三:开启mybatis的驼峰命名自动映射开关
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);
//条件查询员工
//方式一:
/* @Select("select *\n" +
"from emp\n" +
"where name like '%${name}%'\n" +
" and #{gender} = 1\n" +
" and entrydate between #{begin} and #{end}\n" +
"order by update_time desc;")
public List<Emp> listEmp(String name, Short gender, LocalDate begin,LocalDate end);*/
//方式二:
/* @Select("select *\n" +
"from emp\n" +
"where name like concat('%',#{name},'%')\n" +
" and gender= #{gender}\n" +
" and entrydate between #{begin} and #{end}\n" +
"order by update_time desc;")
public List<Emp> listEmp(String name,Short gender, LocalDate begin, LocalDate end);*/
//通过xml配置文件来配置sql语句(条件查询员工)
public List<Emp> listEmp(String name,Short gender, LocalDate begin, LocalDate end);
}
在xml文件中写sql语句
<?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="com.csdn.mapper.EmpMapper">
<!--resultType:单条记录所封装的类型-->
<select id="listEmp" resultType="com.csdn.pojo.Emp">
select *
from emp
where name like concat('%', #{name}, '%')
and gender = #{gender}
and entrydate between #{begin} and #{end}
order by update_time desc;
</select>
</mapper>
- MybatisX插件
官方说明: https://mybatis.net.cn/getting-started.html