• Mybatis实例


    数据库中: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();
    	}
    }
    

      

     

     

  • 相关阅读:
    Sogou C++ Workflow 安装与使用例子
    Ubuntu c++ 使用mysql++ 链接mysql 使用cmake 构建
    现代cmake 从github引入三方库,使用FetchContent ( 3.14 以上版本)
    Vue3 + TypeScript 开发实践总结
    Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)
    Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)
    Spring MVC 学习总结(八)——Spring MVC概要与环境配置(IDEA+Maven+Tomcat7+JDK8、示例与视频)
    Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成
    Spring MVC 学习总结(五)——校验与文件上传
    Spring MVC 学习总结(四)——视图与综合示例
  • 原文地址:https://www.cnblogs.com/ziyanxiaozhu/p/8358754.html
Copyright © 2020-2023  润新知