- 该项目封装了所有的DAO 操作,包括增删改查.
- 使用时,新建DAO类,只要继承GenericDao 就会自动拥有常用的DAO操作, 例如:
import com.common.dao.generic.GenericDao;
import com.girltest.entity.Girl;
import org.springframework.stereotype.Component;
@Component("girlDao")
public class GirlDao extends GenericDao<Girl> {
}
GenericDao 继承了com.common.dao.generic.UniversalDao
类名 | 功能 |
---|---|
.util.CreateSQLHelper | 根据hibernate注解自动生成建表sql |
.util.RedisHelper | redis 操作工具类 |
.util.PageUtil | 分页 |
.web.filter.SimpleCORSFilter | 支持跨域的拦截器 |
.web.servlet.verifycode.RandomCodeServlet | 图形验证码 |
hibernate 配置文件hibernate.cfg.xml 中
<mapping package="com.girltest.entity" />
并不好使. 所以我进行了扩展, WildCardConfiguration 是我自定义的类,继承 org.hibernate.cfg.Configuration
see com.common.util.CreateSQLHelper
see com.common.util.CreateSQLHelper
/***
* 不会真正在数据库中创建表,仅生成SQL语句<br />
* 会自动读取classpath中的hibernate.cfg.xml,若没有该文件,请创建
*/
public static void generateCreateTableSql() {
generateCreateTableSql(null);
}
/***
* 生成建表的SQL语句
* @param resource
*/
public static void generateCreateTableSql(String resource) {
org.hibernate.cfg.Configuration configuration = null;
if (ValueWidget.isNullOrEmpty(resource)) {
//会自动读取classpath中的hibernate.cfg.xml,若没有该文件,请创建
configuration = new
WildCardConfiguration().configure();
} else {
configuration = new
WildCardConfiguration().configure(resource);
}
new SchemaExport(configuration).create(true, false);
}
具体方法:
@Test
public final void test_exportSQL() {
CreateSQLHelper.generateCreateTableSql();
}
有时我们删除业务数据,比如生成一个订单,我们并不会真正物理删除,而是修改某个字段.这就是软删除.
- 在实体类增加注解:
@SoftDelete(value = "enable", columnType = String.class)
SoftDelete 的value 表示数据库中字段的名字;
columnType 表示数据库中字段的类型.
支持:String、Int、Byte、Short、Long,short
SoftDelete 的columnType 必须与实体类中成员变量的类型相同
关联抓取时增加where语句
使用方法: 使用hibernate的注解@Filter
分为两种:
- 实体类上面的@Filter;
- OneToMany 注解上的@Filter
@Entity
@FilterDef(name = "filterEnable")
@Table(name = "t_agent")
@Filter(name = "filterEnable", condition = "enable = 2")
@OneToMany(mappedBy = "primaryAgent",
cascade = {CascadeType.ALL},
fetch = FetchType.LAZY)
@Fetch(FetchMode.SUBSELECT)
@Filter(name = "filterEnable", condition = "enable = 1")
@Override
protected void enableFilter(Session session) {
session.enableFilter("filterEnable");
}