Mybatis使用resultType实现一对一查询
Posted 小玄影123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis使用resultType实现一对一查询相关的知识,希望对你有一定的参考价值。
引入mybatis相关jar,本示例是基于3.2.7版本,引入相关数据库驱动jar
创建两张数据库表
user表
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL COMMENT ‘用户姓名‘, `birthday` date DEFAULT NULL COMMENT ‘出生日期‘, `sex` char(1) DEFAULT NULL COMMENT ‘性别‘, `address` varchar(256) DEFAULT NULL COMMENT ‘住址‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8
orders表
CREATE TABLE `orders` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL COMMENT ‘下单用户id号‘, `number` varchar(32) NOT NULL COMMENT ‘用户订单号‘, `createtime` datetime NOT NULL COMMENT ‘创建订单的时间‘, `note` varchar(100) DEFAULT NULL COMMENT ‘备注信息‘, PRIMARY KEY (`id`), KEY `FK_orders_1` (`user_id`), CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
创建user的javabean对象
import java.util.Date; public class User { private int id; private String username; private Date birthday; private String sex; private String address; 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address=" + address + "]"; } }
创建user的扩展类匹配输出结果
import cn.zqq.bean.User; public class UserCustom extends User{ private String number; private Date craetetime; private String note; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Date getCraetetime() { return craetetime; } public void setCraetetime(Date craetetime) { this.craetetime = craetetime; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "UserCustom [number=" + number + ", craetetime=" + craetetime + ", note=" + note +"id=" + super.getId() + ", username=" + super.getUsername() + ", birthday=" + super.getBirthday() + ", sex=" + super.getSex() + ", address=" + super.getAddress() + "]"; } }
编写SqlMapConfig.xml使其在classpath中,数据库相关配置以mysql为例
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="****" /> </dataSource> </environment> </environments> <mappers> //基于包的扫描对相应的mapper.xml的读取 <package name="对应的包名"/> </mappers> </configuration>
编写UserCustomOrder.xml(mapper.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">
//namespace的名字和对应的接口全类名相同 <mapper namespace="cn.zqq.one2one.UserCustomOrder"> <select id="findAllInfoUserOrders" resultType="包名.UserCustom"> SELECT * FROM USER,orders WHERE user.id=orders.user_id; </select> </mapper>
编写对应的UserCustomOrder.java类名和mapper.xml的具体名字对应,
import java.util.List; public interface UserCustomOrder { public List<UserCustom> findAllInfoUserOrders()throws Exception; }
编写测试类
import java.io.IOException; import java.io.InputStream; import java.util.List; 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 org.junit.Before; import org.junit.Test; public class TestOne2One { SqlSession sqlSession = null; @Before public void beforesourse() throws IOException{
//加载配置文件 String resource = "SqlMapConfig.xml";
//获取相关文件的流 InputStream inputStream = Resources.getResourceAsStream(resource); //得到sqlSession工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream);
//得到sqlSession sqlSession = sqlSessionFactory.openSession(); } @Test public void findAllInfoUserOrders() throws Exception {
//使用mapper代理获得实例结果 UserCustomOrder order = sqlSession.getMapper(UserCustomOrder.class); List<UserCustom> userOrders = order.findAllInfoUserOrders(); for (UserCustom userCustom : userOrders) { System.out.println(userCustom); }
sqlSession.close();
}
}
运行上边的程序便可得到结果,简单应用,正在学习中
以上是关于Mybatis使用resultType实现一对一查询的主要内容,如果未能解决你的问题,请参考以下文章