Mybatis(5)一对一映射

学生和身份证是一对一映射


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());
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值