mybatis中的映射关系,这里我用的依然是hibernate中定义的几张表。如果没有看hibernate的映射关系的话,可能有点不明白表之间的关系。这里就不多说了。
首先是准备工作,新建一个动态web项目,导入mybatis的jar包跟数据库连接的jar包。工程目录如下:
工程目录(一些包是自己建的)
然后我们在util包下创建一个sqlsession的工具类。如下
package com.nieshenkuan.util;
import java.io.IOException;
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 MyBatisUtil {
static InputStream inputStream ;
public static SqlSession getSqlSession() throws IOException {
inputStream= Resources.getResourceAsStream("sqlMapperConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
在创建这个工具类之前,我们要先创建一个sqlMapperConfig.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>
<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:///mys" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
</mappers>
</configuration>
这里我把mapper删掉了,等会要用的时候添加即可。
接下来就学习一对一的映射关系:
这里用到Person跟IdCard两个实体,具体关系看hibernate中的。这里使用的是外键关联一对一。(觉得主键关联一对一并不是很好)
1、创建实体对象
Person.java
package com.nieshenkuan.model;
public class Person {
private int id;
private String name;
private IdCard idCard;
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 IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", idCard=" + idCard + "]";
}
}
IdCard.java
package com.nieshenkuan.model;
public class IdCard {
private int id;
private String cardNo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
@Override
public String toString() {
return "IdCard [id=" + id + ", cardNo=" + cardNo + "]";
}
}
2、创建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.nieshenkuan.dao.PersonMapper">
<!-- 一个person对应一个idcard -->
<resultMap type="com.nieshenkuan.model.Person" id="personResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<association property="idCard" column="cardId" javaType="com.nieshenkuan.model.IdCard">
<id property="id" column="id" />
<result property="cardNo" column="cardNo" />
</association>
</resultMap>
<select id="getPerson" resultMap="personResultMap" parameterType="java.lang.Integer">
select * from person p, idcard i where i.id=p.cardId and p.id=#{id}
</select>
</mapper>
3、创建dao接口(PersonMapper.java)
package com.nieshenkuan.dao;
import com.nieshenkuan.model.Person;
public interface PersonMapper {
// 查询一个Person并把idcard对象一并带着查出来
public Person getPerson(int id);
}
4、将PersonMapper.xml注册到sqlMapperConfig.xml文件中去
<mappers>
<mapper resource="com/nieshenkuan/mapper/PersonMapper.xml"></mapper>
</mappers>
5、测试
package com.nieshenkuan.test;
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.nieshenkuan.dao.PersonMapper;
import com.nieshenkuan.model.Person;
import com.nieshenkuan.util.MyBatisUtil;
public class TestOneToOne {
@Test
public void test1() throws IOException {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
/*
* 1、首先根据person的id查到person对象,这里是外键关联一对一,
* 在person实体对应的数据库中有一个字段是作为外键指向idcard对象的id的
*/
Person person = personMapper.getPerson(1);
/*
* 2、得到person对象后,同时也可以得到idcard对象,mybatis这点要在mapper文件中才能显现出来,
* 对象之间的一对一,一对多,多对多主要有两个关键字给弄得。association跟collection
* */
System.out.println(person.getName());
System.out.println(person.getIdCard().getCardNo());
System.out.println(person);
}
}
6、测试结果
qqq
11111111
Person [id=1, name=qqq, idCard=IdCard [id=1, cardNo=11111111]]
网友评论