数据库中: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)查看
对象级联 方法查询
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(); } }
第二种方式:结果是从一对多这个表(Usert)查看
UsertMapper.java
package com.hanqi.mapper; import java.util.List; import com.hanqi.model.Usert; public interface UsertMapper { //Usert selectUsertById(Integer ids); 查某一个人的详细数据时 List<Usert> selectUsertById(); /*查所有人的详细数据时, 这两种方式的差别主要是打印的时候,想看几个人的数据还是所有人的数据*/ }
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"> <resultMap type="Usert" id="usertResult"> <id property="ids" column="IDS"/> <collection property="webinfoList" column="IDS" select="com.hanqi.mapper.WebInfoMapper.selectWebInfoByUsert" /> </resultMap> <select id="selectUsertById" resultMap="usertResult"> select * from usert u where u.ids=#{userid} </select> </mapper>
WebInfoMapper .java
package com.hanqi.mapper; import java.util.List; import com.hanqi.model.webInfo; public interface WebInfoMapper { List<webInfo> selectWebInfoByUsert(Integer ids); }
WebInfoMapper .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.WebInfoMapper"> <select id="selectWebInfoByUsert" resultType="webInfo"> select * from webinfor w where w.userid=#{ids} </select> </mapper>
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.UsertMapper; import com.hanqi.mapper.WebInfoMapper; import com.hanqi.model.webInfo; import com.hanqi.util.MyBatisDButil; class JTest { private SqlSession sqlSession; private WebInfoMapper webinfoMapper; private UsertMapper usertmapper; //private GirlMapper gm; @Test void test() { /*1对多第三种方法 * List<Usert> list = usertmapper.selectUsertById(); for (Usert usert : list) { System.out.println(usert); }*/ } @BeforeEach void setUp() throws Exception { sqlSession = MyBatisDButil.getSqlSession(); webinfoMapper = sqlSession.getMapper(WebInfoMapper.class); usertmapper = sqlSession.getMapper(UsertMapper.class); //gm = sqlSession.getMapper(GirlMapper.class); } @AfterEach void tearDown() throws Exception { sqlSession.commit(); sqlSession.close(); } }
来源:https://www.cnblogs.com/ziyanxiaozhu/p/8358754.html