mybatis利用resultType方式实现数据库一对一查询
sqlMapConfig.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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <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="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="OrdersMapperCustom.xml"/> </mappers> </configuration>
OrdersMapperCustom.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mx.resultMap.OrderMapperCustom"> <!-- resultType 映射方式 --> <select id="findUser" resultType="com.mx.resultType.OrdresCustom"> select orders.*, user.username, user.sex, user.address from orders,user where orders.user_id=user.id; </select> </mapper>
Orders 类:
package com.mx.resultType; import java.util.Date; public class Orders { private int id; private String user_id; private String number; private Date createtime; private String note; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "Orders [id=" + id + ", user_id=" + user_id + ", number=" + number + ", createtime=" + createtime + ", note=" + note + "]"; } }
OrdersCustom 类:
package com.mx.resultType; public class OrdresCustom extends Orders{ private String username; private String sex; private String address; 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
OrdersMapperCustom接口:
package com.mx.resultType; import java.util.List; public interface OrdersMapperCustom { public List<OrdresCustom> findUser(); }
Test 测试类:
package com.mx.resultType; 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; public class Test { public static void main(String[]args) throws IOException{ String resource = "sqlMapConfig.xml"; InputStream is = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(); OrdersMapperCustom omc = sqlSession.getMapper(OrdersMapperCustom.class); List<OrdresCustom> list=omc.findUser(); System.out.println(list); sqlSession.close(); } }