数据库中:USERT t , WEBINFOR t(表中有一列关联usert某列)

建模型,分析好哪个表是一对一(webinfo ),哪个表是一对多(usert)(一条数据对另一个表中多条数据).

对象级联
建包

建xml和interface接口类

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">
对象级联 方法查询 结果是从一对一这个表查看
WebinfoMapper.xml
<mapper namespace="com.hanqi.mapper.WebInfoMapper">//复制的对应的接口里面的限定名
<resultMap type="webInfo" id="webinfoResult2">
<id property="ids" column="IDS"/>
<association property="usert" column="USERID" select="com.hanqi.mapper.UsertMapper.selectUsertById" />
</resultMap>
<select id="selectWebInfo" resultMap="webinfoResult2">
select * from webinfor //注意表名别写错了
</select>
</mapper>
WebInfoMapper.java
package com.hanqi.mapper;
import java.util.List;
import com.hanqi.model.webInfo;
public interface WebInfoMapper {
List<webInfo> selectWebInfo();
}
UsertMapper.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.hanqi.mapper.UsertMapper">
<select id="selectUsertById" resultType="Usert" >
select * from usert u where u.ids=#{userid}
</select>
</mapper>
UsertMapper.java
package com.hanqi.mapper;
import java.util.List;
import com.hanqi.model.Usert;
public interface UsertMapper {
List<Usert> selectUsertById(); //返回值只在想打印时有用,否则可以写int型
}
test表
package com.hanqi.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.hanqi.mapper.WebInfoMapper;
import com.hanqi.model.webInfo;
import com.hanqi.util.MyBatisDButil;
class JTest {
private SqlSession sqlSession;
private WebInfoMapper webinfoMapper;
@Test
void test() {
List<webInfo> list = webinfoMapper.selectWebInfo();
for (webInfo webInfo : list) {
System.out.println(webInfo);
}
}
@BeforeEach
void setUp() throws Exception {
sqlSession = MyBatisDButil.getSqlSession();
webinfoMapper = sqlSession.getMapper(WebInfoMapper.class);
}
@AfterEach
void tearDown() throws Exception {
sqlSession.commit();
sqlSession.close();
}
}