Hibernate的SQL查询和批量抓取

Customer.java

public class Customer {
	
	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Customer(String cust_name, String cust_source) {
		super();
		this.cust_name = cust_name;
		this.cust_source = cust_source;
	}

	private Long cust_id;
	private String cust_name;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_phone;
	private String cust_mobile;
	//通过 ORM方式表示:一个客户对应的多个联系人
	//防止的多的一方的集合。Hibernate默认使用的是Set集合
	private Set<LinkMan> linkMans=new HashSet<LinkMan>();
    //省略了get和set方法
}

LinkMan.java

public class LinkMan {
	private Long lkm_id;
	private String lkm_name;
	private String lkm_gender;
	private String lkm_phone;
	private String lkm_mobile;
	private String lkm_email;
	private String lkm_qq;
	private String lkm_position;
	private String lkm_memo;
	//通过ORM方式表示:一个联系人只能属于一个客户
	//放置的是一的一方的对象
	private Customer customer;
    //省略了get和set方法
}

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.hibernate.domain.Customer" table="cst_customer">
		<!-- 建立OID与主键映射 -->
		<id name="cust_id" column="cust_id">
			<generator class="native"/>
		</id>
		<!-- 建立普通属性和表的字段的对应 -->
		<property name="cust_name" column="cust_name"/>
		<property name="cust_source" column="cust_source"/>
		<property name="cust_industry" column="cust_industry"/>
		<property name="cust_level" column="cust_level"/>
		<property name="cust_phone" column="cust_phone"/>
		<property name="cust_mobile" column="cust_mobile"/>
		<!-- 配置一对多的映射:放置的多的一方的集合 -->
		<!-- 
			set标签
				* name	:多的一方的对象集合的属性名称
				* cascade:级联
				* inverse:放弃`外键`维护权    默认false:不放弃
		 -->
		<set name="linkMans">
			<!-- 
				key标签
					* column:多的一方的外键的名称
			 -->
			<key column="lkm_cust_id"/>
			<!-- 
				one-to-many标签
					* class	:多的一方的全路径
			 -->
			<one-to-many class="com.hibernate.domain.LinkMan"/>
		</set>
	</class>
</hibernate-mapping>

LinkMan.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.hibernate.domain.LinkMan" table="cst_linkman">
		<!-- 建立OID与主键映射 -->
		<id name="lkm_id" column="lkm_id">
			<generator class="native"/>
		</id>
		<!-- 建立普通属性与表字段映射 -->
		<property name="lkm_name"/>
		<property name="lkm_gender"/>
		<property name="lkm_phone"/>
		<property name="lkm_mobile"/>
		<property name="lkm_email"/>
		<property name="lkm_qq"/>
		<property name="lkm_position"/>
		<property name="lkm_memo"/>
		<!-- 配置多对一的关系:放置的是一的一方的对象 -->
		<!-- 
			many-to-one标签
				* name		:一的一方的对象的属性名称
				* class		:一的一方的全路径
				* column	:在多的一方的表的外键的名称
		 -->
		 <many-to-one name="customer" cascade="save-update,delete" class="com.hibernate.domain.Customer" column="lkm_cust_id"/>
	</class>
</hibernate-mapping>

src下的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 连接数据库的基本参数 -->
		<!-- hibernate-release-5.0.7.Final\project\etc\hibernate.properties -->
		<!-- localhost:3306 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day04</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">password</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- ==========可选配置========= -->
		<!-- 打印sql -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化sql -->
		<property name="hibernate.format_sql">true</property>
		<!-- 自动建表create update -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 设置事务隔离级别 -->
		<!-- 
			1 read uncommitted  :脏读、不可重复读、虚读都会发生
			2 read committed    :解决脏读  (oracle默认)
			4 repeatable read   :解决脏读和不可重复读(mysql默认)
			8 serializable      :解决所有问题。串行执行、效率低
		 -->
		<property name="hibernate.connection.isolation">4</property>
		<!-- 配置当前线程绑定的Session -->
		<!-- 不用session.close() -->
		<property name="hibernate.current_session_context_class">thread</property>
		
		<!-- 引入映射 -->
		<mapping resource="com/hibernate/domain/Customer.hbm.xml"/>
		<mapping resource="com/hibernate/domain/LinkMan.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>

测试类

/**
 * SQL查询
 * @author zhang
 *
 */
public class HibernateDemo3 {
	
	@Test
	public void demo1() {
		Session session=HibernateUtils.getCurrentSession();
		Transaction tx=session.beginTransaction();
		
		/*SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
		List<Object[]> list = sqlQuery.list();
		for (Object[] objects : list) {
			System.out.println(Arrays.toString(objects));
		}*/
		
		//封装到对象
		SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
		sqlQuery.addEntity(Customer.class);
		List<Customer> list = sqlQuery.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		tx.commit();
	}
}
/**
 * 批量抓取
 *
 */
public class HibernateDemo6 {

	@SuppressWarnings("unchecked")
	@Test
	/**
	 * 获取客户的时候,批量抓取联系人
	 * 在Customer.hbm.xml中set上配置batch-size
	 */
	public void demo1(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();
		
		List<Customer> list = session.createQuery("from Customer").list();
		for (Customer customer : list) {
			System.out.println(customer.getCust_name());
			for (LinkMan linkMan : customer.getLinkMans()) {
				System.out.println(linkMan.getLkm_name());
			}
		}
		tx.commit();
	}
	
	@SuppressWarnings("unchecked")
	@Test
	/**
	 * 获取联系人的时候,批量抓取客户
	 * * 在Customer.hbm.xml中<class>上配置
	 */
	public void demo2(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();
		
		List<LinkMan> list = session.createQuery("from LinkMan").list();
		for (LinkMan linkMan : list) {
			System.out.println(linkMan.getLkm_name());
			System.out.println(linkMan.getCustomer().getCust_name());
		}
		tx.commit();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值