MyBatis 一对一(OneToOne)__SELECT
1.创建SQL脚本:
CREATE TABLE t_person(
id int(3) not null auto_increment,
name varchar(20) default null,
age int(3) default 0,
primary key (id)
) charset="gb2312";
CREATE TABLE t_passport(
id int(3),
serial varchar(20) default NULL,
expiry int(3) default NULL,
PRIMARY KEY (id),
CONSTRAINT fk_passport FOREIGN KEY (id) REFERENCES t_person(id)
)charset="gb2312";
id int(3) not null auto_increment,
name varchar(20) default null,
age int(3) default 0,
primary key (id)
) charset="gb2312";
CREATE TABLE t_passport(
id int(3),
serial varchar(20) default NULL,
expiry int(3) default NULL,
PRIMARY KEY (id),
CONSTRAINT fk_passport FOREIGN KEY (id) REFERENCES t_person(id)
)charset="gb2312";
-- t_passport主键Id 参照外键 t_person下的Id
2.创建实体类(POJO):
Person.java
package com.lixing.ibatis.entity.onetoone;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 4524251837950555570L;
private int id;
private String name;
private int age;
private Passport passport; //Person指向Passport的引用
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Passport getPassport() {
return passport;
}
public void setPassport(Passport passport) {
this.passport = passport;
}
}
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 4524251837950555570L;
private int id;
private String name;
private int age;
private Passport passport; //Person指向Passport的引用
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Passport getPassport() {
return passport;
}
public void setPassport(Passport passport) {
this.passport = passport;
}
}
Passport.java
package com.lixing.ibatis.entity.onetoone;
public class Passport {
private int id;
private String serial;
private int expiry;
private Person person; //Passport指向Person的引用
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public int getExpiry() {
return expiry;
}
public void setExpiry(int expiry) {
this.expiry = expiry;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
public class Passport {
private int id;
private String serial;
private int expiry;
private Person person; //Passport指向Person的引用
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public int getExpiry() {
return expiry;
}
public void setExpiry(int expiry) {
this.expiry = expiry;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
3.创建Mapper接口:
PersonMapper.java
package com.lixing.ibatis.onetoone.mapper;
import com.lixing.ibatis.entity.onetoone.Person;
public interface PersonMapper {
import com.lixing.ibatis.entity.onetoone.Person;
public interface PersonMapper {
void insertPerson(Person person);
Person getPerson(int id);
}
Person getPerson(int id);
}
PassportMapper.java
package com.lixing.ibatis.onetoone.mapper;
import com.lixing.ibatis.entity.onetoone.Passport;
public interface PassportMapper {
void insertPassport(Passport passport);
Passport getPassport(int id);
}
import com.lixing.ibatis.entity.onetoone.Passport;
public interface PassportMapper {
void insertPassport(Passport passport);
Passport getPassport(int id);
}
4.创建Mapper映射XML文件:
PersonMapper.xml
<?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="com.lixing.ibatis.onetoone.mapper.PersonMapper">
<parameterMap type="Person" id="parameterPersonMap">
<parameter property="id"/>
<parameter property="name"/>
<parameter property="age"/>
</parameterMap>
<insert id="insertPerson" parameterMap="parameterPersonMap">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID() AS ID
</selectKey>
INSERT INTO t_person(name,age)
VALUES(#{name},#{age})
</insert>
<resultMap type="Person" id="personMap">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="passport" column="id" javaType="Passport" select="com.lixing.ibatis.onetoone.mapper.PassportMapper.getPassport">
</association>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lixing.ibatis.onetoone.mapper.PersonMapper">
<parameterMap type="Person" id="parameterPersonMap">
<parameter property="id"/>
<parameter property="name"/>
<parameter property="age"/>
</parameterMap>
<insert id="insertPerson" parameterMap="parameterPersonMap">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID() AS ID
</selectKey>
INSERT INTO t_person(name,age)
VALUES(#{name},#{age})
</insert>
<resultMap type="Person" id="personMap">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="passport" column="id" javaType="Passport" select="com.lixing.ibatis.onetoone.mapper.PassportMapper.getPassport">
</association>
<!-- property:指Person中指向Passport对象的实例名称 -->
<!--select:指执行级联查询-->
</resultMap>
<select id="getPerson" resultMap="personMap" parameterType="int">
SELECT * FROM t_person
WHERE id=#{id}
</select>
</mapper>
</resultMap>
<select id="getPerson" resultMap="personMap" parameterType="int">
SELECT * FROM t_person
WHERE id=#{id}
</select>
</mapper>
PassportMapper.xml
<?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="com.lixing.ibatis.onetoone.mapper.PassportMapper">
<parameterMap type="Passport" id="passportMap">
<parameter property="id"/>
<parameter property="serial"/>
<parameter property="expiry"/>
</parameterMap>
<insert id="insertPassport" parameterMap="passportMap">
INSERT INTO t_passport(id,serial,expiry)
VALUES(?,?,?)
</insert>
<resultMap type="Passport" id="passportMap">
<result property="id" column="id"/>
<result property="serial" column="serial"/>
<result property="expiry" column="expiry"/>
<association property="person" column="id" select="com.lixing.ibatis.onetoone.mapper.PersonMapper.getPerson">
</association>
</resultMap>
<select id="getPassport" resultMap="passportMap" parameterType="int">
SELECT *
FROM t_passport
WHERE id=#{id}
</select>
</mapper>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lixing.ibatis.onetoone.mapper.PassportMapper">
<parameterMap type="Passport" id="passportMap">
<parameter property="id"/>
<parameter property="serial"/>
<parameter property="expiry"/>
</parameterMap>
<insert id="insertPassport" parameterMap="passportMap">
INSERT INTO t_passport(id,serial,expiry)
VALUES(?,?,?)
</insert>
<resultMap type="Passport" id="passportMap">
<result property="id" column="id"/>
<result property="serial" column="serial"/>
<result property="expiry" column="expiry"/>
<association property="person" column="id" select="com.lixing.ibatis.onetoone.mapper.PersonMapper.getPerson">
</association>
</resultMap>
<select id="getPassport" resultMap="passportMap" parameterType="int">
SELECT *
FROM t_passport
WHERE id=#{id}
</select>
</mapper>
5.创建测试类:
package com.lixing.ibatis.test;
import java.sql.SQLException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.lixing.ibatis.entity.onetoone.Passport;
import com.lixing.ibatis.entity.onetoone.Person;
import com.lixing.ibatis.onetoone.mapper.PassportMapper;
import com.lixing.ibatis.onetoone.mapper.PersonMapper;
import com.lixing.ibatis.util.MyBatisUtil;
public class PassportMapperTest {
private SqlSessionFactory sqlSessionFactory = null;
private SqlSession session = null;
@Before
public void before() {
sqlSessionFactory = MyBatisUtil.getInstance();
session = sqlSessionFactory.openSession();
}
@Test
public void testInsertPerson() throws SQLException {
Person person = new Person();
person.setName("李新2");
person.setAge(25);
Passport passport = new Passport();
passport.setSerial("Serial2");
passport.setExpiry(22222);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Transaction tx = transactionFactory.newTransaction(session
.getConnection(), false);
PersonMapper personMapper = session.getMapper(PersonMapper.class);
PassportMapper passportMapper = session.getMapper(PassportMapper.class);
try{
personMapper.insertPerson(person);
passport.setId(person.getId());
passportMapper.insertPassport(passport);
tx.commit();
}catch(Exception e){
tx.rollback();
e.printStackTrace();
}finally{
tx.close();
System.out.println(person.getId());
}
}
@Test
public void testGetPerson(){
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Person person=personMapper.getPerson(2);
Passport passport=person.getPassport();
System.out.println(person.getId()+"\t"+person.getName()+"\t"+person.getAge()+"\t"+passport.getSerial()+"\t"+passport.getExpiry());
session.commit();
}
@Test
public void testGetPassport(){
PassportMapper passportMapper=session.getMapper(PassportMapper.class);
Passport passport=passportMapper.getPassport(2);
Person person=passport.getPerson();
System.out.println(passport.getId()+"\t"+passport.getExpiry()+"\t"+person.getName());
}
@After
public void after() {
session.close();
}
}
import java.sql.SQLException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.lixing.ibatis.entity.onetoone.Passport;
import com.lixing.ibatis.entity.onetoone.Person;
import com.lixing.ibatis.onetoone.mapper.PassportMapper;
import com.lixing.ibatis.onetoone.mapper.PersonMapper;
import com.lixing.ibatis.util.MyBatisUtil;
public class PassportMapperTest {
private SqlSessionFactory sqlSessionFactory = null;
private SqlSession session = null;
@Before
public void before() {
sqlSessionFactory = MyBatisUtil.getInstance();
session = sqlSessionFactory.openSession();
}
@Test
public void testInsertPerson() throws SQLException {
Person person = new Person();
person.setName("李新2");
person.setAge(25);
Passport passport = new Passport();
passport.setSerial("Serial2");
passport.setExpiry(22222);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Transaction tx = transactionFactory.newTransaction(session
.getConnection(), false);
PersonMapper personMapper = session.getMapper(PersonMapper.class);
PassportMapper passportMapper = session.getMapper(PassportMapper.class);
try{
personMapper.insertPerson(person);
passport.setId(person.getId());
passportMapper.insertPassport(passport);
tx.commit();
}catch(Exception e){
tx.rollback();
e.printStackTrace();
}finally{
tx.close();
System.out.println(person.getId());
}
}
@Test
public void testGetPerson(){
PersonMapper personMapper=session.getMapper(PersonMapper.class);
Person person=personMapper.getPerson(2);
Passport passport=person.getPassport();
System.out.println(person.getId()+"\t"+person.getName()+"\t"+person.getAge()+"\t"+passport.getSerial()+"\t"+passport.getExpiry());
session.commit();
}
@Test
public void testGetPassport(){
PassportMapper passportMapper=session.getMapper(PassportMapper.class);
Passport passport=passportMapper.getPassport(2);
Person person=passport.getPerson();
System.out.println(passport.getId()+"\t"+passport.getExpiry()+"\t"+person.getName());
}
@After
public void after() {
session.close();
}
}
6.Mybatis配置文件:
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">
<configuration>
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<!---->
<typeAlias alias="Passport" type="com.lixing.ibatis.entity.onetoone.Passport" />
<typeAlias alias="Person" type="com.lixing.ibatis.entity.onetoone.Person"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="7501857" />
</dataSource>
</environment>
</environments>
<mappers>
<!---->
<mapper resource="com/lixing/ibatis/onetoone/mapper/PassportMapper.xml" />
<mapper resource="com/lixing/ibatis/onetoone/mapper/PersonMapper.xml" />
</mappers>
</configuration>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<!---->
<typeAlias alias="Passport" type="com.lixing.ibatis.entity.onetoone.Passport" />
<typeAlias alias="Person" type="com.lixing.ibatis.entity.onetoone.Person"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="7501857" />
</dataSource>
</environment>
</environments>
<mappers>
<!---->
<mapper resource="com/lixing/ibatis/onetoone/mapper/PassportMapper.xml" />
<mapper resource="com/lixing/ibatis/onetoone/mapper/PersonMapper.xml" />
</mappers>
</configuration>