注解开发也是很常见的,但是作为我来说,注解开始基本没有使用过,这次刚好也全部回忆一遍吧。常用注解如下:
- @Insert:新增
- @Update:修改
- @Delete:删除
- @Select:查询
- @Result:结果集封装
- @Results:与Result一起使用,封装多个结果集
- @One:实现一对一封装
@Many:实现一对多封装
具体的案例如下的,实际回顾完还是感觉xml方便,习惯使然吧。
代码链接: https://gitee.com/wzlove521/lagou_study/tree/master/mybatis/mybatis_multitableCRUD
```java /**
- 插入操作
@param user */ @Insert(“INSERT INTO user values (#{id}, #{name})”) void insertUser(User user);
/**
- 更新操作
@param user */ @Update(“UPDATE user SET name = #{name} WHERE id = #{id} “) void updateUser(User user);
/**
- 根据id删除
@param id */ @Delete(“DELETE FROM user WHERE id = #{id}”) void deleteUser(int id);
/**
- 根据id进行查询
- @param id
- @return */ @Select(“SELECT id, name FROM user WHERE id = #{id} “) User findById(int id);
// 测试 @Test public void testInsert() throws IOException { User user = new User(); user.setId(3); user.setName(“大智”); mapper.insertUser(user); sqlSession.commit(); sqlSession.close(); }
@Test public void testAnnoUpdate() throws IOException { User user = new User(); user.setId(3); user.setName(“智会”); mapper.updateUser(user); sqlSession.commit(); sqlSession.close(); }
@Test public void testAnnoSelect() throws IOException { User byId = mapper.findById(1); System.out.println(byId); sqlSession.close(); }
@Test public void testAnnoDelete() throws IOException { mapper.deleteUser(3); sqlSession.commit(); sqlSession.close(); }
<a name="4YESH"></a>
### 一对一
```java
/**
* 注解一对一查询
* @return
*/
@Select("SELECT * FROM orders")
@Results({
@Result(column = "id", property = "id"),
@Result(column = "name", property = "name"),
@Result(column = "create_time", property = "createTime"),
@Result(column = "user_id", property = "user", javaType = User.class,
one = @One(select = "com.wangzhi.mapper.UserMapper.findById"))
})
List<Orders> selectAllByAnno();
// 测试
// 测试类进行了优化
private OrdersMapper ordersMapper;
private SqlSession sqlSession;
@Before
public void createS() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
ordersMapper = sqlSession.getMapper(OrdersMapper.class);
}
@Test
public void testAnnoOneToOne() {
List<Orders> orders = ordersMapper.selectAllByAnno();
orders.forEach(System.out::println);
}
一对多
/**
* 一对多查询
* @return
*/
@Select("SELECT * FROM user")
@Results({
@Result(column = "id", property = "id"),
@Result(column = "name", property = "name"),
@Result(column = "id", property = "ordersList", javaType = List.class,
many = @Many(select = "com.wangzhi.mapper.OrdersMapper.selectByUserId"))
})
List<User> findAllByAnno();
@Select("SELECT * FROM orders WHERE user_id = #{userId} ")
List<Orders> selectByUserId(Integer userId);
多对多
@Select("SELECT * FROM user")
@Results({
@Result(column = "id", property = "id"),
@Result(column = "name", property = "name"),
@Result(column = "id", property = "ordersList", javaType = List.class,
many = @Many(select = "com.wangzhi.mapper.OrdersMapper.selectByUserId")),
@Result(column = "id", property = "roles", javaType = List.class,many = @Many(select = "com.wangzhi.mapper.SysRoleMapper.selectByUserId"))
})
List<User> findAllByAnnoForManyToMany();
@Select("SELECT * FROM sys_role LEFT JOIN sys_user_role sur on sys_role.id = sur.roleid WHERE sur.userid = #{userId} ")
Role selectByUserId(Integer userId);
总结一下吧,其实复杂的映射相对xml来说是将sql进行了分离,原来一个sql,现在多个sql,还是那句话多对多的本质还是一对多,就按主方在哪一方,另一方连中间表进行查询。使用还是挺方便的,自我感觉比较清晰吧,个人还是喜欢xml方式的。