Mybatis表关联多对一
创建表
创建表对应的 JavaBean 对象
package com.tanlei.newer.pojo; import java.io.Serializable; import java.util.List; /** * @author:Mr.Tan * @Create:2018-11-05-10-55 **/ public class Person implements Serializable { private int id; private String username; private String mobile; private List<Post> posts; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public List<Post> getPosts() { return posts; } public void setPosts(List<Post> posts) { this.posts = posts; } @Override public String toString() { return "Person{" + "id=" + id + ", username='" + username + '\'' + ", mobile='" + mobile + '\'' + ", posts=" + posts + '}'; } }
package com.tanlei.newer.pojo; import java.io.Serializable; /** * @author:Mr.Tan * @Create:2018-11-05-10-55 **/ public class Post implements Serializable { private int id; private Person person; private String title; private String content; public int getId() { return id; } public void setId(int id) { this.id = id; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } 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; } }
配置文件
<?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> <typeAliases> <!--<typeAlias alias="User" type="com.tanlei.newer.pojo.User"></typeAlias>--> <typeAlias alias="Person" type="com.tanlei.newer.pojo.Person"></typeAlias> <typeAlias alias="Post" type="com.tanlei.newer.pojo.Post"></typeAlias> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"></property> <property name="username" value="root"></property> <property name="password" value="password"></property> </dataSource> </environment> </environments> <mappers> <!-- <mapper resource="com/tanlei/newer/pojo/User.xml"></mapper>--> <mapper resource="com/tanlei/newer/pojo/Person.xml"></mapper> </mappers> </configuration>
映射文件Person.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.tanlei.newer.pojo.IPerson"> <resultMap type="Post" id="resultPostMap" > <result property="id" column="post_id" ></result> <result property="title" column="title" ></result> <result property="content" column="content"></result> <association property="person" javaType="Person"> <result property="username" column="username"/> <result property="mobile" column="mobile"/> </association> </resultMap> <select id="getPosts" resultMap="resultPostsMap" parameterType="int"> SELECT pp.*,p.* FROM person pp, post p WHERE pp.id=p.personid AND p.post_id=#{id} </select> </mapper>
测试程序运行 main类
package com.tanlei.newer.test; import com.tanlei.newer.pojo.Person; import com.tanlei.newer.pojo.Post; 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; /** * @author:Mr.Tan * @Create:2018-11-05-13-52 **/ public class PersonMain { public static Reader reader; public static SqlSessionFactory sqlSessionFactory; static { try { reader= Resources.getResourceAsReader("config/Configure.xml"); sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } public static SqlSessionFactory getSession(){ return sqlSessionFactory; } public static void main(String[] args) { SqlSession session = sqlSessionFactory.openSession(); int postId = 1; Post post = session.selectOne("com.tanlei.newer.pojo.IPerson.getPosts", postId); System.out.println("title: "+post.getTitle()); System.out.println("userName: "+post.getPerson().getUsername()); session.close(); } }
输出结果如下: