mybatis将指定列赋值到实体的HashMap属性中
2013-11-01 00:00
162 查看
需求:将一个表中的部分列值存入到实体的HashMap属性中
表结构:
实体类:
mapper.xml配置:
配置:
测试类:
测试结果:
表结构:
/* Navicat MySQL Data Transfer Source Server Version : 50533 Target Server Type : MYSQL Target Server Version : 50533 File Encoding : 65001 Date: 2013-11-01 15:34:47 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `TEST_MAP` -- ---------------------------- DROP TABLE IF EXISTS `TEST_MAP`; CREATE TABLE `TEST_MAP` ( `id` int(11) DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `address` varchar(100) DEFAULT NULL, `telephone` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of TEST_MAP -- ---------------------------- INSERT INTO `TEST_MAP` VALUES ('1', 'name1', 'address1', '1111'); INSERT INTO `TEST_MAP` VALUES ('2', 'name2', 'address2', '2222');
实体类:
import java.util.Map; public class TestMap { private Integer id; private String name; private Map<String, String> attributes;//将address和telephone的值放入其中 public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, String> getAttributes() { return attributes; } public void setAttributes(Map<String, String> attributes) { this.attributes = attributes; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
mapper.xml配置:
<resultMap id="BaseResultMap" type="cn.com.china.entity.TestMap" > <id column="id" property="id" jdbcType="INTEGER" /> <id column="name" property="name" jdbcType="VARCHAR" /> <association property="attributes" javaType="java.util.HashMap" > <id column="address" property="address" jdbcType="VARCHAR" /> <id column="telephone" property="telephone" jdbcType="VARCHAR" /> </association> </resultMap>
配置:
<!-- 给予被嵌套的resultMap以字段-属性的映射支持 --> <setting name="autoMappingBehavior" value="PARTIAL" />
测试类:
public class TestMapServiceTest extends BaseTest { TestMapMapper testMapMapper = null; @Before public void setUp() { testMapMapper = (TestMapMapper) ctx.getBean(TestMapMapper.class); } @Test public void select() { TestMap map = testMapMapper.selectByPrimaryKey(1); System.out.println("name:"+map.getName()); System.out.println("name:"+map.getAttributes().get("name")); System.out.println("address:"+map.getAttributes().get("address")); System.out.println("telephone:"+map.getAttributes().get("telephone")); } }
测试结果:
name:name1 name:null address:address1 telephone:1111
相关文章推荐
- 使用MyBatis指定返回结果与实体属性相对应的方法
- Java使用PropertyDescriptor获取实体类中私有属性的值,并给私有属性赋值
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- 实体--C# 反射动态给属性赋值
- C# 实体/集合差异比较,比较两个实体或集合值是否一样,将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- 不同实体类相同属性之间的快速赋值
- 在动态引用DLL-A中,当参数是个实体,而实体的属性在另一个DLL-B中。。我们需要得到A这个实体并将其赋值,并将赋值的实体传人DLL-A的方法中。
- mysql数据库日期设计以及mybatis映射文件和实体属性的关系
- 两实体之间属性字段的自动赋值
- 从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~五 LINQ实体类中对属性的赋值,变化前与变化后SendPropertyChanging与SendPropertyChanged
- MyBatis拦截器:给参数对象属性赋值
- C#遍历实体的所有属性并为属性赋值
- Mybatis 属性 无法 赋值
- java 为一个实体类中已赋值的属性生成sql语句
- mybatis无法给带有下划线属性赋值问题
- mybatis插入一个对象后获取表中自增的主键Id并且传入到插入的的对象中,方便将对象中其他属性赋值给其他以前表主键Id作为非空字段的表
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值