Hr账户下建表SQL语句如下:
create table identity(
card_id varchar2(18) primary key,
address_info varchar2(50)
);
create table person(
id number(9) primary key,
name varchar2(50),
age number(3),
mobile varchar2(11),
card_id varchar2(18) references identity(card_id) unique
);
create sequence person_seq;
create sequence identity_seq start with 23010019980102;
insert into identity values(identity_seq.nextval,'北京海淀区');
insert into identity values(identity_seq.nextval,'北京朝阳区');
commit();
insert into person values(person_seq.nextval,'小明',18,'12332112312','23010019980102');
insert into person values(person_seq.nextval,'小红',18,'12332112311','23010019980103');
commit();
红线为本次的项目代码:person表与identity表一对一:
Person代码:
package com.baizhi.entity;
import java.io.Serializable;
public class Person implements Serializable{
private Integer id;
private String name;
private Integer age;
private String mobile;
//关系属性
private Identity identity;
public Number 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 Number getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Identity getIdentity() {
return identity;
}
public void setIdentity(Identity identity) {
this.identity = identity;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", mobile=" + mobile + ", identity=" + identity
+ "]";
}
public Person(Integer id, String name, Integer age, String mobile, Identity identity) {
super();
this.id = id;
this.name = name;
this.age = age;
this.mobile = mobile;
this.identity = identity;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
}
Identity代码:
package com.baizhi.entity;
import java.io.Serializable;
public class Identity implements Serializable{
private String cardId;
private String addressInfo;
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getAddressInfo() {
return addressInfo;
}
public void setAddressInfo(String addressInfo) {
this.addressInfo = addressInfo;
}
@Override
public String toString() {
return "Identity [cardId=" + cardId + ", addressInfo=" + addressInfo + "]";
}
public Identity(String cardId, String addressInfo) {
super();
this.cardId = cardId;
this.addressInfo = addressInfo;
}
public Identity() {
super();
// TODO Auto-generated constructor stub
}
}
PersonDao代码:
package com.baizhi.dao;
import java.util.List;
import com.baizhi.entity.Person;
public interface PersonDao {
List<Person> queryAll();
}
PersonDaoMapper.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.baizhi.dao.PersonDao">
<!-- 定义resultMap -->
<resultMap type="Person" id="personResultMap">
<id column="pid" property="id"/>
<result column="pname" property="name"/>
<result column="page" property="age"/>
<result column="pmobile" property="mobile"/>
<!-- 关系属性的映射
此时Person中有一个关系属性,类型是一个Identity对象
property指定关系属性变量名
javaType指定实体类全限定名或别名
-->
<association property="identity" javaType="Identity">
<id column="cardId" property="cardId"/>
<result column="addressInfo" property="addressInfo"/>
</association>
</resultMap>
<select id="queryAll" resultMap="personResultMap">
select p.id pid,p.name pname,p.age page,p.mobile pmobile,
i.card_id cardId,i.address_info addressInfo
from person p left join identity i on p.card_id=i.card_id
</select>
</mapper>
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>
<!-- 引入外部的小配置文件 -->
<properties resource="jdbc.properties"></properties>
<!-- 给实体类起别名 -->
<typeAliases>
<!-- type属性值为实体类全限定名
alias 属性值为别名
-->
<!--<typeAlias type="com.baizhi.entity.Account" alias="Account"/> -->
<!-- 起别名用第二种方式 给实体类起别名 -->
<package name="com.baizhi.entity"/>
</typeAliases>
<!-- 编写mybatis的运行环境 -->
<environments default="oracle">
<environment id="oracle">
<!-- 指定事务采用JDBC的方式管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 使用mybatis提供的POOLED连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 注册mapper文件位置 -->
<mappers>
<!-- 对mapper标签注册一个mapper文件的位置:路径一定严格大小写 文件用斜杠,包用点 -->
<mapper resource="com/baizhi/dao/PersonDaoMapper.xml"></mapper>
</mappers>
</configuration>
jdbc.properties:
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=hr
password=hr
Util包中的MybatisUtil代码:
package com.baizhi.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;
import com.baizhi.dao.AccountDao;
import com.baizhi.entity.Account;
public class MybatisUtil {
public static SqlSessionFactory factory;
//静态初始代码块,书写只需要执行一次的操作,静态代码块的代码只在类加载的时候执行一次
static {
InputStream in = null;
try {
//读取核心配置文件mybatis-config.xml
in = Resources.getResourceAsStream("mybatis-config.xml");
//创建SqlSessionFactory
factory = new SqlSessionFactoryBuilder().build(in);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("初始化工厂失败");
}finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//创建一个线程绑定对象
private static ThreadLocal<SqlSession> tol=new ThreadLocal<SqlSession>();
//返回一个SqlSession
public static SqlSession opSession() {
//先到当前线程取一下
SqlSession sqlSession =tol.get();
if(sqlSession==null) {
//创建SqlSession
sqlSession = factory.openSession();
//绑定到当前线程
tol.set(sqlSession);
}
return sqlSession;
}
//释放资源
public static void close(){
SqlSession sqlSession=tol.get();
if(sqlSession!=null) {
sqlSession.close();
tol.remove();//从当前线程移除
}
}
//提交事务
public static void commit() {
SqlSession sqlSession=tol.get();
if(sqlSession!=null) {
sqlSession.commit();
}
}
//回滚事务
public static void rollback() {
SqlSession sqlSession=tol.get();
if(sqlSession!=null) {
sqlSession.rollback();
}
}
}
TestPersonDao代码:
package com.baizhi.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.baizhi.dao.PersonDao;
import com.baizhi.entity.Person;
import com.baizhi.util.MybatisUtil;
public class TestPersonDao {
@Test
public void testQueryAll() {
SqlSession sqlSession = MybatisUtil.opSession();
PersonDao dao =sqlSession.getMapper(PersonDao.class);
List<Person> list =dao.queryAll();
for (Person person : list) {
System.out.println(person.getName()+"的身份证号是:"+person.getIdentity().getCardId());
}
}
}