学习记录--mybatis 代理方法实现

时间:2022-05-12 00:09:05

学习记录--mybatis 代理方法实现

POJO类代码(两个表Orders,User的联合查询)

package com.xie.domain;

/**
*
* 映射查询 的结果(数据库语句查询出来的全部字段)
* 继承较多的POJO类
*
*/

public class OrdersCustom extends Orders{

private String userName;
private String sex;
private String addess;
public String getuserName() {
return userName;
}
public void setuserName(String userName) {
this.userName = userName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddess() {
return addess;
}
public void setAddess(String addess) {
this.addess = addess;
}
@Override
public String toString() {
return "OrderCustom [userName=" + userName + ", sex=" + sex + ", addess=" + addess + ", getuserName()="
+ getuserName() + ", getSex()=" + getSex() + ", getAddess()=" + getAddess() + ", getId()=" + getId()
+ ", getUser_id()=" + getUser_id() + ", getNumber()=" + getNumber() + ", getCreateTime()="
+ getCreateTime() + ", getNote()=" + getNote() + ", getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}

}

接口OrderMapperCustom代码:

package com.xie.mapping;

import java.util.List;

import com.xie.domain.OrdersCustom;

public interface OrdersMapperCustom {

public List<OrdersCustom> findOrderUser() throws Exception;
}

conf.xml代码

<?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"/>

<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="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="com/xie/mapping/OrdersMapperCustom.xml"/>
</mappers>

</configuration>

测试类代码:

String resource = "conf.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession=factory.openSession();
OrdersMapperCustom orderMapperCustom=sqlSession.getMapper(OrdersMapperCustom.class);
List<OrdersCustom> list=orderMapperCustom.findOrderUser();
System.out.println(list);
sqlSession.close();