mybatis获取对象及其关联对象的两种方式
2013-07-06 15:28
141 查看
场景如下:有一个Book类,一个Category类,Book类有一个对Category的引用,要求获取Book对象的同时取出Category对象
Book.java类代码如下
public class Book implements Serializable { /** * */ private static final long serialVersionUID = 8634505584095346474L; private String id; private String name; private Date publishDate; private String author; private Float price; private Category category; private Long quantity; // setter and getter method is omited! }
Category.java类代码如下
public class Category implements Serializable { /** * */ private static final long serialVersionUID = 8964146416205979692L; private String id; private String name; private String spell; private String comments; //setter and getter method is omited! }
对应获取Book对象的两种方式,通过配置BookMapper.xml中实现
方式一(通过配置映射实现):
<resultMap id="bookMap" type="Book"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="publishDate" column="publishDate"/> <result property="author" column="author"/> <result property="price" column="price"/> <result property="quantity" column="quantity"/> <association property="category" javaType="Category"> <id property="id" column="category_id" javaType="string"/> <id property="name" column="category_name"/> <id property="spell" column="category_spell"/> <id property="comments" column="category_comments"/> </association> </resultMap> <select id="getBook" parameterType="string" resultMap="bookMap"> select b.id, b.name, b.publishDate, b.author, b.price, b.category, b.quantity, c.id as category_id, c.name as category_name, c.spell as category_spell, c.comments as category_comments from t_book b left outer join t_bookcategory c on b.category=c.id where b.id=#{id} </select>
方式二(通过OGNL实现):
<select id="getBook" parameterType="string" resultType="Book"> select b.id, b.name, b.publishDate, b.author, b.price, b.quantity, c.id as "category.id", c.name as "category.name", c.spell as "category.spell", c.comments as "category.comments" from t_book b left outer join t_bookcategory c on b.category=c.id where b.id=#{id} </select>
相关文章推荐
- MyBatis配置一对一关联查询的两种方式及其双向获取时注意问题
- myBatis获取SqlSession连接对象的两种方式
- MyBatis+Spring在注解@Autowried后通过反射的方式调用方法获取注入的Service或DAO对象为空
- Mybatis——使用注释方式查询出关联的对象
- Java反射机制及其获取InputStream的两种方式
- 外部获取IndexPath的几种方式(关联对象等)
- 获取jQuery validation对象的两种方式
- JAVA反射(一)获取Class对象的三种方式及其区别
- mybatis+spring中的Mapper通过非自动注入方式获取Mapper代理对象(Spring 工厂)
- 获取servletContext对象的两种方式
- Java反射02 : Class对象获取的三种方式和通过反射实例化对象的两种方式
- MyBatis笔记----多表关联查询两种方式实现
- Java——深入理解Class对象(二):Class对象的加载及其获取方式
- strut获取作用对象的两种方式
- JAVA反射(一)获取Class对象的三种方式及其区别
- 外部获取IndexPath的几种方式(关联对象等)
- jvm两种方式获取对象所占用的内存
- mybatis使用注释方式查询出关联的对象
- MyBatis获取自增长主键值的两种方式及源码浅析
- js获取url参数值的两种方式