將本學(xué)期學(xué)習(xí)的hibernate運(yùn)用在項(xiàng)目中
在線商城項(xiàng)目功能:
(1)前臺(tái)功能:用戶注冊(cè)逾礁、商品展示、購物車功能(商品進(jìn)行添加畸肆、刪除宦芦、修改、查看操作)轴脐,訂單提交调卑。
(2)后臺(tái)功能:商品增刪改查和訂單管理抡砂。
(3)商品搜索(搜索內(nèi)容進(jìn)行分詞,提取關(guān)鍵字恬涧,模糊查詢)注益。
只展示hibernate有關(guān)代碼內(nèi)容
1.配置文件 hibernate.xfg.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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/shop</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/cn/shop/model/User.hbm.xml"/>
<mapping resource="com/cn/shop/model/Order.hbm.xml"/>
<mapping resource="com/cn/shop/model/Item.hbm.xml"/>
<mapping resource="com/cn/shop/model/Product.hbm.xml"/>
<!--
<mapping class="com.cn.hibernate.model.Husband"/>
<mapping class="com.cn.hibernate.model.Wife"/>
-->
<!-- <mapping class="com.cn.shop.model.User"/> -->
<!-- <mapping class="com.cn.hibernate.model.Teacher"/> -->
</session-factory>
</hibernate-configuration
>
四個(gè)hbm.xml
訂單項(xiàng)目 Item.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 >
<!-- 定義Item實(shí)體 -->
<class name="com.cn.shop.model.Item" table="store_order_item">
<!-- 定義主鍵及其生成策略 -->
<id name="id" column="id" type="long">
<generator class="increment" />
</id>
<!-- 定義數(shù)量字段 -->
<property name="number" type="integer" not-null="true" column="amount"/>
<!-- 定義商品引用 -->
<many-to-one name="product" column="product_id" class="com.cn.shop.model.Product" cascade="none"></many-to-one>
<!-- 定義所屬的訂單 -->
<many-to-one name="order" column="order_id" class="com.cn.shop.model.Order" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
訂單Order.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 >
<!-- 定義Order實(shí)體 -->
<class name="com.cn.shop.model.Order" table="store_order">
<!-- 定義主鍵及其生成策略 -->
<id name="id" column="id" type="long">
<generator class="increment" />
</id>
<!-- 定義訂單號(hào)字段 -->
<property name="orderNum" column="orderNum" unique="true" not-null="true" length="17"></property>
<!-- <property name="orderNum" column="orderNum" length="17"></property> -->
<!-- 定義訂單狀態(tài)字段 -->
<property name="status" type="integer" not-null="true" column="status"/>
<!-- <property name="status" type="integer" column="status"/> -->
<!-- 定義總價(jià)字段 -->
<property name="cost" type="double" column="cost"></property>
<!-- 定義用戶引用 -->
<many-to-one name="user" column="user_id" class="com.cn.shop.model.User" cascade="none" lazy="false"></many-to-one>
<!-- 定義包含的訂單項(xiàng) -->
<map name="items" table="store_order_item" lazy="false" cascade="all" order-by="product_id">
<key column="order_id"></key>
<map-key column="product_id" type="long"/>
<one-to-many class="com.cn.shop.model.Item"/>
</map>
</class>
</hibernate-mapping>
商品 Product.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 >
<!-- 定義Product實(shí)體 -->
<class name="com.cn.shop.model.Product" table="store_product">
<!-- 定義主鍵及其生成策略 -->
<id name="id" column="id" type="long">
<generator class="increment" />
</id>
<!-- 定義商品名稱字段 -->
<property name="name" not-null="true" length="32" column="name"/>
<!-- 定義商品描述字段 -->
<property name="description" length="500" column="description" />
<!-- 定義價(jià)格字段 -->
<property name="price" type="double" column="price"></property>
<!-- 定義圖片地址字段 -->
<property name="imageSrc" length="500" column="imageSrc"></property>
</class>
</hibernate-mapping>
用戶 User.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 >
<!-- 定義User實(shí)體 -->
<class name="com.cn.shop.model.User" table="store_user" discriminator-value="common">
<!-- 定義主鍵及其生成策略 -->
<id name="id" column="id" type="long">
<generator class="increment" />
</id>
<!-- 定義類型區(qū)別字段type,普通用戶的值為common溯捆,管理員為admin -->
<discriminator column="type" type="string"></discriminator>
<!-- 定義用戶名字段 -->
<property name="name" unique="true" not-null="true" length="32" column="name"/>
<!-- 定義密碼字段 -->
<property name="password" not-null="true" length="16" column="password" />
<!-- 定義住址字段 -->
<property name="address" column="address"></property>
<!-- 定義email字段 -->
<property name="email" column="email"></property>
<!-- 定義郵編字段 -->
<property name="postCode" column="postCode"></property>
<!-- 定義家庭電話號(hào)碼字段 -->
<property name="homePhone" column="homePhone"></property>
<!-- 定義手機(jī)電話號(hào)碼字段 -->
<property name="cellPhone" column="cellPhone"></property>
<!-- 定義辦公室電話號(hào)碼字段 -->
<property name="officePhone" column="officePhone"></property>
<!-- 定義管理員子類 -->
<subclass name="com.cn.shop.model.Administrator" discriminator-value="admin" >
<property name="workNo" column="workNo"></property>
</subclass>
</class>
</hibernate-mapping>
Dao層接口和實(shí)現(xiàn)
BaseDAO.java
package com.cn.shop.dao;
import java.io.Serializable;
import java.util.List;
//基礎(chǔ)DAO丑搔,包含基本的實(shí)體類增刪改查操作
public interface BaseDAO {
//保存一個(gè)實(shí)體對(duì)象
public <T> void saveEntity( T entity );
//刪除一個(gè)實(shí)體對(duì)象
public <T> void deleteEntity( T entity );
//根據(jù)實(shí)體類型和ID刪除一個(gè)實(shí)體對(duì)象
public <T> void deleteEntityById( Class <T> entityClass , Serializable id );
//修改一個(gè)實(shí)體對(duì)象
public <T> void updateEntity( T entity );
//查詢某個(gè)實(shí)體的全部實(shí)例
public <T> List<T> getAllEntity( String entityName );
//通過主鍵獲的一個(gè)實(shí)體對(duì)象
public <T> T getEntityById( Class<T> entityClass , Serializable id );
//添加商品分頁功能
//分頁
public <T> List<T> getbyPage(String hql, int pageNo, int pageSize ,int real );
//最頁碼
public <T> int realPage( String hql );
}
BaseDAOImpl.java
package com.cn.shop.dao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class BaseDAOImpl implements BaseDAO {
private HibernateTemplate hibernateTemplate;
// 定義一個(gè)Logger,便于它及其子類計(jì)入日志用
protected Logger logger = Logger.getLogger(this.getClass());
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public <T> void saveEntity(T entity) {
// TODO Auto-generated method stub
this.hibernateTemplate.saveOrUpdate(entity);
}
@Override
public <T> void deleteEntity(T entity) {
// TODO Auto-generated method stub
this.hibernateTemplate.delete(entity);
}
@Override
public <T> void updateEntity(T entity) {
// TODO Auto-generated method stub
this.hibernateTemplate.saveOrUpdate(entity);
}
@Override
public <T> List<T> getAllEntity( String entityName) {
// TODO Auto-generated method stub
return (List<T>)this.hibernateTemplate.find("from" + " "+ entityName );
//return (List<T>)this.hibernateTemplate.find("from Product");
}
@Override
public <T> T getEntityById(Class<T> entityClass, Serializable id) {
// TODO Auto-generated method stub
return (T) this.hibernateTemplate.get(entityClass, id);
}
@Override
public <T> void deleteEntityById(Class<T> entityClass, Serializable id) {
// TODO Auto-generated method stub
this.hibernateTemplate.delete( this.getEntityById(entityClass, id) );
}
// 添加商品分頁功能
@Override
public <T> List<T> getbyPage(String hql, int pageNo, int pageSize ,int real ) {
// TODO Auto-generated method stub
List<T> list = new ArrayList<T>();
int begin = (pageNo-1 )*pageSize;
int end = (pageNo )*pageSize;
//分頁顯示可能存在bug
/*if( real < 5 )
{
end =real;
}
else
{
if(real < end )
{
//end = real -1;
end = real;
}
}*/
//bug 修改顯示bug
if( end > real )
{
end = real;
}
list = this.hibernateTemplate.find(hql).subList( begin , end );
return list;
}
//實(shí)際頁碼
@Override
public <T> int realPage(String hql) {
// TODO Auto-generated method stub
int count = 0;
count = this.hibernateTemplate.find(hql).size();
return count;
}
}
UserDAO.java
package com.cn.shop.dao;
import com.cn.shop.model.User;
public interface UserDAO extends BaseDAO {
// 登錄驗(yàn)證现使,如果失敗返回null
public User loginCheck(String name, String password);
public void save( User user );
}
UserDAOImpl.java
package com.cn.shop.dao;
import java.io.Serializable;
import java.util.List;
import com.cn.shop.model.User;
public class UserDAOImpl extends BaseDAOImpl implements UserDAO {
@Override
public User loginCheck(String name, String password) {
// TODO Auto-generated method stub
/*String hql = "from com.cn.shop.model.User u where u.name = ? and u.password = ? ";
List<User> list = this.getHibernateTemplate().find( hql, new String []{"throne212", "123" });*/
String hql = "from com.cn.shop.model.User u where u.name = "+"'"+name +"'" + " " + "and u.password =" + " " + "'"+password +"'";
List<User> list = this.getHibernateTemplate().find( hql);
if( list != null && list.size() >0 )
{
super.logger.info("login check succ, user.name=" + list.get(0).getName());
return list.get(0);
}
else
{
return null;
}
}
@Override
public void save(User user) {
// TODO Auto-generated method stub
super.saveEntity(user);
}
}