1. 程式人生 > >mybatis之多對一+多對多+一對多(部分)

mybatis之多對一+多對多+一對多(部分)

1.com.dao(customerMapper+shopMapper)

public interface CustomerMapper {

	List<Customer> many2many();
}
——————————————————————————————————————
<mapper namespace="com.dao.CustomerMapper">
	<resultMap type="customer" id="mm">
		<result property="customerName" column="customername"/>
		
		<collection property="olist" ofType="Orders">
			<id property="ordersId" column="ordersid"/>
			<result property="ordersDate" column="ordersdate"/>
			
			<collection property="odlist" ofType="OrdersDetail">
				<result property="quantity" column="quantity"/>
				
				<association property="product" javaType="product">
					<result property="title" column="title"/>
					<result property="currentPrice" column="currentprice"/>
				</association>
			</collection>
		</collection>
	</resultMap>

	<select id="many2many" resultMap="mm">
		select customername,o.ordersid,to_char(ordersdate,'yyyy-mm-dd') ordersdate,
  quantity,title,to_char(currentprice,'fm999990.0099') currentprice 
  from customer c inner join orders o on c.customerid=o.customerid 
  inner join ordersdetail od on o.ordersid=od.ordersid 
  inner join product p on od.productid=p.productid
	</select>
</mapper>
——————————————————————————————————————

public interface ShopMapper {

	List<Product> many2one();
	
	List<Shop> one2many();
}
——————————————————————————————————————
<mapper namespace="com.dao.ShopMapper">
	<resultMap type="com.pojo.Product" id="aaa">
		<id property="productId" column="productId"/>
		<result property="title" column="title"/>
		
		<!-- association:用於對映一方實體 -->
		<association property="shop" javaType="com.pojo.Shop">
			<result property="shopName" column="shopname"/>
			<result property="shopAddress" column="shopaddress"/>
		</association>
	</resultMap>
	
	<resultMap type="shop" id="bbb">
		<id property="shopId" column="shopid"/>
		<result property="shopName" column="shopname"/>
		<result property="shopAddress" column="shopaddress"/>
		
		<!-- collection:用於對映多方實體 -->
		<collection property="plist" ofType="product">
			<result property="title" column="title"/>
		</collection>
	</resultMap>

	<select id="many2one" resultMap="aaa">
		select productid,title,shopname,shopaddress 
		from product p inner join shop s on p.shopid=s.shopid 
		order by productid
	</select>
	
	<select id="one2many" resultMap="bbb">
		select s.shopid,shopname,shopaddress,title 
		from shop s inner join product p on s.shopid=p.shopid
	</select>
</mapper>

2.com.pojo(customer+orders+ordersDetail+product+shop)

public class Customer { private String customerId;

private String login;

private String pwd;

private String customerName;

private String tel;

private String address;

private String gender;

private String birthday;

private List<Orders> olist;

} —————————————————————————— public class Orders { private String ordersId;

private String customerId;

private String ordersDate;

private String deliveryDate;

private String amount;

private List<OrdersDetail> odlist;
}

—————————————————————————— public class OrdersDetail {

private String ordersId;

private String productId;

private String quantity;

private Orders orders;

private Product product;

} —————————————————————————— public class Product {

private String productId;

private String areaId;

private String categoryId;

private String shopId;

private String title;

private String productDesc;

private String originalPrice;

private String currentPrice;

private String picture;

private String isCommend;

private String salesCount;

// product到shop表之間的關係為多對一
private Shop shop;

} —————————————————————————— public class Shop {

private String shopId;

private String shopName;

private String shopAddress;

private String contact;

// 對映多方實體(通過集合)
private List<Product> plist;

3.com.service(CustomerService+ShopService)

public class CustomerService {

public List<Customer> many2many() {
	
	SqlSession session = null;
	
	try {
		session = MyBatisUtil.getSqlSession();
		CustomerMapper mapper = session.getMapper(CustomerMapper.class);
		return mapper.many2many();
	} finally {
		MyBatisUtil.close(session);
	}
}

}

public class ShopService {

public List<Product> many2one() {
	
	SqlSession session = null;
	
	try {
		session = MyBatisUtil.getSqlSession();
		ShopMapper mapper = session.getMapper(ShopMapper.class);
		return mapper.many2one();
	} finally {
		MyBatisUtil.close(session);
	}
}

4.com.test(TestMyBatis)

public class TestMyBatis {

	private ShopService service = new ShopService();
	
	private CustomerService cs = new CustomerService();
	
	@Test
	public void testMany2one() {
		
		List<Product> list = service.many2one();
		
		for(Product p : list) {
			String productId = p.getProductId();
			String title = p.getTitle();
			
			Shop shop = p.getShop();
			String shopName = shop.getShopName();
			String shopAddress = shop.getShopAddress();

			System.out.println(productId + "    " + title + "    " + shopName + "    " + shopAddress);
		}
	}
	
	@Test
	public void testOne2many() {
		
		List<Shop> list = service.one2many();
		
		for(Shop shop : list) {
			String shopId = shop.getShopId();
			String shopName = shop.getShopName();
			String shopAddress = shop.getShopAddress();
			
			System.out.println(shopId + "    " + shopName + "    " + shopAddress);
			
			List<Product> plist = shop.getPlist();
			for(Product p : plist) {
				String title = p.getTitle();
				System.out.println("\t\t" + title);
			}
		}
	}
	
	@Test
	public void testMany2Many() {
		
		List<Customer> list = cs.many2many();
		System.out.println(list.size());
	}
}

5.com.util(MyBatisUtil)

public final class MyBatisUtil {

	private static SqlSessionFactory factory = buildSqlSessionFactory();
	
	private MyBatisUtil() {}
	
	private static final SqlSessionFactory buildSqlSessionFactory() {
		
		String resource = "resources/mybatis-config.xml";
		
		try {
			InputStream inputStream = Resources.getResourceAsStream(resource);
			return new SqlSessionFactoryBuilder().build(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static final SqlSession getSqlSession() {
		
		return factory.openSession();
	}
	
	public static final void close(SqlSession sqlSession) {
		
		if(sqlSession != null) {
			sqlSession.close();
		}
	}
}

6.resources(db.properties+mybatis-config.xml)

jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=meitao
jdbc.password=123
___________________________________________
<configuration>
	<!-- 外部屬性檔案 -->
	<properties resource="resources/db.properties"/>

	<settings>
		<!-- 控制檯顯示sql語句(開發使用) -->
		<setting name="logImpl" value="STDOUT_LOGGING"/>
	</settings>
	
	<typeAliases>
  		<package name="com.pojo"/>
	</typeAliases>

	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			
			<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="resources/ShopMapper.xml"/> -->
		
		<!-- 註冊指定包中的所有對映器介面(推薦) -->
		<package name="com.dao"/>
	</mappers>
</configuration>