JAVA日記之mybatis-3一對一,一對多,多對多xml與註解配置

1.Mybatis多表查詢
1.1 一對一查詢
1.1.1 一對一查詢的模型
用戶表和訂單表的關係爲,一個用戶有多個訂單,一個訂單隻從屬於一個用戶


java

一對一查詢的需求:查詢一個訂單,與此同時查詢出該訂單所屬的用戶sql

 

1.1.2一對一查詢的語句
對應的sql語句:select * from orders o,user u where o.uid=u.id;
app

查詢的結果以下:dom

 

1.1.3 建立Order和User實體學習

public class Order {測試

private int id;
private Date ordertime;
private double total;

ui

//表明當前訂單從屬於哪個客戶
private User user;
}

spa

public class User {

private int id;
private String username;
private String password;
private Date birthday;




3d

}code

1.1.4 建立OrderMapper接口
public interface OrderMapper { List<Order> findAll(); }


1.1.5 配置OrderMapper.xml

<mapper namespace="com.itheima.mapper.OrderMapper">
<resultMap id="orderMap" type="com.itheima.domain.Order">
<result column="uid" property="user.id"></result>
<result column="username" property="user.username"></result>
<result column="password" property="user.password"></result>
<result column="birthday" property="user.birthday"></result>
</resultMap>
<select id="findAll" resultMap="orderMap">
select * from orders o,user u where o.uid=u.id
</select>
</mapper>









其中還能夠配置以下:

<resultMap id="orderMap" type="com.itheima.domain.Order">
<result property="id" column="id"></result>
<result property="ordertime" column="ordertime"></result>
<result property="total" column="total"></result>
<association property="user" javaType="com.itheima.domain.User">
<result column="uid" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>









1.1.6 測試結果

OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
List<Order> all = mapper.findAll();
for(Order order : all){
System.out.println(order);
}





 

 

 

1.2 一對多查詢

1.2.1 一對多查詢的模型

用戶表和訂單表的關係爲,一個用戶有多個訂單,一個訂單隻從屬於一個用戶

一對多查詢的需求:查詢一個用戶,與此同時查詢出該用戶具備的訂單

 
 

 

 

 

1.2.2 一對多查詢的語句

對應的sql語句:select *,o.id oid from user u left join orders o on u.id=o.uid;

查詢的結果以下:

 

 

 

1.2.3 修改User實體

public class Order {

private int id;
private Date ordertime;
private double total;

//表明當前訂單從屬於哪個客戶
private User user;
}

public class User {

private int id;
private String username;
private String password;
private Date birthday;
//表明當前用戶具有哪些訂單
private List<Order> orderList;
}







1.2.4 建立UserMapper接口
public interface UserMapper { List<User> findAll(); } 

 

1.2.5 配置UserMapper.xml

<mapper namespace="com.itheima.mapper.UserMapper">
<resultMap id="userMap" type="com.itheima.domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="orderList" ofType="com.itheima.domain.Order">
<result column="oid" property="id"></result>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
select *,o.id oid from user u left join orders o on u.id=o.uid
</select>
</mapper>














1.2.6 測試結果

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
for(User user : all){
System.out.println(user.getUsername());
List<Order> orderList = user.getOrderList();
for(Order order : orderList){
System.out.println(order);
}
System.out.println("----------------------------------");
}











 

 

 

1.3 多對多查詢

1.3.1 多對多查詢的模型

用戶表和角色表的關係爲,一個用戶有多個角色,一個角色被多個用戶使用

多對多查詢的需求:查詢用戶同時查詢出該用戶的全部角色

 

1.3.2 多對多查詢的語句

對應的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;

查詢的結果以下:



 

1.3.3 建立Role實體,修改User實體

public class User {
private int id;
private String username;
private String password;
private Date birthday;
//表明當前用戶具有哪些訂單
private List<Order> orderList;
//表明當前用戶具有哪些角色
private List<Role> roleList;
}








public class Role {

private int id;
private String rolename;

}

1.3.4 添加UserMapper接口方法

List<User> findAllUserAndRole();

1.3.5 配置UserMapper.xml

 

<resultMap id="userRoleMap" type="com.itheima.domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="roleList" ofType="com.itheima.domain.Role">
<result column="rid" property="id"></result>
<result column="rolename" property="rolename"></result>
</collection>
</resultMap>
<select id="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 測試結果

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAllUserAndRole();
for(User user : all){
System.out.println(user.getUsername());
List<Role> roleList = user.getRoleList();
for(Role role : roleList){
System.out.println(role);
}
System.out.println("----------------------------------");
}










 

 

 

.4 知識小結
MyBatis多表配置方式:

一對一配置:使用作配置

一對多配置:使用+作配置

多對多配置:使用+作配置

2.Mybatis的註解開發
2.1 MyBatis的經常使用註解
這幾年來註解開發愈來愈流行,Mybatis也可使用註解開發方式,這樣咱們就能夠減小編寫Mapper

映射文件了。咱們先圍繞一些基本的CRUD來學習,再學習複雜映射多表操做。

@Insert:實現新增

@Update:實現更新

@Delete:實現刪除

@Select:實現查詢

@Result:實現結果集封裝

@Results:能夠與@Result 一塊兒使用,封裝多個結果集

@One:實現一對一結果集封裝

@Many:實現一對多結果集封裝

2.2 MyBatis的增刪改查
咱們完成簡單的user表的增刪改查的操做

private UserMapper userMapper;

@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
userMapper = sqlSession.getMapper(UserMapper.class);
}






@Test
public void testAdd() {
User user = new User();
user.setUsername("測試數據");
user.setPassword("123");
user.setBirthday(new Date());
userMapper.add(user);
}
@Test
public void testUpdate() throws IOException {
User user = new User();
user.setId(16);
user.setUsername("測試數據修改");
user.setPassword("abc");
user.setBirthday(new Date());
userMapper.update(user);
}















@Test
public void testDelete() throws IOException {
userMapper.delete(16);
}
@Test
public void testFindById() throws IOException {
User user = userMapper.findById(1);
System.out.println(user);
}
@Test
public void testFindAll() throws IOException {
List<User> all = userMapper.findAll();
for(User user : all){
System.out.println(user);
}
}














修改MyBatis的核心配置文件,咱們使用了註解替代的映射文件,因此咱們只須要加載使用了註解的Mapper接口便可

 
 

 <mappers> <!--掃描使用註解的類--> <mapper class="com.itheima.mapper.UserMapper"></mapper> </mappers>

或者指定掃描包含映射關係的接口所在的包也能夠

<mappers> <!--掃描使用註解的類所在的包--> <package name="com.itheima.mapper"></package> </mappers>

2.3 MyBatis的註解實現複雜映射開發

實現複雜關係映射以前咱們能夠在映射文件中經過配置來實現,使用註解開發後,咱們可使用@Results註解,@Result註解,@One註解,@Many註解組合完成複雜關係的配置

 

 

 

 

 

2.4 一對一查詢

2.4.1 一對一查詢的模型

用戶表和訂單表的關係爲,一個用戶有多個訂單,一個訂單隻從屬於一個用戶

一對一查詢的需求:查詢一個訂單,與此同時查詢出該訂單所屬的用戶

[外鏈圖片轉存失敗(img-wmckNwf4-1562073026161)(img\圖片12.png)]

 

2.4.2 一對一查詢的語句

對應的sql語句:

select * from orders; select * from user where id=查詢出訂單的uid;

查詢的結果以下:

 

 

2.4.3 建立Order和User實體

 

public class Order {

private int id;
private Date ordertime;
private double total;

//表明當前訂單從屬於哪個客戶
private User user;
}

public class User {

private int id;
private String username;
private String password;
private Date birthday;




}

2.4.4 建立OrderMapper接口
public interface OrderMapper { List<Order> findAll(); }


2.4.5 使用註解配置Mapper

public interface OrderMapper {
@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();
}public interface UserMapper { @Select("select * from user where id=#{id}") User findById(int id); }










2.4.6 測試結果

@Test
public void testSelectOrderAndUser() {
List<Order> all = orderMapper.findAll();
for(Order order : all){
System.out.println(order);
}
}





 

 

 

2.5 一對多查詢

2.5.1 一對多查詢的模型

用戶表和訂單表的關係爲,一個用戶有多個訂單,一個訂單隻從屬於一個用戶

一對多查詢的需求:查詢一個用戶,與此同時查詢出該用戶具備的訂單

[外鏈圖片轉存失敗(img-aaaElVnT-1562073026164)(img\圖片15.png)]

2.5.2 一對多查詢的語句

對應的sql語句:

select * from user;

select * from orders where uid=查詢出用戶的id;

 

 

查詢的結果以下:

[外鏈圖片轉存失敗(img-8eGJNkVv-1562073026165)(img\圖片16.png)]

 

2.5.3 修改User實體

public class Order {

private int id;
private Date ordertime;
private double total;

//表明當前訂單從屬於哪個客戶
private User user;
}

public class User {

private int id;
private String username;
private String password;
private Date birthday;
//表明當前用戶具有哪些訂單
private List<Order> orderList;
}







2.5.4 建立UserMapper接口

List<User> findAllUserAndOrder();

2.5.5 使用註解配置Mapper

public interface UserMapper {
@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();
}











public interface OrderMapper {
@Select("select * from orders where uid=#{uid}")
List<Order> findByUid(int uid);

}

2.5.6 測試結果

List<User> all = userMapper.findAllUserAndOrder();
for(User user : all){
System.out.println(user.getUsername());
List<Order> orderList = user.getOrderList();
for(Order order : orderList){
System.out.println(order);
}
System.out.println("-----------------------------");
}









 

 

2.6 多對多查詢

2.6.1 多對多查詢的模型

用戶表和角色表的關係爲,一個用戶有多個角色,一個角色被多個用戶使用

多對多查詢的需求:查詢用戶同時查詢出該用戶的全部角色

[外鏈圖片轉存失敗(img-JM72Dl4Q-1562073026166)(img\圖片18.png)]

2.6.2 多對多查詢的語句

對應的sql語句:

 
 
select * from user; select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=用戶的id

查詢的結果以下:

 
 

 

 

 

2.6.3 建立Role實體,修改User實體
 
 

public class User {
private int id;
private String username;
private String password;
private Date birthday;
//表明當前用戶具有哪些訂單
private List<Order> orderList;
//表明當前用戶具有哪些角色
private List<Role> roleList;
}








 
 

public class Role {

 
 

private int id;
private String rolename;

 
 

}

public class User {
private int id;
private String username;
private String password;
private Date birthday;
//表明當前用戶具有哪些訂單
private List<Order> orderList;
//表明當前用戶具有哪些角色
private List<Role> roleList;
}








public class Role {

private int id;
private String rolename;

}

2.6.5 使用註解配置Mapper

public interface UserMapper {
@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();}










 

public interface RoleMapper {
@Select("select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=#{uid}")
List<Role> findByUid(int uid);
}


2.6.6 測試結果

UserMapper mapper = sqlSession.getMapper(UserMapper.class);

List<User> all = mapper.findAllUserAndRole();
for(User user : all){
System.out.println(user.getUsername());
List<Role> roleList = user.getRoleList();
for(Role role : roleList){
System.out.println(role);
}
System.out.println("----------------------------------");
}

相關文章
相關標籤/搜索