之前做了一些基础准备,设计了一下数据库,之后就开始制作bean,编写hbm.xml了
首先创建一个web项目,用的是web3.1 java1.8
之后导jar包,关于环境之间已经介绍过了,我就不在赘述了
创建bean:我按数据库设计的模块,放在不同的package下了,方便使用
Entity.java是接口,所有bean需要实现,方便之后的dao层编写
- Entity.java
-
- public interface Entity {
- public abstract Long getId();
- public abstract void setId(Long long1);
- }
|
QueryBuilder.java用于分页查询,在dao层会用到
- QueryBuilder.java
-
- public class QueryBuilder {
- // 是否查询一列 true:查询全部 false:查询alias的值
- public boolean countMode = true;
- // 查询列名
- public String alias = null;
- // 查询表名
- public String tableName = null;
- // 是否排序 true:排序 false:不排序
- public boolean orderMode = false;
- // 排序字段
- public String orderColimn = null;
- // 升序ASC 降序DESC
- public String orderSort = null;
- // public final String ORDER_ASC = "ASC";
- // public final String ORDER_DESC = "DESC";
- // 是否分页 true:分页 false:不分页
- public boolean pageMode = false;
- // 查询条数 第一条 最后一条
- public int firstResult = -1;
- public int lastResult = -1;
- public boolean getCountMode() {
- return countMode;
- }
- public void setCountMode(boolean countMode) {
- this.countMode = countMode;
- }
- public String getAlias() {
- return alias;
- }
- public void setAlias(String alias) {
- this.alias = alias;
- }
- public String getTableName() {
- return tableName;
- }
- public void setTableName(String tableName) {
- this.tableName = tableName;
- }
- public String getOrderColimn() {
- return orderColimn;
- }
- public void setOrderColimn(String orderColimn) {
- this.orderColimn = orderColimn;
- }
- public boolean isPageMode() {
- return pageMode;
- }
- public void setPageMode(boolean pageMode) {
- this.pageMode = pageMode;
- }
- public int getFirstResult() {
- return firstResult;
- }
- public void setFirstResult(int firstResult) {
- this.firstResult = firstResult;
- }
- public int getLastResult() {
- return lastResult;
- }
- public void setLastResult(int lastResult) {
- this.lastResult = lastResult;
- }
- public boolean isOrderMode() {
- return orderMode;
- }
- public void setOrderMode(boolean orderMode) {
- this.orderMode = orderMode;
- }
- public String getOrderSort() {
- return orderSort;
- }
- public void setOrderSort(String orderSort) {
- this.orderSort = orderSort;
- }
- }
|
然后编写hbm.xml
放一个出来看看吧 Cosmetics.hbm.xml
- Cosmetics.hbm.xml
-
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
- <hibernate-mapping>
- <class name="com.ldz.model.cosmetic.Cosmetics" table="cosmetic_cosmetics">
- <id name="id" column="id" length="10" type="java.lang.Long">
- <generator class="native"></generator>
- </id>
- <property name="name" column="name" length="50" type="java.lang.String"></property>
- <property name="describes" column="describes" length="500" type="java.lang.String"></property>
- <property name="is_recommend" column="is_recommend" length="1" type="java.lang.Byte"></property>
-
- <many-to-one name="type" column="type_id" class="com.ldz.model.cosmetic.Type"></many-to-one>
- <many-to-one name="function" column="function_id" class="com.ldz.model.cosmetic.Functions"></many-to-one>
- <many-to-one name="result" column="result_id" class="com.ldz.model.test.Result"></many-to-one>
-
- <set name="imageSet" table="cosmetic_image">
- <key column="cosmetics_id"></key>
- <one-to-many class="com.ldz.model.cosmetic.Image"/>
- </set>
- <set name="commentSet" table="cosmetic_comment">
- <key column="cosmetics_id"></key>
- <one-to-many class="com.ldz.model.comment.Comment"/>
- </set>
- </class>
- </hibernate-mapping>
|
今天暂时就写到这里了,下次会写ssh框架的搭建了