<typeAlias alias="user" type="com.wodwl.User" />
<!-- 使用resultMap --> <resultMap class="user" id="resultMap1"> <result property="id" column="id" javaType="integer" jdbcType="int" /> <result property="name" column="xingming" javaType="string" jdbcType="VARCHAR" /> <result property="sex" column="xingbie" javaType="integer" jdbcType="int" /> </resultMap> <select id="select2" resultMap="resultMap1" parameterClass="integer"> <![CDATA[ SELECT id,xingming,xingbie FROM user WHERE id=#id# ]]> </select>
public static void select2() throws IOException, SQLException{ String resource="com/wodwl/map/SqlMapConfig.xml"; Reader reader=Resources.getResourceAsReader(resource); SqlMapClient smc=SqlMapClientBuilder.buildSqlMapClient(reader); smc.startTransaction(); User user=(User)smc.queryForObject("user.select2",new Integer(1)); smc.commitTransaction(); System.out.println(user.getName()); }
使用select as 实现字段名和属性名的映射
<select id="select1" resultClass="java.util.HashMap" parameterClass="integer"> SELECT id,xingming as name,xingbie as sex FROM user WHERE id=#id# </select>