MyBatis笔记----多表关联查询
Posted Stay Hungry, Stay Foolish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis笔记----多表关联查询相关的知识,希望对你有一定的参考价值。
数据库
方式一:XML
按照下面类型建立article表
Article.java
package com.ij34.model; public class Article { private int id; private User user; private String title; private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getId() { return id; } public void setId(int id) { this.id = 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; } }
UserMapper.xml
颜色aid重点标出部分很重要,必须使用别名,不要最后结果获得article的id全是输入的id
<?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="Article" id="resultAticleList"> <id property="id" column="aid"/> <result property="title" column="title"/> <result property="content" column="content"/> <association property="user" javaType="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 users.id,users.name,users.age,article.id aid,article.title,article.content 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 { List<Article> articles=session.selectList("com.ij34.mybatis.UserMapper.selectarticle",1); for(Article article:articles){ System.out.println(article.getId()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser()); } } finally { // TODO: handle finally clause session.close(); } } }
结果
方式二:通过接口
结构
xml
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> <typeAliases> <typeAlias type="com.ij34.model.User" alias="User"/> <typeAlias type="com.ij34.model.Article" alias="Article"/> </typeAliases> <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/mybatis?useSSL=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/ij34/mybatis/UserMapper.xml"/> <!-- <mapper class="com.ij34.model.UserMapper"/> --> </mappers> </configuration>
UserMapper.xml
<mapper namespace="com.ij34.model.UserMapper">
对应于接口UserMapper
<?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.model.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="Article" id="resultAticleList"> <id property="id" column="aid"/> <result property="title" column="title"/> <result property="content" column="content"/> <association property="user" javaType="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 users.id,users.name,users.age,article.id aid,article.title,article.content from users,article where users.id=article.userid and users.id=#{id} </select> </mapper>
model
User.java
package com.ij34.model; public class User { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
Article.java
package com.ij34.model; public class Article { private int id; private User user; private String title; private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getId() { return id; } public void setId(int id) { this.id = 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; } }
UserMapper.java
package com.ij34.model; import java.util.List; public interface UserMapper { /*// @Select("select * from User where id=#{id}") public User selectUser(int id); // @Update("update User set name=#{name},age=#{age} where id=#{id}") public void updateUser(User user); */ public List<Article> selectarticle(int id); }
测试
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.Article; import com.ij34.model.UserMapper; 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 { 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(); } } }
结果
以上是关于MyBatis笔记----多表关联查询的主要内容,如果未能解决你的问题,请参考以下文章
Java--Mybatis关联查询,多表同名字段导致SQL报错