标签:步骤 namespace one pac 表之间 cst verilog oftype account
目录
特例:
? 如果拿出每一个订单,他都只能属于一个用户
? 所以 Mybatis 就把多对一看成了一对一
实体类参数
private Integer id;
private Integer uid;
private double money;
private User user;
xml 对应
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.IAccountDao">
<!--定义封装account和user的resultMap-->
<resultMap id="accountUserMap" type="account">
<id property="id" column="aid"/>
<result property="uid" column="uid"/>
<result property="money" column="money"/>
<!--一对一的关系映射,配置封装user的内容-->
<!--javaType 告诉下面那个封装到那个对象,因为有别名,所以直接用 user-->
<association property="user" column="uid" javaType="user">
<id property="id" column="id" />
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
</association>
</resultMap>
<select id="findAll" resultMap="accountUserMap">
select u.*,a.id as aid,a.uid,a.money from account a,user u where u.id=a.id;
</select>
</mapper>
一个用户可拥有多个账户
private Integer id;
private String username;
private String password;
private Date birthday;
private String address;
//一对多关系映射,主表实体应该包括从表实体的集合引用
private List<Account> accounts;
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.IUserDao">
<!--定义user的resultMap-->
<resultMap id="userAccountMap" type="user">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
<!--配置 user对象中 accounts 集合的映射-->
<!--ofType 集合中元素类型 account 是别名-->
<collection property="accounts" ofType="account">
<!--column 起个别名-->
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
</collection>
</resultMap>
<!--不管有没有账户,用户的信息都得有,所以不能用内连接,所以用左外连接,会返回左表的所有数据-->
<select id="findAll" resultMap="userAccountMap">
select *from user u left outer join account a on u.id=a.uid;
</select>
</mapper>
private Integer roleId;
private String roleName;
private String roleDesc;
/*多对多的关系映射,一个角色可以赋予多个用户*/
private List<User> users;
<mapper namespace="dao.IRoleDao">
<!--定义 role 表的 ResultMao-->
<resultMap id="roleMap" type="role">
<id property="roleId" column="rid"/>
<result property="roleName" column="role_name"/>
<result property="roleDesc" column="role_desc"/>
<collection property="users" ofType="user">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
</collection>
</resultMap>
<select id="findAll" resultMap="roleMap">
select u.*,r.id as rid,r.role_name,r.role_desc from role r
left outer join user_role ur on r.id =ur.rid
left outer join user u on u.id =ur.uid
</select>
</mapper>
-----------每个角色信息-------------
Role{roleId=1, roleName='院长', roleDesc='学员管理者'}
[User{id=1, username='龙族', password='123', birthday=Mon Oct 12 00:00:00 CST 1998, address='浏阳'}, User{id=3, username='罪人', password='123', birthday=Tue Jul 23 00:00:00 CST 2019, address='堕落之地'}]
-----------每个角色信息-------------
Role{roleId=2, roleName='CEO', roleDesc='首席执行官'}
[User{id=1, username='龙族', password='123', birthday=Mon Oct 12 00:00:00 CST 1998, address='浏阳'}, User{id=2, username='信守斩龙', password='123', birthday=Mon Oct 12 00:00:00 CST 1998, address='浏阳'}]
-----------每个角色信息-------------
Role{roleId=3, roleName='暗部', roleDesc='光明下的黑暗组织'}
[User{id=2, username='信守斩龙', password='123', birthday=Mon Oct 12 00:00:00 CST 1998, address='浏阳'}]
标签:步骤 namespace one pac 表之间 cst verilog oftype account
原文地址:https://www.cnblogs.com/zuiren/p/11406128.html