首页 > 其他分享 >mybatis学习第六部分:Mybatis注解开发

mybatis学习第六部分:Mybatis注解开发

时间:2022-11-19 22:55:06浏览次数:34  
标签:List private public mybatis user Mybatis 注解 id User

6.1   MyBatis的常⽤注解

这⼏年来注解开发越来越流⾏,Mybatis也可以使⽤注解开发⽅式,这样我们就可以减少编写Mapper

映射⽂件了。我们先围绕⼀些基本的CRUD来学习,再学习复杂映射多表操作。

@Insert:实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与@Result    ⼀起使⽤,封装多个结果集

@One:实现⼀对⼀结果集封装

@Many:实现⼀对多结果集封装

 

6.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.lagou.mapper.UserMapper"></mapper>
</mappers>

或者指定扫描包含映射关系的接⼝所在的包也可以

<mappers>
    <!--扫描使⽤注解的类所在的包-->
    <package name="com.lagou.mapper"></package>
</mappers>

6.3 MyBatis的注解实现复杂映射开发

实现复杂关系映射之前我们可以在映射⽂件中通过配置来实现,使⽤注解开发后,我们可以使⽤

@Results注解,@Result注解,@One注解,@Many注解组合完成复杂关系的配置

6.4 ⼀对⼀查询

6.4.1 ⼀对⼀查询的模型

⽤户表和订单表的关系为,⼀个⽤户有多个订单,⼀个订单只从属于⼀个⽤户

⼀对⼀查询的需求:查询⼀个订单,与此同时查询出该订单所属的⽤户

6.4.2 ⼀对⼀查询的语句

对应的sql语句:

select * from orders;

select * from user where id=查询出订单的ID

查询的结果如下:

6.4.3  创建OrderUser实体

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;
}

6.4.4  创建OrderMapper接⼝

public interface OrderMapper {
    List<Order> findAll();
}

6.4.5  使⽤注解配置Mapper

public 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.lagou.mapper.UserMapper.findById"))
})
List<Order> findAll();
}
public interface UserMapper {
@Select("select * from user where id=#{id}")
User findById(int id);
}

6.4.6 测试代码

@Test
public void testSelectOrderAndUser() {
List<Order> all = orderMapper.findAll();
for(Order order : all){
System.out.println(order);
}
}

6.4.7 测试结果

6.5 ⼀对多查询

6.5.1 ⼀对多查询的模型

⽤户表和订单表的关系为,⼀个⽤户有多个订单,⼀个订单只从属于⼀个⽤户

⼀对多查询的需求:查询⼀个⽤户,与此同时查询出该⽤户具有的订单

6.5.2 ⼀对多查询的语句

对应的sql语句:

select * from user;

select * from orders where uid=查询出⽤户的

查询的结果如下:

6.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;
}

6.5.4  创建UserMapper接⼝:

List<User> findAllUserAndOrder();

6.5.5  使⽤注解配置Mapper

public 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.lagou.mapper.OrderMapper.findByUid"))
})
List<User> findAllUserAndOrder();
}
public interface OrderMapper {
@Select("select * from orders where uid=#{uid}")
List<Order> findByUid(int uid);
}

6.5.6 测试代码

List<User> all = userMapper.findAllUserAndOrder(); 
for(User user : all){
System.out.println(user.getUsername()); 
List<Order> orderList = user.getOrderList();

for(Order order : orderList){ System.out.println(order); } System.out.println("-----------------------------"); }

6.5.7测试结果:

 

6.6 多对多查询

6.6.1 多对多查询的模型

⽤户表和⻆⾊表的关系为,⼀个⽤户有多个⻆⾊,⼀个⻆⾊被多个⽤户使⽤ 多对多查询的需求:查询⽤户同时查询出该⽤户的所有⻆⾊

6.6.2 多对多查询的语句

对应的sql语句:

 

select * from user;
select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=⽤户的id

查询的结果如下:

6.6.3  创建Role实体,修改User实体

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;
}

6.6.4  添加UserMapper接⼝⽅法

List<User> findAllUserAndRole();

6.6.5  使⽤注解配置Mapper

public 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 = "roleList",column = "id",
javaType = List.class,
many = @Many(select =
"com.lagou.mapper.RoleMapper.findByUid"))
})
List<User> findAllUserAndRole();}
public interface RoleMapper {
@Select("select * from role r,user_role ur where r.id=ur.role_id and
ur.user_id=#{uid}")
List<Role> findByUid(int uid);
}

6.6.6 测试代码:

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAllUserAndRole();
for(User user : all){
System.out.println(user.getUsername());
List<Role> roleList = user.getRoleList();
for(Role role : roleList){
System.out.println(role);
}
System.out.println("----------------------------------");
}

6.6.7 测试结果

标签:List,private,public,mybatis,user,Mybatis,注解,id,User
From: https://www.cnblogs.com/eagle888/p/16907431.html

相关文章

  • mybatis学习第五部分:Mybatis复杂映射开发
    5.1⼀对⼀查询5.1.1⼀对⼀查询的模型⽤户表和订单表的关系为,⼀个⽤户有多个订单,⼀个订单只从属于⼀个⽤户⼀对⼀查询的需求:查询⼀个订单,与此同时查询出该订单所属的⽤......
  • Java中的自定义注解Annotation
    与注释不同,注解可以被其他程序读取。内置注解:@SuppressWarnings参数:   元注解:用来注解其它注解的注解。1.@Target:使用的位置。包括:TYPE意味着,它能标注"类......
  • mybatis学习第四部分: Mybatis配置⽂件深⼊
    4.1  核⼼配置⽂件SqlMapConfig.xml4.1.1 MyBatis核⼼配置⽂件层级关系   4.2  MyBatis常⽤配置解析1)environments标签数据库环境的配置,⽀持多环境配置......
  • Mybatis出现Caused by: net.sf.jsqlparser.parser.ParseException: ....异常
    今天在开发项目中遇到了一个奇怪的异常,记录一下把!异常信息如下(截取了主要的部分)Causedby:net.sf.jsqlparser.parser.ParseException:Encountered""TOP""top""at......
  • mybatis学习第⼆部分:Mybatis相关概念
    2.1 对象/关系数据库映射(ORM)ORM全称Object/Relation Mapping:表示对象-关系映射的缩写ORM完成⾯向对象的编程语⾔到关系数据库的映射。当ORM框架完成映射后,程序员既可......
  • mybatis学习第三部分:Mybatis基本应⽤
    3.1 快速⼊⻔MyBatis官⽹地址:http://www.mybatis.org/mybatis-3/ 3.1.1 开发步骤:①添加MyBatis的坐标②创建user数据表③编写User实体类④编写映射⽂件UserMappe......
  • mybatis 之 sql语句写法集锦
    1、mapper接口中的方法存在批量数据操作,接口参数是List类型   在mybatis的xml文件中,使用foreach动态标签拼接SQL语句,每一条数据的更新语句对应一条update语句,......
  • mybatis源码解析1
    一、mybatis的简单使用根据mybatis官网提供的例子,需要这么几个步骤1、获取mybatis配置文件的输入流对象2、创建SqlSessionFactoryBuilder对象3、调用SqlSessionFactor......
  • MyBatisPlus 多数据源动态切换
    一、官方文档https://baomidou.com/pages/a61e1b/#%E6%96%87%E6%A1%A3-documentation二、操作步骤1、引入依赖版本号在下面 懒得单独复制了 一起贴出来了<!......
  • MyBatis练习(初)
    目录环境准备库,表idea准备题目一、简单查询环境准备MySQL,JDK17,Maven,MyBatis库,表库:CREATEDATABASEStuDB;学生表:Student(Sno,Sname,Ssex,Sage,Sdept)其中Sno为......