先給大家看一下我的項目 整個的一個 構造:

好,然後 開始我的mybatis關系,用的是mysql
1.首先,先導入依賴:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
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 Set<Citys> citysSet;
--------------------------------
Citys表:
private int cid;
private String cname;
--------------------------------------------
IProvincesDao:
public interface IProvincesDao {
@Select("select * from provinces where pid = #{pid}")
@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 {
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());
}
三:再是我的多對多,這裡,拿使用者(Users)和角色(Roles)來做實列:
Users:
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}")
@Result(id=true,column="uid",property="uid"),
@Result(column="uname",property="uname"),
@Result(column="uid",property="roles",
select="com.desert.dao.IRolesDao.getAllRolesByuid",
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 {
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());