MyBatis注解开发
常用注解
@Insert: 实现新增
@Update: 实现更新
@Delete: 实现删除
@Select: 实现查询
@Result: 实现结果集封装
@Results: 可以与@Result一起使用,封装多个结果集
@ResultMap: 实现引用@Result 定义的封装
@one: 实现一对一结果集封装
@Many: 实现一对多结果集封装
@SelectProvider: 实现动态SQL映射
实现代码:单表注解
@Insert(value = "insert into tb_stu(name,age) values(#{name},#{age})")
public void save(Student student) ;
@Update("update tb_stu set name = #{name},age = #{age} where id = #{id}")
public void update(Student student);
@Delete("delete from tb_stu where id = #{id}")
public void delete(Integer id);
@Select("select * from tb_stu where id = #{id}")
public Student finById(Integer id);
@Select("select * from tb_stu")
public List<Student> findAll();
@Select("select * from tb_stu where name like #{username}")
public List<Student> findByName(String name);
实现代码:多表注解
@Select("select * from dept")
@Results(id = "base_map",value= {
@Result(id=true,property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "employyes" , column = "id",
many = @Many(select = "com.hp.mybatis.mapper.EmployeeMapper.findById"))
})
public List<Dept> findAll();
@Select("select * from dept where id = #{id}")
@ResultMap("base_map")
public Dept findDeptById(Integer id);
@Select("select * from dept where name like #{username}")
@ResultMap("base_map")
public List<Dept> findByName(String name);
对比xml