博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis实战教程二:多对一关联查询(一对多)
阅读量:4958 次
发布时间:2019-06-12

本文共 6239 字,大约阅读时间需要 20 分钟。

多对一关联查询

一、数据库关系。article表和user表示多对一的关系

CREATE TABLE `article` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `userid` int(11) NOT NULL,  `title` varchar(100) NOT NULL,  `content` text NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
CREATE TABLE `user` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `userName` varchar(50) DEFAULT NULL,  `gender` int(11) DEFAULT NULL,  `hobby` varchar(255) DEFAULT NULL,  `member` int(11) DEFAULT NULL,  `userAge` int(11) DEFAULT NULL,  `userAddress` varchar(200) DEFAULT NULL,  `reg_time` varchar(200) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

 二、创建跟表对应关系的model

public class Article {    private int id;    private User user;    private String title;    private String content;    setter()... getter()...}
public class User {    private int id;    private String userName;    private String userAge;    private Gender gender;    private Hobby hobby;    private Member member;    private String userAddress;    private Date regTime;       setters()&getters()}

 

 三、新建对应的mapper映射器组合

 

public interface ArticleMapper {    public List
getUserArticles();}

 

  组合一:

      
    
    

 

  组合二:

  组合三:

 

 

四、在configuration.xml 配置别名和注册映射器:

 

五、测试 

 

public class ArticleTest extends BaseTest{    public static void main(String[] args) {        selectArticlesWithUser();    }    public static void selectArticlesWithUser() {        SqlSessionFactory sessionFactory =getSession();        SqlSession session = sessionFactory.openSession();        ArticleMapper mapper = session.getMapper(ArticleMapper.class);        List
list = mapper.getUserArticles(); for(Article a : list) { System.out.println(a.getTitle() + "," + a.getUser().getUserName() + "," + a.getUser().getGender()); } }}

一对多关联查询 

一、数据库关系。user表和article表示一对多的关系

二、创建跟表对应关系的model

public class User {    private int id;    private String userName;    private String userAge;    private Gender gender;    private Hobby hobby;    private Member member;    private String userAddress;    private Date regTime;    private List
articleList; setters()&getters()}

 

 三、新建对应的mapper映射器组合

@Repositorypublic interface UserMapper {        public List
getUserArticlesById(Long id); }

 

 组合一:

 

组合二:这种方法适合SQL类似 SELECT * FROM table_name,就是没有where条件的情况

 

Article.xml

 

 

四、测试

import com.yihaomen.mybatis.dao.UserMapper;import com.yihaomen.mybatis.model.Article;import com.yihaomen.mybatis.model.User;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.Reader;import java.util.List;public class TestGetUser {    private static SqlSessionFactory sqlSessionFactory;    private static Reader reader;    static {        try {            reader = Resources.getResourceAsReader("configuration.xml");            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);        } catch (IOException e) {            e.printStackTrace();        }    }    public static SqlSessionFactory getSession() {        return sqlSessionFactory;    }    public static void getUserArticlesById(Long id) {        SqlSession session = sqlSessionFactory.openSession();        UserMapper userMapper = session.getMapper(UserMapper.class);        List
users = userMapper.getUserArticlesById(id); for(User u : users) { System.out.println("------------------"); System.out.println(u.getUserName()); for(Article a : u.getArticleList()) { System.out.println(a.getTitle() + "," + a.getContent()); } } } public static void main(String[] args) { getUserArticlesById(11L); }}

 

 

 参考:

 [1] 《mybati实战教程》

 [2] CSDN博客,

转载于:https://www.cnblogs.com/happyflyingpig/p/7695837.html

你可能感兴趣的文章
java语法之final
查看>>
python 多进程和多线程的区别
查看>>
sigar
查看>>
iOS7自定义statusbar和navigationbar的若干问题
查看>>
[Locked] Wiggle Sort
查看>>
deque
查看>>
Setting up a Passive FTP Server in Windows Azure VM(ReplyCode: 227, Entering Passive Mode )
查看>>
Python模块调用
查看>>
委托的调用
查看>>
c#中从string数组转换到int数组
查看>>
数据模型(LP32 ILP32 LP64 LLP64 ILP64 )
查看>>
java小技巧
查看>>
POJ 3204 Ikki's Story I - Road Reconstruction
查看>>
【BZOJ】2959: 长跑(lct+缩点)(暂时弃坑)
查看>>
iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
查看>>
toad for oracle中文显示乱码
查看>>
SQL中Group By的使用
查看>>
错误org/aopalliance/intercept/MethodInterceptor解决方法
查看>>
Pylint在项目中的使用
查看>>
使用nginx做反向代理和负载均衡效果图
查看>>