1.Mybatis多表查詢
1.1 一對一查詢
1.1.1 一對一查詢的模型
用戶表和訂單表的關(guān)系為横缔,一個用戶有多個訂單,一個訂單只從屬于一個用戶
一對一查詢的需求:查詢一個訂單闯狱,與此同時查詢出該訂單所屬的用戶
1.1.2一對一查詢的語句
對應(yīng)的sql語句:select *? from orders o,user u where o.uid=u.id;
查詢的結(jié)果如下:
1.1.3 創(chuàng)建Order和User實體
publicclassOrder{
?
privateintid;
privateDateordertime;
privatedoubletotal;
?
//代表當(dāng)前訂單從屬于哪一個客戶
privateUseruser;
}
?
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
privateDatebirthday;
?
}
1.1.4 創(chuàng)建OrderMapper接口
publicinterfaceOrderMapper{
List<Order>findAll();
}
1.1.5 配置OrderMapper.xml
<mappernamespace="com.itheima.mapper.OrderMapper">
<resultMapid="orderMap"type="com.itheima.domain.Order">
<resultcolumn="uid"property="user.id"></result>
<resultcolumn="username"property="user.username"></result>
<resultcolumn="password"property="user.password"></result>
<resultcolumn="birthday"property="user.birthday"></result>
</resultMap>
<selectid="findAll"resultMap="orderMap">
? ? ?? select * from orders o,user u where o.uid=u.id
</select>
</mapper>
其中<resultMap>還可以配置如下:
<resultMapid="orderMap"type="com.itheima.domain.Order">
<resultproperty="id"column="id"></result>
<resultproperty="ordertime"column="ordertime"></result>
<resultproperty="total"column="total"></result>
<associationproperty="user"javaType="com.itheima.domain.User">
<resultcolumn="uid"property="id"></result>
<resultcolumn="username"property="username"></result>
<resultcolumn="password"property="password"></result>
<resultcolumn="birthday"property="birthday"></result>
</association>
</resultMap>
1.1.6 測試結(jié)果
OrderMappermapper=sqlSession.getMapper(OrderMapper.class);
List<Order>all=mapper.findAll();
for(Orderorder:all){
System.out.println(order);
}
1.2 一對多查詢
1.2.1 一對多查詢的模型
用戶表和訂單表的關(guān)系為,一個用戶有多個訂單,一個訂單只從屬于一個用戶
一對多查詢的需求:查詢一個用戶,與此同時查詢出該用戶具有的訂單
1.2.2 一對多查詢的語句
對應(yīng)的sql語句:select *,o.id oid from user u left join orders o on u.id=o.uid;
查詢的結(jié)果如下:
1.2.3 修改User實體
publicclassOrder{
?
privateintid;
privateDateordertime;
privatedoubletotal;
?
//代表當(dāng)前訂單從屬于哪一個客戶
privateUseruser;
}
?
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
privateDatebirthday;
//代表當(dāng)前用戶具備哪些訂單
privateList<Order>orderList;
}
?
1.2.4 創(chuàng)建UserMapper接口
publicinterfaceUserMapper{
List<User>findAll();
}
?
1.2.5 配置UserMapper.xml
<mappernamespace="com.itheima.mapper.UserMapper">
<resultMapid="userMap"type="com.itheima.domain.User">
<resultcolumn="id"property="id"></result>
<resultcolumn="username"property="username"></result>
<resultcolumn="password"property="password"></result>
<resultcolumn="birthday"property="birthday"></result>
<collectionproperty="orderList"ofType="com.itheima.domain.Order">
<resultcolumn="oid"property="id"></result>
<resultcolumn="ordertime"property="ordertime"></result>
<resultcolumn="total"property="total"></result>
</collection>
</resultMap>
<selectid="findAll"resultMap="userMap">
? ? ?? select *,o.id oid from user u left join orders o on u.id=o.uid
</select>
</mapper>
1.2.6 測試結(jié)果
UserMappermapper=sqlSession.getMapper(UserMapper.class);
List<User>all=mapper.findAll();
for(Useruser:all){
System.out.println(user.getUsername());
List<Order>orderList=user.getOrderList();
for(Orderorder:orderList){
System.out.println(order);
?? }
System.out.println("----------------------------------");
}
1.3 多對多查詢
1.3.1 多對多查詢的模型
用戶表和角色表的關(guān)系為瓮具,一個用戶有多個角色,一個角色被多個用戶使用
多對多查詢的需求:查詢用戶同時查詢出該用戶的所有角色
1.3.2 多對多查詢的語句
對應(yīng)的sql語句:select u.,r.,r.id rid from user u left join user_role ur on u.id=ur.user_id
inner join role r on ur.role_id=r.id;
查詢的結(jié)果如下:
1.3.3 創(chuàng)建Role實體凡人,修改User實體
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
privateDatebirthday;
//代表當(dāng)前用戶具備哪些訂單
privateList<Order>orderList;
//代表當(dāng)前用戶具備哪些角色
privateList<Role>roleList;
}
?
publicclassRole{
?
privateintid;
privateStringrolename;
?
}
?
1.3.4? 添加UserMapper接口方法
List<User>findAllUserAndRole();
1.3.5 配置UserMapper.xml
<resultMapid="userRoleMap"type="com.itheima.domain.User">
<resultcolumn="id"property="id"></result>
<resultcolumn="username"property="username"></result>
<resultcolumn="password"property="password"></result>
<resultcolumn="birthday"property="birthday"></result>
<collectionproperty="roleList"ofType="com.itheima.domain.Role">
<resultcolumn="rid"property="id"></result>
<resultcolumn="rolename"property="rolename"></result>
</collection>
</resultMap>
<selectid="findAllUserAndRole"resultMap="userRoleMap">
?? select u.*,r.*,r.id rid from user u left join user_role ur on u.id=ur.user_id
?? inner join role r on ur.role_id=r.id
</select>
1.3.6 測試結(jié)果
UserMappermapper=sqlSession.getMapper(UserMapper.class);
List<User>all=mapper.findAllUserAndRole();
for(Useruser:all){
System.out.println(user.getUsername());
List<Role>roleList=user.getRoleList();
for(Rolerole:roleList){
System.out.println(role);
?? }
System.out.println("----------------------------------");
}
1.4 知識小結(jié)
MyBatis多表配置方式:
一對一配置:使用<resultMap>做配置
一對多配置:使用<resultMap>+<collection>做配置
多對多配置:使用<resultMap>+<collection>做配置
2.Mybatis的注解開發(fā)
2.1 MyBatis的常用注解
這幾年來注解開發(fā)越來越流行名党,Mybatis也可以使用注解開發(fā)方式,這樣我們就可以減少編寫Mapper
映射文件了挠轴。我們先圍繞一些基本的CRUD來學(xué)習(xí)传睹,再學(xué)習(xí)復(fù)雜映射多表操作。
@Insert:實現(xiàn)新增
@Update:實現(xiàn)更新
@Delete:實現(xiàn)刪除
@Select:實現(xiàn)查詢
@Result:實現(xiàn)結(jié)果集封裝
@Results:可以與@Result 一起使用岸晦,封裝多個結(jié)果集
@One:實現(xiàn)一對一結(jié)果集封裝
@Many:實現(xiàn)一對多結(jié)果集封裝
2.2 MyBatis的增刪改查
我們完成簡單的user表的增刪改查的操作
privateUserMapperuserMapper;
?
@Before
publicvoidbefore()throwsIOException{
InputStreamresourceAsStream=Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactorysqlSessionFactory=new
SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSessionsqlSession=sqlSessionFactory.openSession(true);
userMapper=sqlSession.getMapper(UserMapper.class);
}
?
@Test
publicvoidtestAdd() {
Useruser=newUser();
user.setUsername("測試數(shù)據(jù)");
user.setPassword("123");
user.setBirthday(newDate());
userMapper.add(user);
}
@Test
publicvoidtestUpdate()throwsIOException{
Useruser=newUser();
user.setId(16);
user.setUsername("測試數(shù)據(jù)修改");
user.setPassword("abc");
user.setBirthday(newDate());
userMapper.update(user);
}
?
@Test
publicvoidtestDelete()throwsIOException{
userMapper.delete(16);
}
@Test
publicvoidtestFindById()throwsIOException{
Useruser=userMapper.findById(1);
System.out.println(user);
}
@Test
publicvoidtestFindAll()throwsIOException{
List<User>all=userMapper.findAll();
for(Useruser:all){
System.out.println(user);
?? }
}
?
修改MyBatis的核心配置文件欧啤,我們使用了注解替代的映射文件睛藻,所以我們只需要加載使用了注解的Mapper接口即可
<mappers>
<!--掃描使用注解的類-->
<mapperclass="com.itheima.mapper.UserMapper"></mapper>
</mappers>
或者指定掃描包含映射關(guān)系的接口所在的包也可以
<mappers>
<!--掃描使用注解的類所在的包-->
<packagename="com.itheima.mapper"></package>
</mappers>
2.3 MyBatis的注解實現(xiàn)復(fù)雜映射開發(fā)
實現(xiàn)復(fù)雜關(guān)系映射之前我們可以在映射文件中通過配置<resultMap>來實現(xiàn),使用注解開發(fā)后邢隧,我們可以使用@Results注解店印,@Result注解,@One注解倒慧,@Many注解組合完成復(fù)雜關(guān)系的配置
2.4 一對一查詢
2.4.1 一對一查詢的模型
用戶表和訂單表的關(guān)系為按摘,一個用戶有多個訂單,一個訂單只從屬于一個用戶
一對一查詢的需求:查詢一個訂單纫谅,與此同時查詢出該訂單所屬的用戶
2.4.2 一對一查詢的語句
對應(yīng)的sql語句:
select * from orders;
?
select * from user where id=查詢出訂單的uid;
查詢的結(jié)果如下:
2.4.3 創(chuàng)建Order和User實體
publicclassOrder{
?
privateintid;
privateDateordertime;
privatedoubletotal;
?
//代表當(dāng)前訂單從屬于哪一個客戶
privateUseruser;
}
?
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
privateDatebirthday;
?
}
2.4.4 創(chuàng)建OrderMapper接口
publicinterfaceOrderMapper{
List<Order>findAll();
}
2.4.5 使用注解配置Mapper
publicinterfaceOrderMapper{
@Select("select * from orders")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="ordertime",column="ordertime"),
@Result(property="total",column="total"),
@Result(property="user",column="uid",
javaType=User.class,
one=@One(select="com.itheima.mapper.UserMapper.findById"))
?? })
List<Order>findAll();
}
publicinterfaceUserMapper{
?
@Select("select * from user where id=#{id}")
UserfindById(intid);
}
2.4.6 測試結(jié)果
@Test
publicvoidtestSelectOrderAndUser() {
List<Order>all=orderMapper.findAll();
for(Orderorder:all){
System.out.println(order);
?? }
}
2.5 一對多查詢
2.5.1 一對多查詢的模型
用戶表和訂單表的關(guān)系為炫贤,一個用戶有多個訂單,一個訂單只從屬于一個用戶
一對多查詢的需求:查詢一個用戶付秕,與此同時查詢出該用戶具有的訂單
2.5.2 一對多查詢的語句
對應(yīng)的sql語句:
select * from user;
?
select * from orders where uid=查詢出用戶的id;
查詢的結(jié)果如下:
2.5.3 修改User實體
publicclassOrder{
?
privateintid;
privateDateordertime;
privatedoubletotal;
?
//代表當(dāng)前訂單從屬于哪一個客戶
privateUseruser;
}
?
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
privateDatebirthday;
//代表當(dāng)前用戶具備哪些訂單
privateList<Order>orderList;
}
2.5.4 創(chuàng)建UserMapper接口
List<User>findAllUserAndOrder();
2.5.5 使用注解配置Mapper
publicinterfaceUserMapper{
@Select("select * from user")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="username",column="username"),
@Result(property="password",column="password"),
@Result(property="birthday",column="birthday"),
@Result(property="orderList",column="id",
javaType=List.class,
many=@Many(select="com.itheima.mapper.OrderMapper.findByUid"))
?? })
List<User>findAllUserAndOrder();
}
?
publicinterfaceOrderMapper{
@Select("select * from orders where uid=#{uid}")
List<Order>findByUid(intuid);
?
}
2.5.6 測試結(jié)果
List<User>all=userMapper.findAllUserAndOrder();
for(Useruser:all){
System.out.println(user.getUsername());
List<Order>orderList=user.getOrderList();
for(Orderorder:orderList){
System.out.println(order);
?? }
System.out.println("-----------------------------");
}
2.6 多對多查詢
2.6.1 多對多查詢的模型
用戶表和角色表的關(guān)系為兰珍,一個用戶有多個角色,一個角色被多個用戶使用
多對多查詢的需求:查詢用戶同時查詢出該用戶的所有角色
2.6.2 多對多查詢的語句
對應(yīng)的sql語句:
select * from user;
?
select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=用戶的id
查詢的結(jié)果如下:
2.6.3 創(chuàng)建Role實體询吴,修改User實體
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
privateDatebirthday;
//代表當(dāng)前用戶具備哪些訂單
privateList<Order>orderList;
//代表當(dāng)前用戶具備哪些角色
privateList<Role>roleList;
}
?
publicclassRole{
?
privateintid;
privateStringrolename;
?
}
2.6.4 添加UserMapper接口方法
List<User>findAllUserAndRole();
2.6.5 使用注解配置Mapper
publicinterfaceUserMapper{
@Select("select * from user")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="username",column="username"),
@Result(property="password",column="password"),
@Result(property="birthday",column="birthday"),
@Result(property="roleList",column="id",
javaType=List.class,
many=@Many(select="com.itheima.mapper.RoleMapper.findByUid"))
})
List<User>findAllUserAndRole();}
?
?
?
publicinterfaceRoleMapper{
@Select("select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=#{uid}")
List<Role>findByUid(intuid);
}
?
2.6.6 測試結(jié)果
UserMappermapper=sqlSession.getMapper(UserMapper.class);
List<User>all=mapper.findAllUserAndRole();
for(Useruser:all){
System.out.println(user.getUsername());
List<Role>roleList=user.getRoleList();
for(Rolerole:roleList){
System.out.println(role);
?? }
System.out.println("----------------------------------");
}