先给大家看一下我的项目 整个的一个 构造:
好,然后 开始我的mybatis关系,用的是mysql
1.首先,先导入依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
2.然后,连接数据库,写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>
<!-- 懒加载 -->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<!--设置别名-->
<typeAliases>
<!--<typeAlias type="com.desert.Dto.MyPerson" alias="a"></typeAlias>-->
<package name="com.desert"></package>
</typeAliases>
<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://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- Q 加载dao方法 -->
<!--一对一-->
<mapper class="com.desert.dao.ICardDao"/>
<mapper class="com.desert.dao.IPersonDao"/>
<!--一对多-->
<mapper class="com.desert.dao.ICityDao"></mapper>
<mapper class="com.desert.dao.IProvincesDao"></mapper>
<!--多对多-->
<mapper class="com.desert.dao.IUserDao"></mapper>
<mapper class="com.desert.dao.IRolesDao"></mapper>
</mappers>
</configuration>
一:说一下 我的一对一关系:
一对一我那Person和Card拿来做实列:
Person表:
private String pname;
private int pid;
private int page;
private Card card;
-------------------------------------------------------------
Person数据库:
然后 再是我的Card表:(也可以定义一个Person属性,这里我就不定义了)
private int uid;
private String cnumber;
------------------------------------
数据库:
然后,再是我的dao方法:
IPersonDao:
public interface IPersonDao {
@Select("select * from person where pid = #{pid}")
@Results({
@Result(id=true,column="pid",property="pid"),
@Result(column="pname",property="pname"),
@Result(column="page",property="page"),
@Result(column="pid",property="card",one=@One(select="com.desert.dao.ICardDao.getCard",fetchType= FetchType.EAGER))
})
public Person getPerson(int pid);
}
ICardDao:
public interface ICardDao {
@Select("select * from card where uid = #{uid} ")
public Card getCard(int uid);
}
这样,一个简单的一对一关系就好了。
然后,再是去测试
@Test //一对一
public void Testonttoone() throws IOException {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config"));
SqlSession sqlSession = sqlSessionFactory.openSession();
IPersonDao iPersonDao=sqlSession.getMapper(IPersonDao.class);
// 根据id查询Person对象,同时需要获得关联的Card对象
Person person=iPersonDao.getPerson(1);
System.out.println(person);
System.out.println(person.getCard().getCnumber());
sqlSession.close();
}
二:再说一下 一对多的关系:
这里 我拿省份(Provinces)和城市(Citys)拿来做实列:
Provinces表:
private int pid;
private String pname;
private Set<Citys> citysSet;
--------------------------------
数据库:
Citys表:
private int cid;
private String cname;
private int pid;
--------------------------------------------
数据库:
然后,再是我的dao方法:
IProvincesDao:
public interface IProvincesDao {
@Select("select * from provinces where pid = #{pid}")
@Results({
@Result(id=true,column="pid",property="pid"),
@Result(column="pname",property="pname"),
@Result(column="pid",property="citysSet",
many=@Many(
select="com.desert.dao.ICityDao.getCitybypid",
fetchType= FetchType.LAZY
)
)
})
public Provinces getProvincesByid(int pid);
}
ICitysDao:
public interface ICityDao {
@Select("select * from city where pid=#{pid}")
public List<Citys> getCitybypid(int pid);
}
之后,再mybatis-config加载之后,就可以测试了:
测试类:
@Test //一对多
public void Testonttomany() throws IOException {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config"));
SqlSession sqlSession = sqlSessionFactory.openSession();
IProvincesDao iProvincesDao=sqlSession.getMapper(IProvincesDao.class);
Provinces provinces=iProvincesDao.getProvincesByid(1);
System.out.println(provinces.getPname());
for (Citys citys : provinces.getCitysSet()) {
System.out.println(citys.getCname());
}
sqlSession.close();
}
三:再是我的多对多,这里,拿用户(Users)和角色(Roles)来做实列:
Users:
private int uid;
private String uname;
private Set<Roles> roles;
------------------------------------------------
数据库:
Roles:
private int rid;
private String rname;
private Set<Users> users;
-----------------------------------------
数据库:
然后 再是我的IUsersDao:
public interface IUserDao {
//根据id得到用户:
@Select("select * from users where uid = #{uid}")
@Results({
@Result(id=true,column="uid",property="uid"),
@Result(column="uname",property="uname"),
@Result(column="uid",property="roles",
many=@Many(
select="com.desert.dao.IRolesDao.getAllRolesByuid",
fetchType= FetchType.LAZY
)
)
})
public Users getUsersById(int uid);
}
我的IRolesDao:
public interface IRolesDao {
//根据用户id得到所有的角色:
@Select("select * from roles where rid in(select rid from u_r where uid=1)")
public List<Roles> getAllRolesByuid(int uid);
}
然后,在mybatis-config加载好配置文件 就可以直接去测试了:
@Test //多对多
public void Testmanytomany() throws IOException {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config"));
SqlSession sqlSession = sqlSessionFactory.openSession();
IUserDao iUserDao=sqlSession.getMapper(IUserDao.class);
Users users=iUserDao.getUsersById(1);
System.out.println(users.getUname());
for (Roles roles : users.getRoles()) {
System.out.println(roles.getRname());
}
sqlSession.close();
}