MyBatis的常用注解
这几年来注解开发越来越流行,Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper
映射文件了。我们先围绕一些基本的CRUD来学习,再学习复杂映射多表操作。
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
我们完成简单的user表的增删改查的操作
private UserMapper mapper; @Before public void before() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); mapper = sqlSession.getMapper(UserMapper.class); }
@Test public void testSave(){ User user = new User(); user.setUsername("tom"); user.setPassword("abc"); mapper.save(user); } @Test public void testUpdate(){ User user = new User(); user.setId(8); user.setUsername("lucy"); user.setPassword("123"); mapper.update(user); } @Test public void testDelete(){ mapper.delete(9); } @Test public void testFindById(){ User user = mapper.findById(2); System.out.println(user); } @Test public void testFindAll(){ List<User> all = mapper.findAll(); for (User user : all) { System.out.println(user); } }
1.3 MyBatis的注解实现复杂映射开发
实现复杂关系映射之前我们可以在映射文件中通过配置<resultMap>来实现,使用注解开发后,我们可以使用@Results注解
,@Result注解,@One注解,@Many注解组合完成复杂关系的配置
UserMapper
public interface UserMapper { @Insert("insert into user values(#{id},#{username},#{password},#{birthday})") public void save(User user); @Update("update user set username=#{username},password=#{password} where id=#{id}") public void update(User user); @Delete("delete from user where id=#{id}") public void delete(int id); @Select("select * from user where id=#{id}") public User findById(int id); @Select("select * from user") public List<User> findAll(); @Select("select * from user") @Results({ @Result(id=true ,column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), @Result( property = "orderList", column = "id", javaType = List.class, many = @Many(select = "com.itheima.mapper.OrderMapper.findByUid") ) }) public List<User> findUserAndOrderAll(); @Select("SELECT * FROM USER") @Results({ @Result(id = true,column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), @Result( property = "roleList", column = "id", javaType = List.class, many = @Many(select = "com.itheima.mapper.RoleMapper.findByUid") ) }) public List<User> findUserAndRoleAll(); }
OrderMapper
@Select("select * from orders where uid=#{uid}") public List<Order> findByUid(int uid); @Select("select * from orders") @Results({ @Result(column = "id",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result( property = "user", //要封装的属性名称 column = "uid", //根据那个字段去查询user表的数据 javaType = User.class, //要封装的实体类型 //select属性 代表查询那个接口的方法获得数据 one = @One(select = "com.itheima.mapper.UserMapper.findById") ) }) public List<Order> findAll();
RoleMapper
public interface RoleMapper { @Select("SELECT * FROM sys_user_role ur,sys_role r WHERE ur.roleId=r.id AND ur.userId=#{uid}") public List<Role> findByUid(int uid); }