码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis实例

时间:2018-01-26 12:47:16      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:技术   set   3.0   数据库   rtb   res   except   实例   apach   

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

	

}

  

  

 

 

 

Mybatis实例

标签:技术   set   3.0   数据库   rtb   res   except   实例   apach   

原文地址:https://www.cnblogs.com/ziyanxiaozhu/p/8358754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!