A simple example.
In this example, two business objects are: employer and department. The relationship between them is n <--> 1.
Employer. Java
Package COM. mymaven. mybatisdemo. po; public class employer {private string em_id; private string name; private string station; private string work_years; private string create_date; private department DPT; // Department Public Department getdpt () {return DPT;} public void setdpt (Department DPT) {This. DPT = DPT;} Public String getem_id () {return em_id;} public void setem_id (string em_id) {This. em_id = em_id;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String getstation () {return station;} public void setstation (string station) {This. station = station;} Public String getwork_years () {return work_years;} public void setwork_years (string work_years) {This. work_years = work_years;} Public String getcreate_date () {return create_date;} public void setcreate_date (string create_date) {This. create_date = create_date;} public employer (){}}
Department. Java
package com.mymaven.mybatisdemo.po;import java.util.List;public class Department {private String dp_id;private String dp_name;private String cost_center;private List<Employer> employerList;public String getDp_id() {return dp_id;}public void setDp_id(String dp_id) {this.dp_id = dp_id;}public String getDp_name() {return dp_name;}public void setDp_name(String dp_name) {this.dp_name = dp_name;}public String getCost_center() {return cost_center;}public void setCost_center(String cost_center) {this.cost_center = cost_center;}public List<Employer> getEmployerList() {return employerList;}public void setEmployerList(List<Employer> employerList) {this.employerList = employerList;}public Department() {super();}}
Employerdao. Java
package com.mymaven.mybatisdemo.dao;import com.mymaven.mybatisdemo.po.Employer;import java.util.List;public interface EmployerDao {public Employer getEmpByName(String name);public List<Employer> getAllEmpDpt(String dp_id);}
Departmentmapper. Java (the class name is not unified ...)
package com.mymaven.mybatisdemo.dao;import com.mymaven.mybatisdemo.po.Department;import java.util.List;import java.util.Map;public interface DepartmentMapper {public void addDepartMent(Department dpt);public Department queryByDpName(String dp_name);//public Department getDepartmentById(String dp_id);public List<Department> queryAllDepartment();public void updateDepartment(Department dpt);}
Employermapper. xml
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- In this namespace, you must specify the DaO interface --> <mapper namespace = "com. mymaven. mybatisdemo. dao. employerdao "> <resultmap id =" employerresult "type =" employer "> <ID property =" em_id "column =" em_id "/> <result property =" name "column =" name "/> <result property =" station "column =" station "/> <result property =" work_years "column =" work_years "/> <result property =" create_date "column = "create_date"/> <association property = "DPT" re Sultmap = "com. mymaven. mybatisdemo. Dao. departmentmapper. departmentresult"/> <! -- Note the reference method --> </resultmap> <select id = "getempbyname" parametertype = "Java. lang. string "resultmap =" employerresult "> select * From t_employer e inner join t_department t on E. dp_id = T. dp_id where E. name =#{ name} </SELECT> </mapper>
Departmentmapper. XML ()
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- In this namespace, you must specify the DaO interface --> <mapper namespace = "com. mymaven. mybatisdemo. Dao. departmentmapper"> <! -- Property Association attribute: class of the object associated with employer --> <resultmap id = "departmentresult" type = "department"> <ID column = "dp_id" property = "dp_id"/> <result column = "dp_name" property = "dp_name"/> <result column = "cost_center" property = "cost_center"/> <collection property = "employerlist" oftype = "employer" resultmap = "com. mymaven. mybatisdemo. dao. employerdao. employerresult "/> <! -- Note the reference method --> </resultmap> <select id = "querybydpname" resultmap = "departmentresult" parametertype = "Java. lang. string "> select * From t_employer e inner join t_department t on E. dp_id = T. dp_id where T. dp_name =#{ dp_name} </SELECT> </mapper>
Test code:
Package COM. mymaven. mybatisdemo. test; import COM. mymaven. mybatisdemo. dao. departmentmapper; import COM. mymaven. mybatisdemo. dao. employerdao; import COM. mymaven. mybatisdemo. dao. groupdao; import COM. mymaven. mybatisdemo. po. employer; import COM. mymaven. mybatisdemo. po. group; import COM. mymaven. mybatisdemo. utils. mybatishelper; import COM. mymaven. mybatisdemo. po. department; import Org. apache. ibatis. session. sqlsession; import Org. apache. ibatis. session. sqlsessionfactory; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. filesystemxmlapplicationcontext; import Java. util. list; import Java. util. map; public class testmyabtisdemo {public static void main (string [] ARGs) {testmyabtisdemo tmbd = new testmyabtisdemo (); tmbd. getdptandemps ();}// Test the public void getdptandemps () {applicationcontext act = new filesystemxmlapplicationcontext ("classpath *: applicationcontext. XML "); departmentmapper = act. getbean (departmentmapper. class); Department = departmentmapper. querybydpname ("department 1"); system. out. println (department. getdp_id () + "-" + department. getdp_name () + "-" + department. getcost_center () + "Number of employees" + department. getemployerlist (). size ()
);
For (Employer EMP: department. getemployerlist () {system. out. println (EMP. getname () + "-" + EMP. getdpt (). getdp_name () + "-" + EMP. getstation ());
}}
// Test the association method of 1-to-1: Public void getemployeranddpt () {applicationcontext act = new filesystemxmlapplicationcontext ("classpath *: applicationcontext. XML "); employerdao empm = act. getbean (employerdao. class); employer EMP = empm. getempbyname ("Liu Jiang"); system. out. println (EMP. getname () + "-" + EMP. getdpt () + "=" + EMP. getstation () + "-");
}
}