我的问题是这样的:我可以从Sql获取keyCode(num),但将值(String)映射到对象?
我使用以下设置:
- 的MyBatis 3.4.2
- springframework的4.3.3
- 的MySQL 5.7
我的表MySql是
CREATE TABLE `account_info` (
`account_id` int(9) NOT NULL AUTO_INCREMENT,
`account_holder_id` char(64) NOT NULL,
`account_number` char(19) NOT NULL,
`account_type` int(2) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
我想要的是在AccountInfoMapper.xml account_type
字段映射像(1:"debit", 2:"credit", 3:"affiliated", ...)
我的Java类
public class AccountInfo {
private Integer accountId;
private String accountHolderId;
private String accountNumber;
private String accountType;
// getter and setter
}
这
<select id="getAcountInfoById" parameterType="int" resultType="demo.domain.AccountInfo">
select
*
from
account_info
where
account_id = #{accountId}
</select>
这是
public class TestMybatisWithXML {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws Exception {
String resource = "config/myBatisConfig.xml";
Reader inputStream = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testGetAcountInfoById() {
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountInfoMapper accountInfoMapper = sqlSession.getMapper(AccountInfoMapper.class);
AccountInfo temAccountInfo = accountInfoMapper.getAcountInfoById(9);
sqlSession.close();
System.out.println(temAccountInfo.toString());
}
}
测试类返回来自测试课程:
AccountInfo [accountId=9, accountHolderId=1, accountNumber=00010001, accountType=2]
这是我想什么是结果,而不是:
AccountInfo [accountId=9, accountHolderId=1, accountNumber=00010001, accountType=credit]
如果有其他不同的方式,我可以有同样的结果将是巨大的。但这一次,我不能修改数据库表或语句。
创建替换映射也是可以接受的。 但是如果在mybatis中有某种方式,可能对我更好。
请提供具体的代码示例。伪代码不清楚,可能以多种方式解释。 如果我尝试一个猜测:你的查询返回一个代码列表(int),你需要一个对象列表,他们的属性是匹配这个代码的标签?如果你确认,我会写基于resultHandler的解决方案。 – blackwizard
我编辑了我的问题。有足够的信息吗?并感谢您的帮助。 –