学生和身份证是一对一映射
Card.java
StudentCardDao.java
mybatis.xml,c3p0-config.xml配置文件省略不展示。
sql语句:
create table cards(
cid int(5) primary key,
cnum varchar(10)
);
create table students(
sid int(5) primary key,
sname varchar(10),
scid int(5),
constraint scid_fk foreign key(scid) references cards(cid)
);
insert into cards(cid,cnum) values(1,'111');
insert into students(sid,sname,scid) values(1,'哈哈',1);
Card.java
/**
* 身份证(单方)
* @author AdminTC
*/
public class Card {
private Integer id;
private String num;
public Card(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
Student.java
/**
* 学生(单方)
* @author AdminTC
*/
public class Student {
private Integer id;
private String name;
private Card card;//关联属性
public Student(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
}
CardMapper.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="cardNamespace">
<resultMap type="cn.itcast.javaee.mybatis.one2one.Card" id="cardMap">
<id property="id" column="cid"/>
<result property="num" column="cnum"/>
</resultMap>
</mapper>
StudentMapper.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="studentNamespace">
<resultMap type="cn.itcast.javaee.mybatis.one2one.Student" id="studentMap">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<!-- 引入CardMapper.xml文件中的映射信息
property表示Student类的关联属性
resultMap表示引入CardMapper.xml文件的映射类型
-->
<association property="card" resultMap="cardNamespace.cardMap"/>
</resultMap>
<!-- 查询1号学生的信息 -->
<select id="findById" parameterType="int" resultMap="studentMap">
select s.sid,s.sname,c.cid,c.cnum
from students s inner join cards c
on s.scid = c.cid
and s.sid = #{id}
</select>
<!-- 查询哈哈学生的信息 -->
<select id="findByName" parameterType="string" resultMap="studentMap">
select s.sname,c.cnum
from students s inner join cards c
on s.scid = c.cid
and s.sname = #{name}
</select>
</mapper>
StudentCardDao.java
/**
* 持久层
* @author AdminTC
*/
public class StudentCardDao {
/**
* 查询1号学生的信息
* @param id 表示学生的编号
*/
public Student findById(int id) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectOne("studentNamespace.findById",id);
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
/**
* 查询"哈哈"学生的信息
* @param name 表示学生的姓名
*/
public Student findByName(String name) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectOne("studentNamespace.findByName",name);
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
public static void main(String[] args) throws Exception{
StudentCardDao dao = new StudentCardDao();
Student s = dao.findById(1);
System.out.println(s.getId()+":"+s.getName()+":"+s.getCard().getId()+":"+s.getCard().getNum());
System.out.println("-------------------------------");
s = dao.findByName("哈哈");
System.out.println(s.getName()+"的身份证号码为:" + s.getCard().getNum());
}
}