今天在项目中用了mybatis的resultMap。以前用的时候都是一些简单的查询,修改,分页。这次涉及到了POJO对象之间的一对多和多对一的关系映射。
mybatis有几种使用方式, 我喜欢用mapper的方式,然后用spring来管理mybatis.
开发工具是Eclipse jee, mybatis版本是3.0.5, mybatis-sprint-1.0.1
mybatis-config.xml 是mybatis的配置文件:
<configuration>
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<mappers>
<mapper resource="com/exam/persistence/mapper/ClientMapper.xml" />
</mappers>
</configuration>
applicationContext.xml:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
</bean>
<bean id="clientMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.exam.persistence.mapper.ClientMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
配置文件就是这些了。主要的东西在下面:
业务逻辑涉及到三张表: Client, Subscriber, Account. 其中Client表和Subscriber表是多对一的关系. Client表和Account是一对多的关系。
在com.example.persistence.mapper目录下,需要创建两个文件分别是:ClientMapper.java 和 ClientMapper.xml
对象映射关系主要在ClientMapper.xml中定义:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.exam.persistence.mapper.ClientMapper">
<resultMap id="clients" type="com.exam.entity.Clients">
<id property="externalId" column="externalId" />
<result property="id" column="id"/>
<result property="subscriptionId" column="subscriptionId"/>
<association property="subscription" column="subscriptionId" javaType="com.exam.entity.Subscription" select="selectSubscription"/>
<collection property="accounts" column="id" ofType="com.exam.entity.Accounts" select="selectAccounts"/>
</resultMap>
<select id="getClientByID" resultMap="clients" parameterType="java.lang.String">
select
client.internal_id as id,
client.external_id as externalId,
from CLIENT client
left outer join SUBSCRIPTION subscription on client.subscription_id = subscription.subscription_id
left outer join ACCOUNT accounts on client.internal_id = accounts.id
where client.external_id = #{external_id}
</select>
<select id="selectAccounts" parameterType="int" resultType="com.exam.entity.Accounts">
select * from ACCOUNT where client_id=#{client_id}
</select>
<select id="selectSubscription" parameterType="java.lang.String" resultType="com.exam.entity.Subscription">
select * from SUBSCRIPTION where subscription_id = #{subscription_id}
</select>
</mapper>
这个xml文件定义了一个resultMap id="clients". clients里面包含了一个assoction(多对一)和一个collection(一对多). 这两个分别对应了两个select id.
ClientMapper.java是一个interface:
import java.util.List;
import com.exam.entity.Clients;
public interface ClientMapper {
public List<Clients> getClientByID(String external_id);
}
其中函数名"getClientByID"应该和ClientMapper.xml中的select id的值相同。
在com.exam.entity目录下面需要定义Clients, Accounts, Subscription 三个POJO类。就不在这里写POJO类了。
这样我们在service逻辑中就可以使用ClientMapper.
public class ClientServiceImpl implements ClientService {
private ClientMapper clientMapper;
public ClientMapper getClientMapper() {
return deviceMapper;
}
public void setClientMapper(ClientMapper clientMapper) {
this.clientMapper = clientMapper;
}
@Override
public List<Clients> getClientByID(String external_id) {
return getClientMapper().getClientByID(external_id);
}
}我们需要在applicationContext中把clientMapper注入到这个server类中就ok了。
先写到这里。