首页 > 代码库 > mybatis typeAlias(别名)使用和resultMap使用
mybatis typeAlias(别名)使用和resultMap使用
1、MyBatis提供的typeAlias
2、定义自定义类型的别名
定义自定义类型的别名
1.在mybatis-config.xml中增加
1 <typeAliases> 2 <typeAlias alias="user" type="cn.itcast.pojo.User"/> 3 </typeAliases>
2.在UserMapper.xml中使用别名 resultType="user"
1 <select id="loadUserById" parameterType="integer" resultType="user"> 2 select * from user where id = #{id} 3 </select>
3、resultMap使用
1.创建person表
1 create table person(id int primary key,user_name varchar(50),age int,address varchar(150)); 2 desc person; 3 insert into person values(1,’laozhao’,35,’haidian’);
2.创建person.java
1 public class Person {
2 3 private Integer id; 5 private String userName; 7 private Integer age; 9 private String address; 10 11 set get ... 12 13 }
3.创建PersonMapper.xml
1 <mapper namespace="cn.itcast.dao.PersonMapper">
2 <select id="loadPersonById" parameterType="integer" resultType="person"> 3 select * from person where id = #{id} 4 </select>
5 </mapper>
4.修改mybatis-config.xml,增加person相关信息
1 <typeAliases> 2 <typeAlias alias="person" type="cn.itcast.pojo.Person"/> 3 </typeAliases> 4 <mapper resource="cn/itcast/dao/PersonMapper.xml"/>
5.创建测试类TestPerson
1 @Test 2 public void testloadPersonById(){ 3 4 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); 5 Person person = sqlSession.selectOne("cn.itcast.dao.PersonMapper.loadPersonById", 1); 6 System.out.println(person); 7 }
如何在javabean属性和表字段不一致的情况下正常取值?
方式一:查询语句中指定别名
1 <select id="loadPersonById2" parameterType="integer" resultType="person">
2
3 select id,user_name as userName,age,address from person where id = #{id}
4
5 </select>
方式二、使用resultMap标签
1 <resultMap type="person" id="personMap">
2
3 <result property=“userName" column="user_name"/>
4
5 </resultMap>
6
7 <select id="loadPersonById3" parameterType="integer" resultMap="personMap">
8
9 select * from person where id = #{id}
10
11 </select>
resultMap的使用 映射一对多、多对一
1.创建订单表orders
1 create table orders(id int primary key,pid int,num varchar(12),name varchar(150),price double(10,2)); 2 3 desc orders; 4 5 insert orders values(1,1,‘n101‘,‘shouji‘,1342.23); 6 7 insert orders values(2,1,‘d102‘,‘diannao‘,8766.74); 8 9 insert orders values(3,1,‘p103‘,‘pingban‘,3342.53);
2.创建orders.java
1 public class Orders { 2 3 private Integer id; 4 5 private String num; 6 7 private String name; 8 9 private Double price; 10 11 private Person person; 12 13 get set ... 14 15 }
3.创建OrdersMapper.xml
1 <mapper namespace="cn.itcast.dao.OrdersMapper"> 2 3 <resultMap type="orders" id="ordersMap"> 4 5 <result property="num" column="num"/> 6 7 <result property="name" column="name"/> 8 9 <association property="person" javaType="person" > 10 11 <result property="name" column="user_name"/> 12 13 <result property="address" column="address"/> 14 15 </association> 16 17 </resultMap> 18 19 <select id="loadOrdersById" parameterType="integer" resultMap="ordersMap"> 20 21 select a.*,b.* from orders a,person b where a.pid=b.id and a.id = #{id} 22 23 </select> 24 25 </mapper>
4.修改mybatis-config.xml,增加
1 <typeAlias alias="orders" type="cn.itcast.pojo.Orders"/> 2 <typeAlias alias="persion" type="cn.itcast.pojo.Persion"/> 3 <mapper resource="cn/itcast/dao/OrdersMapper.xml"/>
5.创建TestOrders
1 public class TestOrders { 2 3 @Test 4 5 public void loadOrdersById(){ 6 7 Orders o = sqlSession.selectOne("cn.itcast.dao.OrdersMapper.loadOrdersById", 1); 8 9 System.out.println("num:"+o.getNum() + ",name:" + o.getName() + ",personName:" 10 11 + o.getPerson().getName() + ",address:" + o.getPerson().getAddress()); 12 13 } 14 15 }
1.在Person.java中增加对应订单的list
1 private List<Orders> ordersList = new ArrayList<Orders>(); 2 3 public List<Orders> getOrdersList() { 4 5 return ordersList; 6 7 } 8 9 public void setOrdersList(List<Orders> ordersList) { 10 11 this.ordersList = ordersList; 12 13 }
2.在PersonMapper.xml中增加查询
1 <resultMap type="person" id="personMap2"> 2 3 <result property="name" column="user_name"/>
4 <!--配置orderList属性,并制定类型为orders--> 5 <collection property="ordersList" javaType="orders"> 6 7 <result property="name" column="name"/> 8 9 <result property="price" column="price"/> 10 11 <result property="num" column="num"/> 12 13 </collection> 14 15 </resultMap> 16 17 <select id="loadPersonById4" parameterType="integer" resultMap="personMap2"> 18 19 select a.*,b.* from person a,orders b where a.id=b.pid and a.id = #{id} 20 21 </select>
4. TestPerson中增加方法
1 @Test 2 3 public void loadPersonById4(){ 4 5 6 Person person = sqlSession.selectOne("cn.itcast.dao.PersonMapper.loadPersonById4", 1); 7 8 System.out.println(person.getName() + " " + person.getOrdersList().size()); 11 12 for(Orders o: person.getOrdersList()){ 13 14 System.out.println(o.getName() + " " + o.getPrice() + " " + o.getNum()); 15 16 } 17 18 }
mybatis typeAlias(别名)使用和resultMap使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。