一对一查询
我们这里做一个 用户表对订单表,即 一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户
我们准备工作 建表、Bean、核心配置文件、映射文件 这里省略,
执行命令:
然后我们Bean:
package com.bihu.Bean; import java.util.Date; public class User { private int id; private String username; private String password; private Date birthday; public User() { } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username=‘" + username + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ", birthday=" + birthday + ‘}‘; } }
package com.bihu.Bean; import java.util.Date; public class Order { private int id; private Date ordertime; private double total; //当前订单属于哪一个用户 private User user; public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getOrdertime() { return ordertime; } public void setOrdertime(Date ordertime) { this.ordertime = ordertime; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Order{" + "id=" + id + ", ordertime=" + ordertime + ", total=" + total + ", user=" + user + ‘}‘; } }
mybetis核心配置文件:
<?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="datasourceInfo.properties"></properties> <typeAliases> <typeAlias type="com.bihu.Bean.User" alias="User"></typeAlias> <typeAlias type="com.bihu.Bean.Order" alias="Order"></typeAlias> <typeAlias type="java.util.List" alias="List"></typeAlias> </typeAliases> <!-- 插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/bihu/mapper/UserMapper.xml"></mapper> <mapper resource="com/bihu/mapper/OrderMapper.xml"></mapper> </mappers> </configuration>
??? 重点是映射文件这!!!
<?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.bihu.Dao.OrderMapper"> <resultMap id="orderMap" type="Order"> <!--Order已设置别名--> <id column="oid" property="id"></id> <result column="ordertime" property="ordertime"></result> <result column="total" property="total"></result> <!-- 下面开始是User的封装--> <result column="uid" property="user.id"></result> <result column="username" property="user.username"></result> <result column="password" property="user.password"></result> <result column="birthday" property="user.birthday"></result> </resultMap> <select id="findAll" resultMap="orderMap"> <!--这的返回结果更改为上面自定义的结果集resultMap 因为里面有User对象--> SELECT *,o.id oid FROM orders o,user u WHERE o.uid=u.id; </select> </mapper>
重点看注释即可
然后这里测试
注意测试里面的是Order对象,映射的也是Order文件
package com.bihu.Service; import com.bihu.Bean.Order; import com.bihu.Bean.User; import com.bihu.Dao.OrderMapper; import com.bihu.Dao.UserMapper; import com.github.pagehelper.PageHelper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; public class UserService { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(); OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); List<Order> all = mapper.findAll(); for (Order item : all) { System.out.println(item); } sqlSession.close(); } }
运行: