新建实体类 Order:
@Data@NoArgsConstructor@AllArgsConstructor@Table(name = "orders")public class Order {@Idprivate Integer orderId;private Integer userId;private String receiverName;private String receiverMobile;private String receiverAddress;}
新建 dao 接口:
注意,此处 dao 接口继承的是自定义的通用接口模板,相关说明参见之前创建示例 UserDao 的步骤 。
也可以直接继承 tkMapper 的两个接口 。(注意灵活运用!)
public interface OrderDao extends GeneralDao<Order> {}
说明:进行关联/多表查询前,需要修改下之前的 User 实体类,在实体类中需要添加一个订单的字段,以便查询出用户所关联的订单信息 。@Data@NoArgsConstructor@AllArgsConstructor@Table(name = "users") //数据库表名和实体类类名不一致需要指定映射关系!public class User {@Id //指定主键private Integer userId;private String userName;private String userPwd;private String userRealname;private String userImg;//订单private List<Order> orderList;}
方式一:多次单表查询@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringbootTkMapperDemoApplication.class) //启动类.classpublic class UserDaoTest {@Autowiredprivate UserDao userDao; //如果爆红线不用管(或Dao接口上添加@Repository注解)@Autowiredprivate OrderDao orderDao;@Testpublic void test() {//根据用户名查询用户信息Example example = new Example(User.class);Example.Criteria criteria = example.createCriteria();criteria.andEqualTo("userName", "luis");//条件查询List<User> users = userDao.selectByExample(example);User user = users.get(0);//根据用户id查询订单信息Example example1 = new Example(Order.class);Example.Criteria criteria1 = example.createCriteria();criteria.andEqualTo("userId", user.getUserId());//条件查询List<Order> orders = orderDao.selectByExample(example1);//将查询到的订单信息设置到user中user.setOrderList(orders);System.out.println("========> " + user);}}
方式二:自定义连接查询- UserDao 接口中新建查询方法
public interface UserDao extends GeneralDao<User> {public User selectByUserName(String userName);}
- mappers 目录下创建对应的 UserMapper.xml 文件,自定义查询 SQL
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.luis.dao.UserDao"><resultMap id="userMap" type="com.luis.beans.User"><id column="user_id" property="userId"/><result column="user_name" property="userName"/><result column="user_pwd" property="userPwd"/><result column="user_realname" property="userRealname"/><result column="user_img" property="userImg"/><collection property="orderList" ofType="com.luis.beans.Order"><id column="order_id" property="orderId"/><result column="user_id" property="userId"/><result column="receiver_name" property="receiverName"/><result column="receiver_mobile" property="receiverMobile"/><result column="receiver_address" property="receiverAddress"/></collection></resultMap><select id="selectByUserName" resultMap="userMap">select u.user_id,u.user_name,u.user_pwd,u.user_realname,u.user_img,o.order_id,o.user_id,o.receiver_name,o.receiver_mobile,o.receiver_addressfrom users u inner join orders oon u.user_id = o.user_id;</select></mapper>
- 测试
【Spring Boot 中使用 tkMapper】@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringbootTkMapperDemoApplication.class) //启动类.classpublic class UserDaoTest {@Autowiredprivate UserDao userDao; //如果爆红线不用管(或Dao接口上添加@Repository注解)@Autowiredprivate OrderDao orderDao;@Testpublic void test02() {//使用自定义的查询方法User user = userDao.selectByUserName("luis");System.out.println("========> " + user);}}
经验总结扩展阅读
- 2023全国统一中考时间 2023年全国中考时间是6月几号
- 七 SpringCloud - 微信支付
- 2023中秋国庆放假调休哪两天
- 2023中秋国庆怎么调休补班
- 2023中秋国庆加班怎么算加班费
- 2023年中秋国庆哪一天适合结婚
- 2023年10月8日补哪天的班
- 2023年10月28日开业好吗 日子吉利吗
- 中国的疆域
- 卧室里小飞虫哪来的