Mybitis (Ibitis) series of essays: mybitis instance of getting StartedMybitis (Ibitis) series two essays: type Alias (typealiases) and table-Object Mapping (ORM)Mybitis (Ibitis) series of essay Three: simple implementation of CRUDMybitis (Ibitis) series of essays four: Multi-table (many-to-one query operation)Mybitis (Ibitis) series of essays Five: multiple tables (one-to-many association queries)Mybitis (Ibitis) series of essays Six: Mybitis and spring Integration
This blog post describes a one-to-many-table association query in multiple tables
or in accordance with the previous step, do some preparatory work; Create two tables: One is the user, one is the corresponding mobile phone, one user can have a mobile phone.
This is the User T_user table
This is the mobile phone t_mobile watch.
In the Java entity object pair, a one-to-many can be implemented according to list and set, both in the Mybitis with the collection tag to use, and later will do a detailed configuration introduction
Create a JavaBean object for a table
Mobile Bean
[Java]View PlainCopy
- Public class Mobile {
- private int id;
- private String Telnumber;
- public int getId () {
- return ID;
- }
- public void setId (int id) {
- this.id = ID;
- }
- Public String Gettelnumber () {
- return telnumber;
- }
- public void Settelnumber (String telnumber) {
- this.telnumber = Telnumber;
- }
- }
User Bean
[Java]View PlainCopy
- Package Com.jefry;
- Import java.util.List;
- Public class User {
- private int id;
- private String UserName;
- private String password;
- private list<mobile> mobiles; //This can also be a set set
- Public list<mobile> Getmobiles () {
- return mobiles;
- }
- public void Setmobiles (list<mobile> mobiles) {
- this.mobiles = mobiles;
- }
- Public String GetUserName () {
- return userName;
- }
- public void Setusername (String userName) {
- this.username = userName;
- }
- Public String GetPassword () {
- return password;
- }
- public void SetPassword (String password) {
- This.password = password;
- }
- public int getId () {
- return ID;
- }
- public void setId (int id) {
- this.id = ID;
- }
- }
Overwrite the mapping file on the basis of the previous article:
[HTML]View PlainCopy
- <? 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="user">
- <resultmap id="Userresultmap" type="User">
- <ID property="id" column="id" javatype="int" jdbctype="INTEGER" />
- <result < span class= "attribute" >property= "userName" column=< Span class= "Attribute-value" > "name" javatype= "string " jdbctype=" VARCHAR "/>
- < Result property= "password" column= "pass" javatype=< Span class= "Attribute-value" > "string" jdbctype= " VARCHAR "/>
- <collection property= "mobiles" Column= "userid" oftype= "Mobile" >
- < id property= "id" column= "id" javatype=" int " jdbctype=" INTEGER " />
- <result property="Telnumber" column="Telnumber" javatype="string" jdbctype= "VARCHAR"/>
- </Collection>
- </resultmap>
- <!--Multi-table query operation--
- <Select id= "selectuser" parametertype="int" resultmap="Userresultmap" >
- <!--assign aliases to the primary key ID of the mobile and the user's primary key ID, and avoid injecting into the corresponding object name conflict because two table field names are the same--
- Select M.id m_id,m.telnumber,u.id u_id,u.name,u.pass from T_mobile m,t_user u where m.userid = u.id and u.id = #{id}
- </Select>
- </mapper>
Finally, pass the test OK
[Java]View PlainCopy
- Public class Test {
- static String resource = "Mybatis-config.xml";
- public static void Main (string[] args) throws IOException {
- InputStream InputStream = resources.getresourceasstream (Resource);
- Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
- sqlsession session = Sqlsessionfactory.opensession ();
- try {
- User user = Session.selectone ("User.selectuser", 1);
- list<mobile> mobiles = User.getmobiles ();
- For (Mobile mobile:mobiles) {
- System.out.println ("User:" + user.getusername () + ", tel:" + mobile.gettelnumber ());
- }
- } finally {
- Session.close ();
- }
- }
- }
SOURCE Download: http://download.csdn.net/detail/jefry_xdz/5229451
http://blog.csdn.net/jefry_xdz/article/details/8767358
Http://www.cnblogs.com/yinkh/p/5564033.html
Mybitis (Ibitis) series of essays Five: multiple tables (one-to-many association queries)