1. 程式人生 > 其它 >Netty簡介以及入門使用

Netty簡介以及入門使用

技術標籤:mybatisjavamybatis

Mybatis中,自定義resultMap,實現高階結果集對映

方法一

1、介面中定義方法

public Employee getEmpById(Integer id);

2、xml配置

<select id="getEmpById" resultType="com.fenga.mybatis.bean.Employee">
		 select * from tbl_employee where id = #{id}
	 </select>

3、測試方法

@Test
	public void test1() throws IOException {
		//1.獲取sqlSessionFactory物件
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		
		//2.獲取SQLSession物件
		SqlSession openSession = sqlSessionFactory.openSession();
		
		try {
		//3.獲取介面實現類物件
		//會為介面自動的建立一個代理物件,代理物件會執行增刪改查方法
		EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
		
		Employee employee = mapper.getEmpById(1);
		
		System.out.println(employee);
		
		}finally {
			openSession.close();
		}
		
	}

注意:如果結果出現lastName=null,可能是沒開啟駝峰命名法,在全域性配置中配置:

<settings>
	 	<setting name="mapUnderscoreToCamelCase" value="true"/>
	 </settings>

方法二

第一步同上

2、xml配置:

<!-- 自定義某個javaBean的封裝規則
	 		type:自定義規則的Java型別
	 		id:唯一id方便引用		
	  -->
	 <resultMap type="com.fenga.mybatis.bean.Employee" id="MyEmp">
	 	<!-- 指定主鍵列的封裝規則
	 			id定義主鍵會底層有優化
	 			column:指定哪一列
	 			property:指定對應的javaBean屬性
	 	 -->
	 	<id column="id" property="id"/>
	 	<!-- 定義普通列的封裝規則 -->
	 	<result column="last_name" property="lastName"/>
	 	<!-- 其他不指定的列會自動封裝,我們只要寫上resultMap就把全部的對映規則都寫上 -->
	 	<result column="email" property="email"/>
	 	<result column="gender" property="gender"/>
	 </resultMap>
	 
	 <select id="getEmpById" resultMap="MyEmp">
		 select * from tbl_employee where id = #{id}
	 </select>

3、測試程式碼同上

4、效果