第一种解决方案 别名, 在 sql语句中添加as关键字设置列别名
利用sqlyog数据库管理工具的语句对表粘贴sql语句功能
对数据表点击右键->粘贴sql语句->
`book``ay_user`SELECT * FROM board LIMIT 1;`articles``admin``tb_person_info``tb_person_info` SELECT `shop_id`, `owner_id`, `area_id`, `shop_category_id`, `shop_name`, `shop_desc`, `shop_addr`, `phone`, `shop_img`, `priority`, `create_time`, `last_edit_time`, `enable_status`, `advice` FROM `oto`.`tb_shop` LIMIT 0, 1000 ;
在配置文件中使用上面的内容,而不需要自己重新手写
<select id="getShopByIdAlias" parameterType="int" resultType="Shop" > SELECT `shop_id` as `id`, `owner_id` as `ownerId`, `area_id` as `areaId`, `shop_category_id` as `categoryId`, `shop_name` as `name`, `shop_desc` as `desc`, `shop_addr` as `addr`, `phone`, `shop_img` as `image`, `priority` , `create_time` as `createTime`, `last_edit_time` as `lastEditTime`, `enable_status` as `enableStatus`, `advice` from tb_shop where `shop_id` = #{id} </select>
第二种解决方案就是使用resultMap标签,
type是resultMap标签的返回类型
<resultMap id="simpleResultMap" type="Shop"> <id column="shop_id" property="id" ></id> <result column="owner_id" property="ownerId" ></result> <result column="area_id" property="areaId" ></result> <result column="shop_category_id" property="categoryId" ></result> <result column="shop_name" property="name"></result> <result column="shop_desc" property="desc"></result> <result column="shop_addr" property="addr"></result> <result column="phone" property="phone"></result> <result column="shop_img" property="image"></result> <result column="priority" property="priority"></result> <result column="create_time" property="createTime"></result> <result column="last_edit_time" property="lastEditTime"></result> <result column="enable_status" property="enableStatus"></result> </resultMap>
分别编写接口
public interface ShopMapper { Shop getShopById(Integer id); Shop getShopByIdAlias(Integer id); }
测试使用
public class ShopMapperTest { @Test public void testGetShopById() { SqlSession session = MyBatisUtil.getSqlSession(); ShopMapper mapper = session.getMapper(ShopMapper.class); System.out.println(mapper.getShopById(1)); session.close(); } @Test public void testGetShopByIdAlias() { SqlSession session = MyBatisUtil.getSqlSession(); ShopMapper mapper = session.getMapper(ShopMapper.class); System.out.println(mapper.getShopByIdAlias(1)); session.close(); } }
转载于:https://www.cnblogs.com/litran/p/10543431.html