Code:
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" > <!--XML configuration file contains the core settings for the MyBatis system-<configuration> <properties resource= "db.properties"/> <!--means Specific implementation of the log used by MyBatis-<settings> <setting name= "Logimpl" value= "log4j"/> <!--to make the deferred load effective, the following two properties must be configured --<setting name= "lazyloadingenabled" value= "true"/> <setting name= "aggressivelazyloading" value= "false "/> </settings> <environments default=" MySQL "> <!--Environment configuration, which is the connected database. --<environment id= "MySQL" > <!--Specifies the transaction management type, type= "jdbc" refers to the direct simple use of the JDBC Commit and rollback settings and <TRANSACTI Onmanager type= "jdbc"/> <!--datasource refers to the data source configuration, pooled is the implementation of the data source connection pool for the JDBC connection object. --<datasource type= "Pooled" > <property name= "Driver" value= "${driver}"/> <proper Ty name= "url" value= "${url}"/> <propErty name= "username" value= "${username}"/> <property name= "password" value= "${password}"/> </dat Asource> </environment> </environments> <!--Mappers told MyBatis where to find the mapping file for the persistence class--<mappers > <mapper class= "com.mapper.UserMapper"/> <mapper class= "Com.mapper.OrderMapper"/> <mapper cl ass= "Com.mapper.ArticleMapper"/> </mappers> </configuration>
Article.java
Package Com.bean;
Import java.io.Serializable;
Import java.util.List; Public class article implements Serializable {private Integer ID; Commodity ID, primary key private String name; Commodity name private Double price; Commodity price private String remark;
Commodity Description//goods and orders are many-to-many relationships, where a commodity can be included in multiple orders by private list<order> orders;
Public article () {super ();
TODO auto-generated Constructor stub} public article (string name, Double price, string remark) {super ();
THIS.name = name;
This.price = Price;
This.remark = remark;
} public Integer GetId () {return id;
} public void SetId (Integer id) {this.id = ID;
} public String GetName () {return name;
} public void SetName (String name) {this.name = name;
} public Double GetPrice () {return price;
public void Setprice (Double price) {this.price = Price;
} public String Getremark () {return remark;
} public void Setremark (String remark) {This.remark = remark; } public list<order> GetordERs () {return orders;
} public void Setorders (list<order> orders) {this.orders = orders; } @Override Public String toString () {return ' article [id= ' + ID + ', name= "+ name +", price= "+ price +", remark
= "+ remark +"] ";
}
}
Order.java
Package Com.bean;
Import java.io.Serializable;
Import java.util.List; public class Order implements Serializable {private Integer ID; Order ID, primary key private String code; Order number private Double total;
Order Total amount//order and user is a many-to-one relationship, that is, multiple orders belong to only one user private users user;
Orders and commodities are many-to-many relationships, i.e. an order can contain multiple items private list<article> articles;
Public Order () {super ();
TODO auto-generated Constructor stub} public Order (String code, Double total) {super ();
This.code = code;
This.total = total;
} public Integer GetId () {return id;
} public void SetId (Integer id) {this.id = ID;
} public String GetCode () {return code;
} public void Setcode (String code) {this.code = code;
} public Double Gettotal () {return total;
public void Settotal (Double total) {this.total = total;
Public user GetUser () {return user;
public void SetUser (user user) {this.user = user;
} public list<article> Getarticles () {return articles; } PUBlic void Setarticles (list<article> articles) {this.articles = articles;
} @Override Public String toString () {return ' Order [id= + ID + ', code= "+ code +", total= "+ Total +"] ";
}
}
User.java
Package Com.bean;
Import java.io.Serializable;
Import java.util.List; public class User implements serializable{private Integer ID; User ID, primary key private String username; User name private String loginname; Logon name private String password; Password private String phone; Contact phone private String address;
Shipping address//user and Order is a one-to-many relationship, that is, a user can have multiple orders private list<order> orders;
Public User () {super (); TODO auto-generated Constructor stub} public User (string username, string loginname, string password, string phone,
String address) {super ();
This.username = Username;
This.loginname = LoginName;
This.password = password;
This.phone = phone;
this.address = address;
} 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 Getloginname () {return loginname; } public VoiD setloginname (String loginname) {this.loginname = LoginName;
} public String GetPassword () {return password;
} public void SetPassword (String password) {this.password = password;
} public String Getphone () {return phone;
} public void Setphone (String phone) {this.phone = phone;
} public String getaddress () {return address;
The public void setaddress (String address) {this.address = address;
} public list<order> getorders () {return orders;
} public void Setorders (list<order> orders) {this.orders = orders; } @Override Public String toString () {return "User [id=" + ID + ", username=" + Username + ", loginname=" + Loginnam
E + ", password=" + password + ", phone=" + Phone + ", address=" + address + "]";
}
}
Fksqlsessionfactory.java
Package com.factory;
Import Java.io.InputStream;
Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import org.apache.ibatis.session.SqlSessionFactory;
Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Fksqlsessionfactory {
private static sqlsessionfactory sqlsessionfactory = null;
Initialize Create Sqlsessionfactory object
static{
try {
//Read Mybatis-config.xml file
inputstream InputStream = Resources.getresourceasstream ("Mybatis-config.xml");
Sqlsessionfactory = new Sqlsessionfactorybuilder ()
. Build (InputStream);
} catch (Exception e) {
E.printstacktrace ();
}
}
Gets the static method of the Sqlsession object public
static Sqlsession getsqlsession () {
return sqlsessionfactory.opensession ();
}
//Get static method of Sqlsessionfactory public
static Sqlsessionfactory getsqlsessionfactory () {
return sqlsessionfactory;
}
}
Articlemapper.xml
Package com.mapper;
Import java.util.List;
Import Org.apache.ibatis.annotations.Select;
Import com.bean.Article;
Import Com.bean.User;
Public interface Articlemapper {
@Select ("select * from Tb_article where ID in (select article_id from Tb_item where O rder_id = #{id}) ")
list<article> Selectbyorderid (Integer order_id);
}
Ordermapper.xml
Package com.mapper;
Import Org.apache.ibatis.annotations.Many;
Import Org.apache.ibatis.annotations.One;
Import Org.apache.ibatis.annotations.Result;
Import Org.apache.ibatis.annotations.Results;
Import Org.apache.ibatis.annotations.Select;
Import Org.apache.ibatis.mapping.FetchType;
Import Com.bean.Order;
Public interface Ordermapper {
@Select ("select * from Tb_order where id = #{id}")
@Results ({
@Result (id= true,column= "id", property= "id"),
@Result (column= "code", property= "code"),
@Result (column= "Total", Property= "Total"),
@Result (column= "user_id", property= "user",
one= @One (select= " Com.mapper.UserMapper.selectById ", Fetchtype=fetchtype.eager)),
@Result (column=" id ", property=" articles ", many= @Many (select= "Com.mapper.ArticleMapper.selectByOrderId", Fetchtype=fetchtype.lazy)
})
Order Selectbyid (Integer ID);
}
Usermapper.xml
Package com.mapper;
Import Org.apache.ibatis.annotations.Select;
Import Com.bean.User;
Public interface Usermapper {
@Select ("select * from tb_user where id = #{id}")
user Selectbyid (Integer ID);
}
Manytomanytest.java
Package com.test;
Import org.apache.ibatis.session.SqlSession;
Import Com.bean.Order;
Import com.factory.FKSqlSessionFactory;
Import Com.mapper.OrderMapper;
public class Manytomanytest {public
static void Main (string[] args) {
//TODO auto-generated method stub
Sqls ession session = Fksqlsessionfactory.getsqlsession ();
Ordermapper om = session.getmapper (ordermapper.class);
Order order =om.selectbyid (1);
View the query to the Order object
System.out.println (Order.getid () + "" + order.getcode () + "" + order.gettotal ());
View the User Object
System.out.println (Order.getuser ()) of the Order association;
View the associated article collection because the configuration uses lazy lazy loading, so the SQL statement order.getarticles () is executed when it is used
. ForEach (Article- SYSTEM.OUT.PRINTLN (article));
Session.commit ();
Session.close ();
}
}
Screenshots: