Mybatisのマルチテーブルクエリ、エンティティへのマッピングの適用(1)

1人が複数のコメントを持っている1対多のケースから始めましょう

ID名

p001シャオワン

p002シャオリー

コメント

-------------------------------------------------- ---

idコンテンツpersonid

34時半vp00123
番目のp0021
ハハゴーp001

-------------------------------------------------- --------------------

新しいクラス

package model;

public class Person {
  private String id;
  private String name;
public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Person(){
}
public Person(String _id,String _name){
	super();
	this.id=_id;
	this.name=_name;
}
  
}

 

package model;

public class Comment {
	int id;
	String content;
	String personid;
	Person person;
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getPersonid() {
		return personid;
	}
	public void setPersonid(String personid) {
		this.personid = personid;
	}
	
	public Comment(){
		
	}
	public Comment(int _id,String _content,String _personid){
		super();
		this.id=_id;
		this.content=_content;
		this.personid=_personid;
	}
}


srcの下に新しいcommentMapper.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="younamespace">
<select id="selectComment" parameterType="int" resultMap="commentResult">
select 
a.id        as   comtid,
a.content   as   comtContent  ,
b.id        as   personID,
b.name      as   personName
from comment a 
LEFT JOIN person b on a.personid=b.id 
where a.id=#{id}
</select>
<resultMap type="model.Comment" id="commentResult">
   <id property="id" column="comtid"/>
   <result property="content" column="comtContent"/>
   <association property="person" javaType="model.Person" resultMap="personResult"/> 
</resultMap>
<resultMap type="model.Person" id="personResult">
    <id property="id" column="personID"/>    
    <result property="name" column="personName"/>
</resultMap>
</mapper>


srcの下に新しいmybatis-config.xmlを作成します

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/test" />
				<property name="username" value="root" />
				<property name="password" value="1234" />
			</dataSource>
		</environment>
	</environments>
	<mappers>

		<mapper resource="commentMapper.xml"/>
	</mappers>
</configuration>


Javaコード:

 String resource = "mybatis-config.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()。build(inputStream); //ファクトリクラスはシングルトンモードを使用します

 SqlSessionセッション= sqlSessionFactory.openSession();
  {   コメントp =(コメント)session.selectOne( "younamespace.selectComment"、1);を試してください   System.out.print(p.getContent()+ "来自" + p.getPerson()。getName());   }最後に{   session.close();   }




結果:

ハハはシャオワンから行く

 


 

おすすめ

転載: blog.csdn.net/penkee/article/details/8574503