create table mybatis_one2one_addresses(
one2one_addr_id int(3) auto_increment not null primary key,
one2one_street varchar(20) not null,
one2one_city varchar(20) not null,
one2one_state varchar(20) not null,
one2one_zip varchar(10),
one2one_country varchar(20));
create table mybatis_one2one_students(
one2one_stud_id int(3)auto_increment not null primary key,
one2one_name varchar(20) not null,
one2one_email varchar(20),
one2one_dob date,
one2one_phone varchar(15),
one2one_addr_id int references addresses(one2one_addr_id));
insert into mybatis_one2one_addresses(one2one_addr_id,one2one_street,one2one_city,one2one_state,one2one_zip,one2one_country) values(1,'redSt','kunshan','W','12345','china');
insert into mybatis_one2one_addresses(one2one_addr_id,one2one_street,one2one_city,one2one_state,one2one_zip,one2one_country) values(2,'blueST','kunshan','W','12345','china');
insert into mybatis_one2one_students(one2one_stud_id,one2one_name,one2one_email,one2one_phone,one2one_addr_id) values(1,'John','[email protected]','123-456-7890',1);
insert into mybatis_one2one_students(one2one_stud_id,one2one_name,one2one_email,one2one_phone,one2one_addr_id) values(2,'Paul','[email protected]','111-222-3333',2);
public class One2OneAddress {
private int addrId;
private String addrStreet;
private String addrCity;
private String addrState;
private String addrZip;
private String addrCountry;
}
public class One2OneStudent {
private int stuId;
private String stuName;
private String stuEmail;
private String stuDob;
private String stuPhone;
private One2OneAddress stuAddress = new One2OneAddress();
}
<mapper namespace="one2oneMapper">
<resultMap id="one2oneAddress" type="com.demo.mybatis.one2one.One2OneAddress">
<id property="addrId" column="one2one_addr_id"/>
<result property="addrStreet" column="one2one_street"/>
<result property="addrCity" column="one2one_city"/>
<result property="addrState" column="one2one_state"/>
<result property="addrZip" column="one2one_zip"/>
<result property="addrCountry" column="one2one_country"/>
resultMap>
<resultMap id="one2oneStudent" type="com.demo.mybatis.one2one.One2OneStudent">
<id property="stuId" column="one2one_stud_id"/>
<result property="stuName" column="one2one_name"/>
<result property="stuEmail" column="one2one_email"/>
<result property="stuDob" column="one2one_dob"/>
<result property="stuPhone" column="one2one_phone"/>
<association property="stuAddress" resultMap="one2oneAddress"/>
resultMap>
<select id="selectStudent" parameterType="int" resultMap="one2oneStudent">
select * from mybatis_one2one_students left outer join mybatis_one2one_addresses
on mybatis_one2one_students.one2one_addr_id = mybatis_one2one_addresses.one2one_addr_id
where one2one_stud_id = #{stuId}
select>
<select id="selectStudentShot" parameterType="int" resultMap="one2oneStudent">
select * from mybatis_one2one_students student left outer join mybatis_one2one_addresses address
on student.one2one_addr_id = address.one2one_addr_id
where one2one_stud_id = #{stuId}
select>
mapper>
@Test
public void selectStudent() {
SqlSession session = SessionUtis.getSession();
One2OneStudent student = session.selectOne("one2oneMapper.selectStudent", 1);
System.out.println(student.toString());
}
@Test
public void selectStudentShot() {
SqlSession session = SessionUtis.getSession();
One2OneStudent student = session.selectOne("one2oneMapper.selectStudentShot", 1);
System.out.println(student.toString());
}
create table mybatis_one2many_person(
one2many_person_id int(3) auto_increment not null primary key,
one2many_name varchar(100),
one2many_email varchar(100),
one2many_phone varchar(15));
create table mybatis_one2many_classes(
one2many_classes_id int(3) auto_increment not null primary key,
one2many_name varchar(100) not null,
one2many_description varchar(512),
one2many_start_date date ,
one2many_end_date date ,
one2many_person_ids int references mybatis_one2many_tutors (one2many_person_id));
insert into mybatis_one2many_person
(one2many_name,one2many_email,one2many_phone)
values('test name','[email protected]','123-456-7890');
insert into mybatis_one2many_person
(one2many_name,one2many_email,one2many_phone)
values('test name','[email protected]','111-222-3333');
insert into mybatis_one2many_classes
(one2many_name,one2many_description,one2many_person_ids) values
('JavaSE','JavaSE',1);
insert into mybatis_one2many_classes
(one2many_name,one2many_description,one2many_person_ids) values
('JavaEE','JavaEE',2);
insert into mybatis_one2many_classes
(one2many_name,one2many_description,one2many_person_ids) values
('MyBatis','MyBatis',1);
public class One2manyPerson {
private int personId;
private String personName;
private String personEmail;
private String personPhone;
}
public class One2manyClasses {
private int classId;
private String className;
private String classDescription;
private Date startDate;
private Date endDate;
private List personList = new ArrayList();
}
"one2manyMapper">
id="one2manyPerson" type="com.demo.mybatis.one2many.One2manyPerson">
<id property="personId" column="one2many_person_id"/>
<result property="personName" column="one2many_name"/>
<result property="personEmail" column="one2many_email"/>
<result property="personPhone" column="one2many_phone"/>
id="one2manyClasses" type="com.demo.mybatis.one2many.One2manyClasses">
<id property="classId" column="one2many_classes_id"/>
<result property="className" column="one2many_name"/>
<result property="classDescription" column="one2many_description"/>
<result property="startDate" column="one2many_start_date"/>
<result property="endDate" column="one2many_end_date"/>
property="personList" resultMap="one2manyPerson"/>
"one2manyMapper2">
id="one2manyPerson" type="com.demo.mybatis.one2many.One2manyPerson">
<id property="personId" column="one2many_person_id"/>
<result property="personName" column="one2many_name"/>
<result property="personEmail" column="one2many_email"/>
<result property="personPhone" column="one2many_phone"/>
id="one2manyClasses" type="com.demo.mybatis.one2many.One2manyClasses">
<id property="classId" column="one2many_classes_id"/>
<result property="className" column="one2many_name"/>
<result property="classDescription" column="one2many_description"/>
<result property="startDate" column="one2many_start_date"/>
<result property="endDate" column="one2many_end_date"/>
property="personList" column="one2many_person_ids" select="selectPerson"/>
@Test
public void selectClasses(){
SqlSession session = SessionUtis.getSession();
One2manyClasses classes = session.selectOne("one2manyMapper.selectClasses",1);
System.out.println(classes.toString());
}
@Test
public void selectClasses2(){
SqlSession session = SessionUtis.getSession();
One2manyClasses classes = session.selectOne("one2manyMapper2.selectClasses",1);
System.out.println(classes.toString());
}
create table mybatis_many2many_cart (
many2many_cart_id int(3) auto_increment not null primary key,
many2many_cart_code varchar(50) not null,
many2many_cart_name varchar(50) not null);
create table mybatis_many2many_student (
many2many_student_id int(3) auto_increment not null primary key,
many2many_name varchar(50) not null,
many2many_gender varchar(50) ,
many2many_major varchar(50) ,
many2many_grade varchar(50));
create table mybatis_many2many_other (
many2many_other_id int(3) auto_increment not null primary key,
many2many_student_id int references mybatis_many2many_student(many2many_student_id),
many2many_cart_id int references mybatis_many2many_cart(many2many_cart_id));
insert into mybatis_many2many_cart
(many2many_cart_name,many2many_cart_code) values
('JavaSE',1);
insert into mybatis_many2many_cart
(many2many_cart_name,many2many_cart_code) values
('JavaEE',2);
insert into mybatis_many2many_cart
(many2many_cart_name,many2many_cart_code) values
('MyBatis',1);
insert into mybatis_many2many_student
(many2many_name,many2many_gender,many2many_major,many2many_grade)
values('John','johncom','1890','123-7890');
insert into mybatis_many2many_student
(many2many_name,many2many_gender,many2many_major,many2many_grade)
values('Paul','paulcom','111-333','123-90');
insert into mybatis_many2many_other(many2many_student_id,many2many_cart_id)values(1,1);
insert into mybatis_many2many_other(many2many_student_id,many2many_cart_id)values(1,2);
insert into mybatis_many2many_other(many2many_student_id,many2many_cart_id)values(1,3);
insert into mybatis_many2many_other(many2many_student_id,many2many_cart_id)values(2,1);
insert into mybatis_many2many_other(many2many_student_id,many2many_cart_id)values(2,2);
insert into mybatis_many2many_other(many2many_student_id,many2many_cart_id)values(2,3);
public class Many2manyCart {
private int cartId;
private String carCode;
private String cartName;
private List studentList = new ArrayList();
}
public class Many2manyStudent {
private int stuId;
private String stuName;
private String stuGender;
private String stuMajor;
private String stuGrade;
private List cartList = new ArrayList();
}
"many2manyMapper">
id="many2manyStudent" type="com.demo.mybatis.many2many.Many2manyStudent">
<id property="stuId" column="many2many_student_id"/>
<result property="stuName" column="many2many_name"/>
<result property="stuGender" column="many2many_gender"/>
<result property="stuMajor" column="many2many_major"/>
<result property="stuGrade" column="many2many_grade"/>
id="many2manyCart" type="com.demo.mybatis.many2many.Many2manyCart">
<id property="cartId" column="many2many_cart_id"/>
<result property="carCode" column="many2many_cart_code"/>
<result property="cartName" column="many2many_cart_name"/>
id="otherStudent" type="com.demo.mybatis.many2many.Many2manyStudent" extends="many2manyStudent">
property="cartList" resultMap="many2manyCart"/>
id="otherCart" type="com.demo.mybatis.many2many.Many2manyCart" extends="many2manyCart">
property="studentList" resultMap="many2manyStudent"/>
@Test
public void selectStudent() {
SqlSession session = SessionUtis.getSession();
List student = session.selectList("many2manyMapper.selectStudent", 1);
System.out.println(student.toString());
}
@Test
public void selectCart() {
SqlSession session = SessionUtis.getSession();
List carts = session.selectList("many2manyMapper.selectCart", 1);
System.out.println(carts.toString());
}
https://github.com/brusion/brusion-code/tree/master/demo-java/04%20-%20demo%20-%20mybatis/mybatis-04