将本学期学习的hibernate运用在项目中
在线商城项目功能:
(1)前台功能:用户注册、商品展示、购物车功能(商品进行添加、删除、修改、查看操作),订单提交。
(2)后台功能:商品增删改查和订单管理。
(3)商品搜索(搜索内容进行分词,提取关键字,模糊查询)。
只展示hibernate有关代码内容
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
>
四个hbm.xml
订单项目 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实体 -->
<class name="com.cn.shop.model.Item" table="store_order_item">
<!-- 定义主键及其生成策略 -->
<id name="id" column="id" type="long">
<generator class="increment" />
</id>
<!-- 定义数量字段 -->
<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实体 -->
<class name="com.cn.shop.model.Order" table="store_order">
<!-- 定义主键及其生成策略 -->
<id name="id" column="id" type="long">
<generator class="increment" />
</id>
<!-- 定义订单号字段 -->
<property name="orderNum" column="orderNum" unique="true" not-null="true" length="17"></property>
<!-- <property name="orderNum" column="orderNum" length="17"></property> -->
<!-- 定义订单状态字段 -->
<property name="status" type="integer" not-null="true" column="status"/>
<!-- <property name="status" type="integer" column="status"/> -->
<!-- 定义总价字段 -->
<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>
<!-- 定义包含的订单项 -->
<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实体 -->
<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" />
<!-- 定义价格字段 -->
<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实体 -->
<class name="com.cn.shop.model.User" table="store_user" discriminator-value="common">
<!-- 定义主键及其生成策略 -->
<id name="id" column="id" type="long">
<generator class="increment" />
</id>
<!-- 定义类型区别字段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>
<!-- 定义家庭电话号码字段 -->
<property name="homePhone" column="homePhone"></property>
<!-- 定义手机电话号码字段 -->
<property name="cellPhone" column="cellPhone"></property>
<!-- 定义办公室电话号码字段 -->
<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层接口和实现
BaseDAO.java
package com.cn.shop.dao;
import java.io.Serializable;
import java.util.List;
//基础DAO,包含基本的实体类增删改查操作
public interface BaseDAO {
//保存一个实体对象
public <T> void saveEntity( T entity );
//删除一个实体对象
public <T> void deleteEntity( T entity );
//根据实体类型和ID删除一个实体对象
public <T> void deleteEntityById( Class <T> entityClass , Serializable id );
//修改一个实体对象
public <T> void updateEntity( T entity );
//查询某个实体的全部实例
public <T> List<T> getAllEntity( String entityName );
//通过主键获的一个实体对象
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;
// 定义一个Logger,便于它及其子类计入日志用
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;
}
//实际页码
@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 {
// 登录验证,如果失败返回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);
}
}
网友评论