Mybatis的注解开发
MyBatis的常用注解
这几年来注解开发越来越流行,Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper映射文件了。
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
MyBatis的增删改查
我们完成简单的user表的增删改查的操作
修改MyBatis的核心配置文件,我们使用了注解替代的映射文件,所以我们只需要加载使用了注解的Mapper接口即可
<mappers><!--扫描使用注解的类--><mapper class="com.itheima.mapper.UserMapper"></mapper>
</mappers>
或者指定扫描包含映射关系的接口所在的包也可以
<mappers><!--扫描使用注解的类所在的包--><package name="com.itheima.mapper"></package>
</mappers>
userMapper mapper;@Beforepublic void before(){InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);mapper = sqlSession.getMapper(userMapper.class);}@Testpublic void tset4() {User user = new User();user.setUsername("cx");user.setPassword("159");mapper.add(user);}@Testpublic void tset5() {mapper.delete(5);}@Testpublic void tset6() {User user = new User();user.setId(4);user.setPassword("159");mapper.update(user);}@Testpublic void tset7() {System.out.println(mapper.findById(1));}
public interface userMapper {@Select(" select * from user")public List<User> findAll();@Select("select * from user where id=#{id}")public User findById(int i);@Insert(" insert into user values (#{id},#{username},#{password},#{birthday})")public void add(User user);@Update(" update user set password=#{password} where id=#{id}")public void update(User user);@Delete("delete from user where id=#{id}")public void delete(int i);}
MyBatis的注解实现复杂映射开发
实现复杂关系映射之前我们可以在映射文件中通过配置来实现,使用注解开发后,我们可以使用@Results注解,@Result注解,@One注解,@Many注解组合完成复杂关系的配置
一对一查询
一对一查询的模型
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户
创建Order和User实体
public class Order {private int id;private Date ordertime;private double total;//代表当前订单从属于哪一个客户private User user;
}public class User {private int id;private String username;private String password;private Date birthday;}
创建OrderMapper接口
public interface OrderMapper {List<Order> findAll();
}
使用注解配置Mapper
public interface OrderMapper {@Select("select * from orders")@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.controller.userMapper.findById"))})List<Order> findAll();
}
测试结果
@Testpublic void tset3() {InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);System.out.println(mapper.findAll());}
一对多查询
一对多查询的模型
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单
修改User实体
public class Order {private int id;private Date ordertime;private double total;//代表当前订单从属于哪一个客户private User user;
}public class User {private int id;private String username;private String password;private Date birthday;//代表当前用户具备哪些订单private List<Order> orderList;
}
创建UserMapper接口
List<User> findAllUserAndOrder();
使用注解配置Mapper
@Select( "select * from user")@Results({@Result(property = "username" ,column = "username"),@Result(property = "password",column = "password"),@Result(property = "birthday" ,column = "birthday"),@Result(id=true,property = "id",column = "id"),@Result(property = "orderList",column = "id",javaType = List.class,many = @Many(select = "com.controller.OrderMapper.findByUid"))})public List<User> findAllUserAndOrder();
@Select("select * from orders where uid=#{uid}")Order findByUid(int i);
测试结果
userMapper mapper;@Beforepublic void before(){InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);mapper = sqlSession.getMapper(userMapper.class);} @Testpublic void tset7() {System.out.println(mapper.findAllUserAndOrder());}
多对多查询
多对多查询的模型
用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询出该用户的所有角色
public class User {private int id;private String username;private String password;private Date birthday;//代表当前用户具备哪些订单private List<Order> orderList;//代表当前用户具备哪些角色private List<Role> roleList;
}public class Role {private int id;private String rolename;}
添加UserMapper接口方法
List<User> findAllUserAndRole();
使用注解配置Mapper
@Select( "select * from user")@Results({@Result(property = "username" ,column = "username"),@Result(property = "password",column = "password"),@Result(property = "birthday" ,column = "birthday"),@Result(id=true,property = "id",column = "id"),@Result(property = "roleList",column = "id",javaType = List.class,many = @Many(select = "com.controller.RoleMapper.findById"))})public List<User> findAllUserAndRole();
public interface RoleMapper {@Select("select r.* from sys_user_role ur,sys_role r where ur.roleid=r.id and #{id}=ur.userid")List<Role> findById(int i);
}
测试结果
@Beforepublic void before(){InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);mapper = sqlSession.getMapper(userMapper.class);}@Testpublic void tset7() {System.out.println(mapper.findAllUserAndRole());}