javabean: mapper.xml 代碼 圖文解釋: 測試: 結果: 22:47:17.005 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.iba ...
javabean:
package com.me.model; import java.io.Serializable; import java.util.Date; import java.util.List; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String username; private Date birthday; private String sex; private String address; //一對一 放入對象 private Morder morder; //一對多 放入對象集合 private List<Home> homeList; public List<Home> getHomeList() { return homeList; } public void setHomeList(List<Home> homeList) { this.homeList = homeList; } public Morder getMorder() { return morder; } public void setMorder(Morder morder) { this.morder = morder; } public static long getSerialversionuid() { return serialVersionUID; } 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address=" + address + ", morder=" + morder + ", homeList=" + homeList + "]"; } }
package com.me.model; public class Morder { private int orderId; private String orderName; private String orderMessage; public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } public String getOrderMessage() { return orderMessage; } public void setOrderMessage(String orderMessage) { this.orderMessage = orderMessage; } }
package com.me.model; public class Home { private int homeId; private String homeName; public int getHomeId() { return homeId; } public void setHomeId(int homeId) { this.homeId = homeId; } public String getHomeName() { return homeName; } public void setHomeName(String homeName) { this.homeName = homeName; } }
mapper.xml 代碼
<!-- collection :collection屬性的值有三個分別是list、array、map三種, 分別對應的參數類型為:List、數組、map集合,我在上面傳的參數為數組,所以值為array item : 表示在迭代過程中每一個元素的別名 index :表示在迭代過程中每次迭代到的位置(下標) open :首碼 close :尾碼 separator :分隔符,表示迭代時每個元素之間以什麼分隔 --> <delete id="deleteSome"> delete from user where id in <foreach collection="list" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </delete> <!-- 關聯查詢 --> <!-- 關聯查詢 1對1 --> <select id="selectGL" resultMap="userRsultMap"> select * from user u,morder m WHERE u.oid=m.order_id </select> <resultMap type="com.me.model.User" id="userRsultMap"> <id property="id" column="id" /> <result column="username" property="username" /> <result column="birthday" property="birthday" /> <result column="sex" property="sex" /> <result column="address" property="address" /> <association property="morder" javaType="com.me.model.Morder"> <id column="order_id" property="orderId" /> <result column="order_name" property="orderName" /> <result column="order_message" property="orderMessage" /> </association> </resultMap> <!-- 關聯查詢 1對多 --> <select id="selectGL2" resultMap="userRsultMap2"> select * from user u,home h where u.hid=h.home_id; </select> <resultMap type="com.me.model.User" id="userRsultMap2"> <id property="id" column="id" /> <result column="username" property="username" /> <result column="birthday" property="birthday" /> <result column="sex" property="sex" /> <result column="address" property="address" /> <collection property="homeList" ofType="com.me.model.Home"> <id property="homeId" column="home_id" /> <result property="homeName" column="home_name" /> </collection> </resultMap>
圖文解釋:
測試:
//關聯查詢 1 to 多 @Test public void selectGL2(){ try { inputStream = Resources.getResourceAsStream(resource); // 創建會話工廠,傳入MyBatis的配置文件信息 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); // 通過工廠得到SqlSession sqlSession = sqlSessionFactory.openSession(); List<User> list = sqlSession.selectList("test.selectGL2"); for (User u : list) { System.err.println(u.getHomeList().get(0).getHomeName()); } } catch (IOException e) { e.printStackTrace(); } finally { // 釋放資源 sqlSession.close(); } }
結果:
22:47:17.005 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.215 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
22:47:17.420 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 518522822.
22:47:17.420 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.421 [main] DEBUG test.selectGL2 - ==> Preparing: select * from user u,home h where u.hid=h.home_id;
22:47:17.444 [main] DEBUG test.selectGL2 - ==> Parameters:
22:47:17.461 [main] DEBUG test.selectGL2 - <== Total: 4
sasadasd
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.463 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 518522822 to pool.
更多可以參考:https://www.cnblogs.com/xdp-gacl/p/4264440.html