标签:
<?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> <typeAliases> <typeAlias alias="Customer" type="com.mybatis.bean.Customer"/> <typeAlias alias="Order" type="com.mybatis.bean.Order"/> </typeAliases> <!--数据库参数配置 --> <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/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mybatis/bean/Customer.xml" /> <mapper resource="com/mybatis/bean/Order.xml" /> </mappers> </configuration>
package com.mybatis.util; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory() throws IOException { if (sqlSessionFactory == null) { String resource = "mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } return sqlSessionFactory; } }
package com.mybatis.bean; import java.util.List; public class Customer { private int id; private String name; private List<Order> orderList; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Order> getOrderList() { return orderList; } public void setOrderList(List<Order> orderList) { this.orderList = orderList; } }
package com.mybatis.bean; public class Order { private int id; private int price; private String name; private Customer customer; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
<?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.mybatis.inter.ICustomerDAO"> <resultMap type="Customer" id="cumtomerResMap"> <id property="id" column="c_id" /> <result property="name" column="c_name" /> <collection property="orderList" ofType="Order" column="cid"> <id property="id" column="o_id" /> <result property="name" column="o_name" /> <result property="price" column="o_price" /> </collection> </resultMap> <select id="selectCustomer" resultMap="cumtomerResMap"> select * from customer c,orders o where o.cid=c.c_id and c.c_id=#{id} </select> </mapper>
<?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.mybatis.inter.IOrderDAO"> <resultMap type="Order" id="orderResMap"> <id property="id" column="o_id" /> <result property="name" column="o_name" /> <result property="price" column="o_price" /> <association property="customer" javaType="Customer"> <id property="id" column="c_id" /> <result property="name" column="c_name" /> </association> </resultMap> <select id="selectOrder" resultMap="orderResMap"> select * from customer c,orders o where o.cid=c.c_id and o.o_id=#{id} </select> </mapper>
package com.mybatis.inter; import com.mybatis.bean.Customer; public interface ICustomerDAO { public Customer selectCustomer(int id); }
package com.mybatis.inter; import com.mybatis.bean.Order; public interface IOrderDAO { public Order selectOrder(int id); }
package com.mybatis.test; import java.io.IOException; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.mybatis.bean.Customer; import com.mybatis.bean.Order; import com.mybatis.inter.ICustomerDAO; import com.mybatis.inter.IOrderDAO; import com.mybatis.util.MybatisUtil; public class Test { public static void main(String[] args) { SqlSessionFactory factory = null; SqlSession session=null; try { factory=MybatisUtil.getSqlSessionFactory(); session=factory.openSession(); ICustomerDAO customerDAO = session.getMapper(ICustomerDAO.class); Customer customer = customerDAO.selectCustomer(2); System.err.println(customer.getName()+" 买了:"); for(Order order :customer.getOrderList()){ System.err.println(order.getName() +" "+order.getPrice()); } System.err.println("========================================="); IOrderDAO orderDAO = session.getMapper(IOrderDAO.class); Order order = orderDAO.selectOrder(2); System.err.println(order.getName()+" "+order.getPrice()); System.err.println(order.getCustomer().getName()); } catch (IOException e) { e.printStackTrace(); }finally{ if(session != null) session.close(); } } }
标签:
原文地址:http://www.cnblogs.com/zcs201093189/p/4844787.html