1. 程式人生 > >MyBatis 返回值 摘自 https://blog.csdn.net/kangsenkangsen/article/details/51236279

MyBatis 返回值 摘自 https://blog.csdn.net/kangsenkangsen/article/details/51236279

關於mybatis返回單一物件或物件列表的問題
一.說明
返回資料型別由dao中的介面和map.xml檔案共同決定。另外,不論是返回單一物件還是物件列表,***map.xml中的配置都是一樣的,都是resultMap=”***Map”或resultType=“* .* .*”型別.

每一次mybatis從資料庫中select資料之後,都會檢查資料條數和dao中定義的返回值是否匹配。

若返回一條資料,dao中定義的返回值是一個物件或物件的List列表,則可以正常匹配,將查詢的資料按照dao中定義的返回值存放。

若返回多條資料,dao中定義的返回值是一個物件,則無法將多條資料對映為一個物件,此時mybatis報錯。

 

二.程式碼測試
UserMap.xml對映檔案:
<resultMap id="BaseResultMap" type="com.ks.ssm.domain.User" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="qq" property="qq" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="BIT" />
<result column="birthday" property="birthday" jdbcType="DATE" />
<result column="city" property="city" jdbcType="VARCHAR" />
<result column="mood" property="mood" jdbcType="VARCHAR" />
<result column="single" property="single" jdbcType="BIT" />
<result column="enrolltime" property="enrolltime" jdbcType="TIMESTAMP" />
<result column="level" property="level" jdbcType="TINYINT" />
<result column="status" property="status" jdbcType="BIT" />
<result column="titlepic" property="titlepic" jdbcType="VARCHAR" />
<result column="job" property="job" jdbcType="VARCHAR" />
<result column="logintime" property="logintime" jdbcType="TIMESTAMP" />
<result column="loginip" property="loginip" jdbcType="VARCHAR" />
<result column="token" property="token" jdbcType="VARCHAR" />
<result column="modifytime" property="modifytime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, username, password, email, qq, phone, gender, birthday, city, mood, single, enrolltime,
level, status, titlepic, job, logintime, loginip, token, modifytime
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from user_info
where id = #{id,jdbcType=BIGINT}
</select>
<!-- add by ks -->
<select id="selectByUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from user_info
where username = #{username,jdbcType=VARCHAR}
</select>


<!-- mybatis 非常的智慧,返回值統一使用 resultMap="BaseResultMap",mybatis會根據查詢到的條目數量自動進行判斷,如果是一條就返回物件,如果是多條就返回List物件列表-->
<select id="selectByEmail" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from user_info
where email = #{email,jdbcType=VARCHAR}
</select>


 

 

public interface UserMapper {

User selectByPrimaryKey(Long id);

User selectByUserName(String username );

/**關於mybatis返回單一物件或物件列表的問題:
* 1.返回資料型別由dao中的介面和*map.xml檔案共同決定。另外,不論是返回單一物件還是物件列表,*map.xml中的配置都是一樣的,都是resultMap="*Map"*或resultType=“* .* .*”型別.
* 2.每一次mybatis從資料庫中select資料之後,都會檢查資料條數和dao中定義的返回值是否匹配。
* 3.若返回一條資料,dao中定義的返回值是一個物件或物件的List列表,則可以正常匹配,將查詢的資料按照dao中定義的返回值存放。
* 4.若返回多條資料,dao中定義的返回值是一個物件,則無法將多條資料對映為一個物件,此時mybatis報錯。
* */
List<User> selectByEmail(String email );
}

 

 

 

@RunWith(SpringJUnit4ClassRunner.class) //表示繼承了SpringJUnit4ClassRunner類
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})

public class TestMyBatis {
private static Logger logger = Logger.getLogger(TestMyBatis.class);
@Resource
private UserMapper userDao;
@Test
public void testMybatis() {
User user = userDao.selectByUserName("ks");
logger.info("user.........................");
logger.info(JSON.toJSONString(user));
List<User> users=userDao.selectByEmail("ks");
logger.info("list.........................");
for(User userTemp : users)
{
logger.info(JSON.toJSONString(userTemp));
}
}
}

 

再附上鍊接啊

https://blog.csdn.net/u010003835/article/details/78813155