lombok
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private Short age;
private Short gender;
private String phone;
}
三个注解分别代表getter、setter方法,有参无参构造方法。可以简化实体类的定义。
mybatis数据增删改查
@Data
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
public interface EmpMapper {
@Select("select * from mybatis.emp")
public List<User> list();
@Delete("delete from mybatis.emp where id = #{id}")
public void delete(Integer id);
}
#测试
@SpringBootTest
class SpringbootMybaitisQuickstartApplicationTests {
//依赖注入
@Autowired
private EmpMapper empMapper;
@Test
public void testDelete(){
empMapper.delete(17);
}
}
//字段别名
@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);
xml映射文件
动态SQL
<if>
<where>
<set>
<foreach>
<select id="qList" resultType="com.example.pojo.Emp">
select *
from emp
<where>
<if test="name != null">
name like concat('%', #{name}, '%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
</where>
order by update_time desc
</select>
<!--collection:遍历的集合
item:遍历的元素
separator:分割符
open:遍历开始前拼接的SQL片段
close:遍历结束后拼接的SQL片段-->
<delete id="deleteByIds">
delete from emp where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<sql>
<include>
<sql id="commonSelect">
select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time
from emp
</sql>
<include refid="commonSelect"/>
<where>
<if test="name != null">
name like concat('%', #{name}, '%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
</where>
order by update_time desc
抽取公共路径
@RequestMapping
既可以作用在函数上,也可以作用在类上,连接后组成完整的路径。