码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis笔记----多表关联查询

时间:2017-04-03 13:58:00      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:查询   print   多表   parameter   .class   tar   art   ges   dtd   

技术分享

 

 

 

 

 

 

 

XML

按照下面类型建立article表

 

Article.java

package com.ij34.model;

public class Article {
  private int articleid;
  private User user;
  private String title;
  private int userid;
  private String content;
  
public int getUserid() {
    return userid;
}
public void setUserid(int userid) {
    this.userid = userid;
}
public int getArticleid() {
    return articleid;
}
public void setId(int id) {
    this.articleid = id;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}
  
}

 

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  <mapper  namespace="com.ij34.mybatis.UserMapper">
  <select id="selectUser" parameterType="int" resultType="com.ij34.model.User">
    select * from users where id=#{id};
  </select>
    <update id="updateUser" keyProperty="id">
    update users set name=#{name},age=#{age} where id=#{id}
  </update>
  
  <insert id="insertUser" >
   insert into users(name,age)values(#{name},#{age})
  </insert>
  <delete id="deleteUser">
    delete from users where name=#{name}
  </delete>

  <resultMap type="com.ij34.model.Article" id="resultAticleList">
  <id property="articleid" column="articleid"/>
  <result property="userid" column="userid"/>
  <result property="title" column="title"/>
  <result property="content" column="content"/>
  <association property="user" javaType="com.ij34.model.User">
  <id property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="age" column="age"/>
  </association>
  </resultMap>
  <select id="selectArticle" parameterType="int" resultMap="resultAticleList">
  select * from users,article
  where users.id=article.userid and users.id=#{id}
  </select>
  </mapper>

 

Test.java

package com.ij34.bean;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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 com.ij34.model.*;
public class Test {
public static void main(String[] args) throws IOException {
    String resource ="com/ij34/mybatis/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   SqlSession session=sqlSessionFactory.openSession();
   try {
//       User user=session.selectOne("com.ij34.mybatis.UserMapper.selectUser", 8);
//       user.setName("小小A");
//       user.setAge(24);
//       session.update("com.ij34.mybatis.UserMapper.updateUser", user);
//       session.commit();
//       System.out.println(user);
//       User user=new User();
//       user.setName("测试01");
//       user.setAge(19);
//       session.insert("com.ij34.mybatis.UserMapper.insertUser", user);
//       session.commit();
//       int user=session.delete("com.ij34.mybatis.UserMapper.deleteUser","测试01");
//       session.commit();
//       System.out.println(user);
       List<Article> articles=session.selectList("com.ij34.mybatis.UserMapper.selectArticle",1);
       for(Article article:articles){
           System.out.println(article.getArticleid()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser());
       }
//       UserMapper mapper=session.getMapper(UserMapper.class); 
//       List<Article> articles=mapper.selectarticle(1);
//       for(Article article:articles){
//           System.out.println(article.getId()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser());
//       }
} finally {
    // TODO: handle finally clause
   session.close();
}
}
}

 

结果

有点问题 article返回id不该是0

技术分享

 

MyBatis笔记----多表关联查询

标签:查询   print   多表   parameter   .class   tar   art   ges   dtd   

原文地址:http://www.cnblogs.com/tk55/p/6661786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!