您现在的位置是:首页 > 个人日记个人日记
mybatis-07-单表一对多查询
2019-02-03 18:14:55【个人日记】617人已围观
简介一张表对应多个数据
单表一对多查询
文件结构
配置User.java
文件
import java.util.List;
public class User {
private Integer id;
private String userName;
private String userTelephone;
private String userSex;
//这里封装多个信息 *注意*
"private List<Orders> orders;"
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", userTelephone='" + userTelephone + '\'' +
", userSex='" + userSex + '\'' +
", orders=" + orders +
'}';
}
public List<Orders> getOrders() {
return orders;
}
public void setOrders(List<Orders> orders) {
this.orders = orders;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserTelephone() {
return userTelephone;
}
public void setUserTelephone(String userTelephone) {
this.userTelephone = userTelephone;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
}
另一个Orders.java
信息
import java.util.Date;
public class Orders {
private Integer id;
private String userId;
private Date createTime;
@Override
public String toString() {
return "Orders{" +
"id=" + id +
", userId='" + userId + '\'' +
", createTime=" + createTime +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
Orders接口
注意
如果封装另一张表,需要使用List来放入信息
import java.util.List;
public interface OrderMapper {
/**
* 通过id查询
*
* @param orderId
* @return
*/
List<Orders> orders(Integer orderId);
}
User接口
package com.huangxin.order.mapper;
import com.huangxin.order.model.User;
public interface UserMapper {
/**
* 通过id查询
*
* @param id
* @return
*/
User getById(Integer id);
}
配置OrderMapper.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.huangxin.order.mapper.OrderMapper">
<resultMap id="order" type="com.huangxin.order.model.Orders">
<id column="oid" property="id"></id>
<result column="user_id" property="userId"></result>
<result column="create_time" property="createTime"></result>
</resultMap>
<select id="orders" parameterType="java.lang.Integer" resultMap="order">
SELECT * FROM orders WHERE user_id=#{orderId};
</select>
</mapper>
注意命名
重点
配置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.huangxin.order.mapper.UserMapper">
<resultMap id="user" type="com.huangxin.order.model.User">
<id column="id" property="id"></id>
<result column="user_name" property="userName"></result>
<result column="user_telephone" property="userTelephone"></result>
<result column="user_sex" property="userSex"></result>
<collection property="orders" select="com.huangxin.order.mapper.OrderMapper.orders" column="id">
</collection>
</resultMap>
<select id="getById" parameterType="java.lang.Integer" resultMap="user">
SELECT * FROM user WHERE id=#{id};
</select>
</mapper>
注意
1) 注意看collection便签中的column属性,这里需要与SQL语句中查询的列明相对应, 可以忽略大小写,但是必须要相同 ,像上面的column=" id "与SELECT * FROM user WHERE id =#{id},这两个必须相同,这样才会查询到与id相同的信息
2) select="com.huangxin.order.mapper.OrderMapper.orders"这句是另一张对应表的映射信息, com.huangxin.order.mapper.OrderMapper 为namespace, orders 则为id命名
最后是测试
package com.huangxin.order.mapper;
import com.huangxin.order.model.Orders;
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 org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class OrderMapperTest {
private SqlSessionFactory factory;
/**
* 初始化SqlSession工厂
*/
@Before
public void init() throws IOException {
//将全局配置文件读取
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void getById() {
SqlSession sqlSession = factory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.getById(1));
sqlSession.close();
}
}
控制台输出为
D:\Java\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\IntelliJ\lib\idea_rt.jar=4751:D:\IntelliJ\bin -Dfile.encoding=UTF-8 -classpath D:\IntelliJ\lib\idea_rt.jar;D:\IntelliJ\plugins\junit\lib\junit-rt.jar;D:\IntelliJ\plugins\junit\lib\junit5-rt.jar;D:\mybatis05\target\test-classes;D:\mybatis05\target\classes;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\Administrator\.m2\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.huangxin.order.mapper.OrderMapperTest,getById
2019-01-02 21:57:10 [DEBUG] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/C:/Users/Administrator/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar) to method java.lang.Class.checkPackageAccess(java.lang.SecurityManager,java.lang.ClassLoader,boolean)
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2019-01-02 21:57:10 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-02 21:57:10 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-02 21:57:10 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-02 21:57:10 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-02 21:57:10 [DEBUG] Opening JDBC Connection
2019-01-02 21:57:11 [DEBUG] Created connection 37981645.
2019-01-02 21:57:11 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
2019-01-02 21:57:11 [DEBUG] > Preparing: SELECT * FROM user WHERE user_sex=?;
2019-01-02 21:57:11 [DEBUG] > Parameters: 1(Integer)
2019-01-02 21:57:11 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-02 21:57:11 [DEBUG] > Parameters: 1(Integer)
2019-01-02 21:57:11 [DEBUG] < Total: 2
2019-01-02 21:57:11 [DEBUG] < Total: 1
User{id=7, userName='1', userTelephone='1', userSex='1', orders=[Orders{id=1, userId='1', createTime=Tue Jan 01 19:49:58 CST 2019}, Orders{id=2, userId='1', createTime=Tue Jan 01 19:50:46 CST 2019}]}
2019-01-02 21:57:11 [DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
2019-01-02 21:57:11 [DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
2019-01-02 21:57:11 [DEBUG] Returned connection 37981645 to pool.
Process finished with exit code 0
可以看出User对象中封装这Orders信息,反映出一对多的信息
Tags: JavaWeb
上一篇: xml配置mybatis映射注意事项
相关文章
随机图文
评论区
2024-10-03 18:54:07
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信