Database record small problem

  1. What is the role of foreign keys, foreign keys added to the other side of two related tables.
    Effect: maintain data consistency, integrity, the main purpose of the control data stored in the external key table. Association is formed so that two tables, the foreign key values or reference only nulls in the outer columns .
    Which the foreign key in a table: divided into the following situations:
  • First, the foreign key columns referenced in the primary table must be the primary key column or unique column.
  • So 1: n affirmed the foreign key based on n of that table.
  • 1: 1, usually to see who is the master table, who is attached tables, foreign key, of course, to establish a subsidiary in the table.
  • n: where m, it is necessary to establish a relationship table, the original two tables and relationships are 1: n, 1: m
    Examples of (many):
    role table
    Here Insert Picture Description
    user table
    Here Insert Picture Description
    Information Table (foreign key provided here)
    Here Insert Picture Description

Foreign key on the contact list.

  1. many-nested query mybatis's
    role: When using mybatis, when we encounter a link between the table and the table, you can use a nested query
    example or the use of the above:
public class UserInfo {
    private String uid;

    private String username;

    private String name;

    private String password;

    private String salt;

    private Integer state;
	//新建一个roles,用来接受联系表的数据
    private List<SysUserRoleKey> roles;

    public List<SysUserRoleKey> getRoles() {
        return roles;
    }

    public void setRoles(List<SysUserRoleKey> roles) {
        this.roles = roles;
    }
 }
public class SysUserRoleKey {
    private String roleId;

    private String uid;
	//与role的类是一对一的关系
    private SysRole role;

    private UserInfo userInfos;
}

resultMap

<resultMap id="UserAndRole" type="com.example.demo.Model.UserInfo">
    <id column="uid" jdbcType="VARCHAR" property="uid" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="salt" jdbcType="VARCHAR" property="salt" />
    <result column="state" jdbcType="INTEGER" property="state" />
    <collection property="roles" ofType="com.example.demo.Model.SysUserRoleKey">
      <id column="role_id" jdbcType="VARCHAR" property="roleId" />
      <id column="uid" jdbcType="VARCHAR" property="uid" />
      <association property="role" javaType="com.example.demo.Model.SysRole">
        <id column="role_id" jdbcType="VARCHAR" property="roleId" />
        <result column="available" jdbcType="INTEGER" property="available" />
        <result column="description" jdbcType="VARCHAR" property="description" />
        <result column="role" jdbcType="VARCHAR" property="role" />
      </association>
    </collection>
  </resultMap>

check sentence

</select>
  <select id="selectUserRole" resultMap="UserAndRole" parameterType="java.lang.String">
    select sr.role_id ,sr.available,sr.description,sr.role,各种你需要的属性
    FROM user_info  u
    left join  sys_user_role  sur ON u.uid=sur.uid
    left join sys_role  sr ON sur.role_id=sr.role_id
  </select>
Published 36 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/s_xchenzejian/article/details/97039257