查询每个用户信息,以及他所对应的用户档案信息
public class UserInfo {
/* id NUMBER(5) not null,
name VARCHAR2(50),
password VARCHAR2(50),
telephone VARCHAR2(15),
username VARCHAR2(50),
isadmin VARCHAR2(5)*/
private long id;
private String name;
private String password;
private String telephone;
private String username;
private String isadmin;
private UserDetail userda;
getxxx() setxxx().......
}
public class UserDetail {
/* id number(10) primary key,
country varchar2(20),
privince varchar2(20),
city varchar2(20),
street varchar2(20),
sn varchar2(20)*/
private long did;
private String country;
private String privince;
private String city;
private String street;
private String sn;
public UserDetail(long did, String country, String privince, String city,
String street, String sn) {
super();
this.did = did;
this.country = country;
this.privince = privince;
this.city = city;
this.street = street;
this.sn = sn;
}
public UserDetail() {
super();
}
public long getDid() {
return did;
}
public void setDid(long did) {
this.did = did;
}
................}
为体现一对一的关系,要在原来的用户中添加一个字段
private UserDetail userda;
他表示一个档案的实例
import java.io.IOException;
import java.util.List;
import com.entity.UserInfo;
public interface IUserInfoDao {
public List<UserInfo> getAll();
}
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.IUserInfoDao">
<resultMap type="UserInfo" id="user">
<id property="id" column="empno" />
<result property="id" column="id" />
<result property="name" column="name" />
<result property="password" column="password"/>
<result property="telephone" column="telephone" />
<result property="username" column="username" />
<result property="isadmin" column="isadmin" />
<result property="userda.did" column="id" />
<result property="userda.country" column="country"/>
<result property="userda.privince" column="privince" />
<result property="userda.city" column="city" />
<result property="userda.street" column="street" />
<result property="userda.sn" column="sn" />
</resultMap>
<!--public List<UserInfo> getAll(); -->
<select id="getAll" resultMap="user">
select users.* ,userinfo.*
from users ,userinfo
where users.id=userinfo.did
</select>
</mapper>
public List<UserInfo> getAll() {
IUserInfoDao dao=MybatisUtil.getSqlSessetion1().getMapper(IUserInfoDao.class);
List<UserInfo> users= dao.getAll();
MybatisUtil.closeSqlSessetion();
return users;
}
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
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 {
//* A: 静态常量 开发中,我们想在类中定义一个静态常量,通常使用public ,Sttatic final修饰的变量来完成定义。
private static final String CONFIG="mybatis-config.xml";//配置文件名称用常量标示
/*private static SqlSessionFactory fac1=null;//建立sqlsession工厂
private static SqlSessionFactory fac2=null;*/
private static Map<String,SqlSessionFactory> SQLSESSIONFOCTORYS=new HashMap<String,SqlSessionFactory>();
private static ThreadLocal<SqlSession> th=new ThreadLocal<SqlSession>();//建立本地线程
创建线程局部变量threadLocal,用来保存Mybatis的threadLocal
static{//静态与,加载时执行一次,在类初始化之前实现
InputStream is=null;
//1ess
try {//
//is=Resources.getResourceAsStream(CONFIG);//将配置文件资源作为字符流
//初始化sqlsession工厂构造器对象 /通过sqlsession工厂构造器对象将字符流建立SqlSession对象
SqlSessionFactory fac1=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(CONFIG),"house");
SqlSessionFactory fac2=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(CONFIG),"testmysql");
SQLSESSIONFOCTORYS.put("house", fac1);
SQLSESSIONFOCTORYS.put("testmysql", fac2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2
}
public static SqlSession getSqlSessetion1(){
SqlSession session=th.get();得到线程当前的SqlSession
//可以保证每个线程对应一个数据对象,在任何时刻都操作的是这个对象。
if(session==null){
session=SQLSESSIONFOCTORYS.get("house").openSession();//建立SqlSession
th.set(session);;//将新建立的SqlSession放在本地线程中
}
return session;
}
public static SqlSession getSqlSessetion2(){
SqlSession session=th.get();得到线程当前的SqlSession
//可以保证每个线程对应一个数据对象,在任何时刻都操作的是这个对象。
if(session==null){
session=SQLSESSIONFOCTORYS.get("testmysql").openSession();//建立SqlSession
th.set(session);;//将新建立的SqlSession放在本地线程中
}
return session;
}
public static void closeSqlSessetion(){
SqlSession session=th.get();//得到本地线程中的SqlSession
th.set(null);//设置本地线程threadLocal为空
if (session !=null){
session.close();
}
}
}----------------------------------------------------------