MyBatis注解开发 MyBatis同时支持注解开发模式,在面对简单的配置时,注解开发可以明显的减轻开发难度
MyBatis注解类型
注解
作用
@Insert
用于执行新增语句
@Update
用于执行更新语句
@Delete
用于执行删除语句
@Select
用于执行查询语句
@Result
用于实现结果集的封装
@Results
与@Result一起使用,实现多个结果集的封装
@One
实现一对一结果集的封装
@Many
实现一对多或多对多的结果集的封装
基本使用步骤 直接在mapper接口类中使用注解 参数传入的是要执行的sql语句
1 2 3 4 5 6 7 8 9 10 public interface UserMapper { @Select("select * from user") public List<User> findAll () ; @Insert("insert into user values (#{id},#{username},#{password})") 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 (Long id) ; }
在核心文件配置扫描器,扫描指定包下的注解 1 2 3 4 5 <mappers > <package name ="cn.ywrby.mapper" > </package > </mappers >
一对一模型使用注解开发 通过@Results和@Result配置映射关系进行一对一模型的注解开发(column表示数据段名称,property表示类中属性值名称)
1 2 3 4 5 6 7 8 9 10 11 12 public interface OrderMapper { @Results({ @Result(column = "oid",property = "id"), @Result(column = "orderName",property = "orderName"), @Result(column = "orderAmount",property = "orderAmount"), @Result(column = "userId",property = "user.id"), @Result(column = "username",property = "user.username"), @Result(column = "password",property = "user.password") }) @Select("select *,o.id oid from orders o ,user u where o.userId=u.id") public List<Order> findAll () ; }
一对多/多对多模型使用注解开发 一对多模型或多对多模型使用注解开发需要分开查询,首先在用户查询方法中配置基本映射对象,配置orderList对象时需要指定javaType为集合类型,以及many属性利用@Many标签指定一对多模型查询使用的另一个表的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public interface UserMapper { @Select("select * from user") @Results({ @Result(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 = "cn.ywrby.mapper.OrderMapper.findByUid") ) }) public List<User> findAll () ; }
另一个表的查询方法,由于除了查询需要的uid,查询结果数据都是本表内的,所以不需要配置映射关系
1 2 3 4 public interface OrderMapper { @Select("select * from orders where userId=#{uid}") public List<Order> findByUid () ; }
测试用例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MyBatisTest { private UserMapper mapper; @Before public void before () throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml" ); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = factory.openSession(true ); mapper = sqlSession.getMapper(UserMapper.class); } @Test public void test () { List<User> userList=mapper.findAll(); for (User user:userList){ System.out.println(user); } } }