2.Mybatis的注解开发2.1 MyBatis的常用注解 这几年来注解开发越来越流行,Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper 映射文件了。我们先围绕一些基本的CRUD来学习,再学习复杂映射多表操作。 @Insert:实现新增 @Update:实现更新 @Delete:实现删除 @Select:实现查询 @Result:实现结果集封装 @Results:可以与@Result 一起使用,封装多个结果集 @One:实现一对一结果集封装 @Many:实现一对多结果集封装 2.2 MyBatis的增删改查 我们完成简单的user表的增删改查的操作 private UserMapper userMapper;
@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
userMapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testAdd() {
User user = new User();
user.setUsername("测试数据");
user.setPassword("123");
user.setBirthday(new Date());
userMapper.add(user);
}
@Test
public void testUpdate() throws IOException {
User user = new User();
user.setId(16);
user.setUsername("测试数据修改");
user.setPassword("abc");
user.setBirthday(new Date());
userMapper.update(user);
}
@Test
public void testDelete() throws IOException {
userMapper.delete(16);
}
@Test
public void testFindById() throws IOException {
User user = userMapper.findById(1);
System.out.println(user);
}
@Test
public void testFindAll() throws IOException {
List<User> all = userMapper.findAll();
for(User user : all){
System.out.println(user);
}
}
修改MyBatis的核心配置文件,我们使用了注解替代的映射文件,所以我们只需要加载使用了注解的Mapper接口即可 <mappers>
<!--扫描使用注解的类-->
<mapper class="com.itheima.mapper.UserMapper"></mapper>
</mappers>或者指定扫描包含映射关系的接口所在的包也可以 <mappers>
<!--扫描使用注解的类所在的包-->
<package name="com.itheima.mapper"></package>
</mappers>2.3 MyBatis的注解实现复杂映射开发实现复杂关系映射之前我们可以在映射文件中通过配置<resultMap>来实现,使用注解开发后,我们可以使用@Results注解,@Result注解,@One注解,@Many注解组合完成复杂关系的配置 file://F:/JAVA/java%E5%B0%B1%E4%B8%9A%E7%8F%AD/day47-mybatis/(11)mybatis-day03/%E7%AC%94%E8%AE%B0/img/%E5%9B%BE%E7%89%8710.png?lastModify=1570688006 file://F:/JAVA/java%E5%B0%B1%E4%B8%9A%E7%8F%AD/day47-mybatis/(11)mybatis-day03/%E7%AC%94%E8%AE%B0/img/%E5%9B%BE%E7%89%8711.png?lastModify=1570688006 2.4 一对一查询2.4.1 一对一查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户 file://F:/JAVA/java%E5%B0%B1%E4%B8%9A%E7%8F%AD/day47-mybatis/(11)mybatis-day03/%E7%AC%94%E8%AE%B0/img/%E5%9B%BE%E7%89%8712.png?lastModify=1570688006 2.4.2 一对一查询的语句对应的sql语句: select * from orders;
select * from user where id=查询出订单的uid;查询的结果如下: file://F:/JAVA/java%E5%B0%B1%E4%B8%9A%E7%8F%AD/day47-mybatis/(11)mybatis-day03/%E7%AC%94%E8%AE%B0/img/%E5%9B%BE%E7%89%8713.png?lastModify=1570688006 2.4.3 创建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;
}2.4.4 创建OrderMapper接口public interface OrderMapper {
List<Order> findAll();
}2.4.5 使用注解配置Mapperpublic interface OrderMapper {
@Select("select * from orders")
@Results({
@Result(id=true,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.itheima.mapper.UserMapper.findById"))
})
List<Order> findAll();
}public interface UserMapper {
@Select("select * from user where id=#{id}")
User findById(int id);
}2.4.6 测试结果@Test
public void testSelectOrderAndUser() {
List<Order> all = orderMapper.findAll();
for(Order order : all){
System.out.println(order);
}
}file://F:/JAVA/java%E5%B0%B1%E4%B8%9A%E7%8F%AD/day47-mybatis/(11)mybatis-day03/%E7%AC%94%E8%AE%B0/img/%E5%9B%BE%E7%89%8714.png?lastModify=1570688006 2.5 一对多查询2.5.1 一对多查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单 file://F:/JAVA/java%E5%B0%B1%E4%B8%9A%E7%8F%AD/day47-mybatis/(11)mybatis-day03/%E7%AC%94%E8%AE%B0/img/%E5%9B%BE%E7%89%8715.png?lastModify=1570688006 2.5.2 一对多查询的语句对应的sql语句: select * from user;
select * from orders where uid=查询出用户的id;查询的结果如下: file://F:/JAVA/java%E5%B0%B1%E4%B8%9A%E7%8F%AD/day47-mybatis/(11)mybatis-day03/%E7%AC%94%E8%AE%B0/img/%E5%9B%BE%E7%89%8716.png?lastModify=1570688006 2.5.3 修改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;
}2.5.4 创建UserMapper接口List<User> findAllUserAndOrder();2.5.5 使用注解配置Mapperpublic interface UserMapper {
@Select("select * from user")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "username",column = "username"),
@Result(property = "password",column = "password"),
@Result(property = "birthday",column = "birthday"),
@Result(property = "orderList",column = "id",
javaType = List.class,
many = @Many(select = "com.itheima.mapper.OrderMapper.findByUid"))
})
List<User> findAllUserAndOrder();
}
public interface OrderMapper {
@Select("select * from orders where uid=#{uid}")
List<Order> findByUid(int uid);
}
|