关于标签association和collection相同点和不同点如图所示:
测试结果集
(一)准备阶段
(1)实体类(User)
package com.jyk.entity;
public class User {
private Integer id;
private String name;
private String password;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", address=" + address +
'}';
}
}
(2)实体类Address
package com.jyk.entity;
/**
* @Classname Address
* @Description TODO
* @Date 2020/7/11 10:50
* @Created by JYK
*/
public class Address {
private Integer addno;
private String city;
private String county;
private String town;
public Integer getAddno() {
return addno;
}
public void setAddno(Integer addno) {
this.addno = addno;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
@Override
public String toString() {
return "Address{" +
"addno=" + addno +
", city='" + city + '\'' +
", county='" + county + '\'' +
", town='" + town + '\'' +
'}';
}
}
(二)测试阶段(xml文件配置)
association
<resultMap id="userid" type="com.jyk.entity.User">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="password" column="password"></result>
<association property="address" javaType="com.jyk.entity.Address" >
<id property="addno" column="addno"></id>
<result property="city" column="city"></result>
<result property="county" column="country"></result>
<result property="town" column="town"></result>
</association>
</resultMap>
<select id="selectById" resultMap="userid">
select * from user left join address on address.addno=user.id where id=#{id}
</select>
collection
<resultMap id="userid" type="com.jyk.entity.User">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="password" column="password"></result>
<collection property="address" ofType="com.jyk.entity.Address">
<id property="addno" column="addno"></id>
<result property="city" column="city"></result>
<result property="town" column="town"></result>
<result property="county" column="county"></result>
</collection>
</resultMap>
<select id="selectById" resultMap="userid">
select * from user left join address on user.id=address.addno where id=#{id}
</select>
association测试结果
collection测试结果
因此得出结论:一些场景下,association和collection两者都可以满足需求,达到关联查询的效果,使用两者任意一个标签都可以,具体情况具体分析,以需求为导向实现相应需求。使用任何标签其实都无所谓,只要能满足需求即可。
(四)不同点案例及测试
collection属性值包含:
association标签属性值包含:
因此ofType成为,ofType为collection传入的类型,javaType为返回值类型,例如下图,ofType=Address(完全限定名),javaType=List(完全限定名)。
实体类对象:表示一个人可以有多个收货地址
BaseDao.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="com.jyk.dao.BaseDao">
<resultMap id="userid" type="com.jyk.entity.User">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="password" column="password"></result>
<!--另外一个dao层的接口-->
<collection property="addresses" ofType="com.jyk.entity.Address" column="id" javaType="java.util.List" fetchType="lazy" select="com.jyk.dao.AddressDao.selectaddId">
</collection>
</resultMap>
<select id="selectById" resultMap="userid">
select * from user where id=#{id}
</select>
<select id="selectAll" resultType="com.jyk.entity.User" >
select * from test
</select>
<insert id="insert" parameterType="com.jyk.entity.User" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into user(name,password) values (#{name},#{password})
</insert>
<update id="update" parameterType="com.jyk.entity.User" useGeneratedKeys="true" keyProperty="id">
update user set name=#{name},password=#{password} where id=#{id}
</update>
<!-- <delete id="delete" >-->
<!-- delete from test where id=#{id} and name=#{name}-->
<!-- </delete>-->
</mapper>
AddressDao.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="com.jyk.dao.AddressDao">
<select id="selectaddId" resultType="com.jyk.entity.Address">
select * from address where addno=#{addno}
</select>
</mapper>
最终实验结果:
总结:
出错点:
(1).xml的文件和dao层接口的文件名称需要一致,在mybatis-config.xml中使用的是package扫描。这一块需要注意
算法题:
(1)求一个数中二进制1的个数
思路:使用N&(~(N)+1)求最右侧为1的位数
public class Demo {
public static void main(String[] args) {
int count = getCount(15);
System.out.println(count);
}
public static int getCount(int N){
int count=0;
while (N!=0){
count++;
N^=(N&((~N)+1));
}
return count;
}
}
(2)一个数组中,只有一个数出现奇数,其他数都是偶数,求此数为多少?(思路:异或)
public int number(int[]arr){
int result = 0;
for(int i=0;i<arr.length;i++){
result^=arr[i];
}
return result;
}