目录
6.1 MyBatis的常⽤注解
这⼏年来注解开发越来越流⾏,Mybatis也可以使⽤注解开发⽅式,这样我们就可以减少编写Mapper
6.2 MyBatis的增删改查
//新增用户 @Insert("insert into user values(#{id},#{username})") public void addUser(User user); //根据id修改姓名 @Update("update user set username=#{username} where id=#{id}") public void updateUser(User user); //查询用户 @Select("select * from user") public void selectUser(); //根据id删除用户 @Delete("delete from user where id=#{id}") public void deleteUser(Integer id);
编写测试方法,@Before是在方法执行执行会执行的操作,
private IUserMapper mapper; @Before public void before() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(true); mapper = sqlSession.getMapper(IUserMapper.class); } @Test public void testAddUser(){ User user = new User(); user.setId(3); user.setUsername("test"); mapper.addUser(user); }
结果
修改测试
@Test public void testUpdateUser(){ User user = new User(); user.setId(3); user.setUsername("修改test"); mapper.updateUser(user); }
查询用户
@Test public void testSelectUser(){ List<User> users = mapper.selectUser(); System.out.println("查询所有用户"+users); }
结果:
查询所有用户[User{id=1, username='lucy', roleList=[]}, User{id=2, username='tom', roleList=[]}, User{id=3, username='修改test', roleList=[]}]
删除用户
@Test public void testDeleteUser(){ mapper.deleteUser(3); }
<mappers><!-- 扫描使⽤注解的类 --><mapper class = "com.ch.mapper.IUserMapper" ></mapper></mappers>
或者指定扫描包含映射关系的接⼝所在的包也可以
<mappers><!-- 扫描使⽤注解的类所在的包 --><package name = "com.ch.mapper" ></package></mappers>
6.3 MyBatis的注解实现复杂映射开发
6.4 ⼀对⼀查询
6.4.1 ⼀对⼀查询的模型
6.4.2 ⼀对⼀查询的语句
select * from orders;select * from user where id= 查询出订单的 uid;
定义接口
IUserMapper接口
@Select("select * from user where id=#{id}") public User findUserById(Integer id);
IOrderMapper
//查询订单的同时查询该用户 @Results({ @Result(property = "id",column = "id"), @Result(property = "orderTime",column = "ordertime"), @Result(property = "total",column = "total"), @Result(property = "user",column = "uid",javaType = User.class,one = @One(select = "com.ch.mapper.IUserMapper.findUserById")) }) @Select("select * from orders") public List<Order> findOrderAndUser();
将colum="uid"传递给IUserMapper中的findUserById接口,进行查询,IUserMapper中返回的User返回给IOrderMapper进行封装
测试
@Test public void testOnoToOne(){ List<Order> orderAndUser = orderMapper.findOrderAndUser(); System.out.println("用户信息"+orderAndUser); }
测试结果
用户信息[Order{id=1, orderTime='2019-12-12', total=3000.0, user=User{id=1, username='lucy'}}, Order{id=2, orderTime='2019-12-12', total=4000.0, user=User{id=1, username='lucy'}}, Order{id=3, orderTime='2019-12-12', total=5000.0, user=User{id=2, username='tom'}}]
6.5 ⼀对多查询
6.5.1 ⼀对多查询的模型
6.5.2 ⼀对多查询的语句
select * from user;select * from orders where uid= 查询出⽤户的 id;
IOrderMapper接口定义
@Select("select * from orders where uid=#{uid}") public List<Order> findOderByUid(Integer uid);
IUserMapper接口定义
//查询用户信息,查询出每个用户关联的所有订单 @Select("select * from user") @Results({ @Result(property = "id",column = "id"), @Result(property = "username",column = "username"), @Result(property = "orderList",column = "id",javaType = List.class,many = @Many(select = "com.ch.mapper.IOrderMapper.findOderByUid")) }) public List<User> findAll();
测试结果:
用户和订单信息[User{id=1, username='lucy', orderList=[Order{id=1, orderTime='2019-12-12', total=3000.0, user=null}, Order{id=2, orderTime='2019-12-12', total=4000.0, user=null}]}, User{id=2, username='tom', orderList=[Order{id=3, orderTime='2019-12-12', total=5000.0, user=null}]}]
6.6 多对多查询
6.6.1 多对多查询的模型
6.6.2 多对多查询的语句
select * from user;select * from sys_role r,sys_user_role ur where r.id=ur.roleid and ur.userid=用户的id
创建IRoleMapper接口
package com.ch.mapper; import com.ch.pojo.Role; import org.apache.ibatis.annotations.Select; import java.util.List; public interface IRoleMapper { @Select("select * from sys_role r,sys_user_role ur where r.id=ur.roleid and ur.userid=#{uid}") public List<Role> findRoleByUid(Integer uid); }
改造IUserMapper的接口
//查询所有用户,同时查询每个用户关联的角色信息 @Select("select * from user") @Results({ @Result(property = "id",column = "id"), @Result(property = "username",column = "username"), @Result(property = "roleList",column = "id",javaType = List.class,many = @Many(select = "com.ch.mapper.IRoleMapper.findRoleByUid")) }) public List<User> findAllUserAndRole();
测试
@Test public void ManyToMany(){ List<User> allUserAndRole = mapper.findAllUserAndRole(); System.out.println("用户角色信息"+allUserAndRole); }
结果如下:
用户角色信息[User{id=1, username='lucy', roleList=[Role{id=1, roleName='CTO', roleDesc='CTO'}, Role{id=2, roleName='CEO', roleDesc='CEO'}]}, User{id=2, username='tom', roleList=[Role{id=1, roleName='CTO', roleDesc='CTO'}, Role{id=2, roleName='CEO', roleDesc='CEO'}]}]