使用mybatis,进行多表联合查询
在配置文件中添加读取properties文件
添加日志
添加多个映射(mapper)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties" />
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pass}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/qfedu/mapper/OrderMapper.xml"/>
<mapper resource="com/qfedu/mapper/UserMapper.xml"/>
</mappers>
</configuration>
mapper配置文件设置返回类型为resultMap,会根据下面的标签填值
在resuletMap标签中
- id代表主键,分别设置属性(property)、数据库列(column)对应关系
- result代表普通字段映射,如果字段名和属性名一致,可以省略
- association关联,只要对一关系都可以使用
property代表order类中的u(用于储存另一个表查询后的数据),column代表order数据库表的uid字段(两表之间的联系)
select:另一个xml的namespace和id
OrderMapper.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.qfedu.pojo.OrderMapper">
<select id="getOrderByOid" resultMap="orderMap">
select * from orders where oid = #{oid}
</select>
<resultMap id="orderMap" type="com.qfedu.pojo.Order">
<id property="oid" column="oid" ></id>
<result column="price" property="price" />
<result column="addr" property="addr" />
<result column="payType" property="payType" />
<association property="u" column="uid" select="com.qfedu.pojo.UserMapper.getUserByUid"></association>
</resultMap>
</mapper>
UserMapper.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.qfedu.pojo.UserMapper">
<select id="getUserByUid" resultType="com.qfedu.pojo.User">
select * from users where uid = #{uid}
</select>
</mapper>
order.java
public class Order {
private String oid;
private double price;
private String addr;
private String payType;
private User u;
private List<Detail> details;
@Override
public String toString() {
return "Order{" +
"oid='" + oid + '\'' +
", price=" + price +
", addr='" + addr + '\'' +
", payType='" + payType + '\'' +
", u=" + u +
", details=" + details +
'}';
}
public String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getPayType() {
return payType;
}
public void setPayType(String payType) {
this.payType = payType;
}
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public List<Detail> getDetails() {
return details;
}
public void setDetails(List<Detail> details) {
this.details = details;
}
}