今天测试Ibatis的一对一的关联映射时总是出现错误,其中很多的错误都是自己不小心写错的..现把整个Ibatis源代码记录下来,以便以后熟记:
1.数据库脚本:
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
2.创建实体类(Pojo) :Person
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;
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;
}
}
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 4524251837950555570L;
private int id;
private String name;
private int age;
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;
}
}
Passport
package com.lixing.ibatis.entity.onetoone;
public class Passport {
private int id;
private String serial;
private int expiry;
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 class Passport {
private int id;
private String serial;
private int expiry;
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;
}
}
3.创建Mapper接口:(PersonMapper)
package com.lixing.ibatis.onetoone.mapper;
import com.lixing.ibatis.entity.onetoone.Person;
public interface PersonMapper {
void insertPerson(Person person);
}
import com.lixing.ibatis.entity.onetoone.Person;
public interface PersonMapper {
void insertPerson(Person person);
}
PasssportMapper
package com.lixing.ibatis.onetoone.mapper;
import com.lixing.ibatis.entity.onetoone.Passport;
public interface PassportMapper {
void insertPassport(Passport passport);
}
import com.lixing.ibatis.entity.onetoone.Passport;
public interface PassportMapper {
void insertPassport(Passport passport);
}
4.在PersonMapper、PassportMapper接口目录下创建两个XML文件:
PersonMapper.XML
xml
version
="1.0"
encoding
="UTF-8"
?>
"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 >
mapper >
"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 >
mapper >
PassportMapper.XML
xml
version
="1.0"
encoding
="UTF-8"
?>
"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 >
mapper >
"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 >
mapper >
5.myibatis配置文件(mybatis-config.xml)
xml
version
="1.0"
encoding
="UTF-8"
?>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
< configuration >
< settings >
< 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 >
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
< configuration >
< settings >
< 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 >
6.加入Junit做测试:
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.JdbcTransaction;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
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.setId(person.getId()); //写在此处是错误的
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()); // 必须写在insertPerson之后
passportMapper.insertPassport(passport);
tx.commit();
} catch(Exception e){
tx.rollback();
e.printStackTrace();
} finally{
tx.close();
System.out.println(person.getId());
}
}
@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.JdbcTransaction;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
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.setId(person.getId()); //写在此处是错误的
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()); // 必须写在insertPerson之后
passportMapper.insertPassport(passport);
tx.commit();
} catch(Exception e){
tx.rollback();
e.printStackTrace();
} finally{
tx.close();
System.out.println(person.getId());
}
}
@After
public void after() {
// session.close();
}
}
整个项目打包上传(不包括mybatis类库)