距离上次发文得有半个月了,最近学校破事比较多,什么设计logo啦,flush啦,ps啦,好不容易抽空把框架搭起来。
先来看看项目结构,ssh三层框架,dao层、service层、action层,dao层和service层有 impl 包存放实现类,工厂类获取spring对象(这个写多了,用spring注解的话就用不上了)。
这里就发最核心的BaseDaoImlp.java好了,这是一个通用类,可以转移到其他ssh程序里,不过写得应该不好,还有待改进
@Transactional
@Repository
public abstract class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{
public abstract Class getEntityClass() throws Exception;
@Resource(name = "sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
@Override
public void save(Entity bean) throws Exception {
if (bean == null) {
throw new Exception("save fail, bean is null");
} else {
getHibernateTemplate().save(bean);
return;
}
}
@Override
public void delete(Entity bean) throws Exception {
if (bean == null){
throw new Exception("delete fail, bean is null");
} else if (bean.getId() == null) {
throw new Exception("delete fail, id is null");
} else {
getHibernateTemplate().delete(bean);
return;
}
}
@Override
public void update(Entity bean) throws Exception {
if (bean == null){
throw new Exception("update fail, bean is null");
} else if (bean.getId() == null){
throw new Exception("update fail, id is null");
} else {
getHibernateTemplate().update(bean);
return;
}
}
@Override
public Entity getBean(Long id) throws Exception {
if (id == null)
throw new Exception("search fail, id is null");
Entity bean = null;
Session session = getSessionFactory().getCurrentSession();
try {
/*bean = (Entity) session.get(getEntityClass(), id);*/
bean = (Entity) getHibernateTemplate().get(getEntityClass(), id);
} catch (Exception ex) {
try {
} catch (Exception e) {
e.printStackTrace();
}
throw ex;
}
return bean;
}
@Override
public List getBeanListByQuery(QueryBuilder query) throws Exception {
StringBuffer sql = new StringBuffer();
sql.append("SELECT * FROM ").append(query.tableName);
Session session = getSessionFactory().openSession();
List<Entity> result = null;
try {
SQLQuery sqlQuery = session.createSQLQuery(sql.toString()).addEntity(getEntityClass());
result = sqlQuery.list();
} catch (Exception e) {
e.printStackTrace();
} finally{
session.close();
}
return result;
}
@Override
public List getBeanListForPageByQuery(QueryBuilder query) throws Exception{
StringBuffer sql = new StringBuffer();
if(query.countMode){
sql.append("SELECT *");
} else {
sql.append("SELECT ");
if(query.alias != null){
sql.append(query.alias);
}
}
sql.append(" FROM ");
if(query.tableName != null){
sql.append(query.tableName);
} else {
new Exception("no table name");
return null;
}
if(query.orderMode && query.orderColimn != null){
if(query.orderColimn != null){
sql.append(" ORDER BY ").append(query.orderColimn);
}
if(query.orderSort != null){
sql.append(" ").append(query.orderSort);
}
}
if(query.isPageMode() && (query.firstResult != -1 || query.lastResult != -1)){
sql.append(" LIMIT ");
if(query.firstResult > 0){
sql.append(query.firstResult).append(",");
}
if(query.lastResult > 0){
sql.append(query.lastResult);
}
}
System.out.println(sql);
Session session = getSessionFactory().openSession();
List<Entity> result = null;
try {
Query sqlQuery = session.createSQLQuery(sql.toString());//.addEntity(getEntityClass()); 加了之后按列查询失效
result = sqlQuery.list();
} catch (Exception e) {
// TODO: handle exception
} finally{
session.close();
}
return result;
}
}
然后是spring的配制文件(主要是连接数据库的配制,其它注入什么的基本都用注解了,白费我还多建了好几个文件想分清楚点)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载properties -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:com/ldz/conf/properties/jdbc.properties"/>
<!-- c3p0数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置hibernate相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置hibernate的属性 -->
<property name="hibernateProperties">
<props>
<!-- 此处编写时特别注意不要打错了否则数据创建失败MySQL使用的是5.0版本以上org.hibernate.dialect.MySQL5Dialect -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
<!--加载hibernate中的映射文件 -->
<property name="mappingResources">
<list>
<!-- 化妆品 -->
<value>com/ldz/conf/hibernate/cosmetic/Cosmetics.hbm.xml</value>
<value>com/ldz/conf/hibernate/cosmetic/Type.hbm.xml</value>
<value>com/ldz/conf/hibernate/cosmetic/Functions.hbm.xml</value>
<value>com/ldz/conf/hibernate/cosmetic/Image.hbm.xml</value>
<value>com/ldz/conf/hibernate/cosmetic/Like.hbm.xml</value>
<!-- 评论 -->
<value>com/ldz/conf/hibernate/comment/Comment.hbm.xml</value>
<value>com/ldz/conf/hibernate/comment/Image.hbm.xml</value>
<value>com/ldz/conf/hibernate/comment/Reply.hbm.xml</value>
<!-- 测试 -->
<value>com/ldz/conf/hibernate/test/Answer.hbm.xml</value>
<value>com/ldz/conf/hibernate/test/Number.hbm.xml</value>
<value>com/ldz/conf/hibernate/test/Problem.hbm.xml</value>
<value>com/ldz/conf/hibernate/test/Result.hbm.xml</value>
<!-- 用户 -->
<value>com/ldz/conf/hibernate/user/Collection.hbm.xml</value>
<value>com/ldz/conf/hibernate/user/Note.hbm.xml</value>
<value>com/ldz/conf/hibernate/user/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置自动扫描的包 开启注解 -->
<context:component-scan base-package="com.ldz">
</context:component-scan>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ssh_cosmetics</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2的框架的核心 过滤器的配置 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,
struts-plugin.xml,
com/ldz/conf/struts/struts_test.xml,
com/ldz/conf/struts/struts_user.xml,
com/ldz/conf/struts/struts_cosmetic.xml,
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<!-- Spring框架的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定加载指定路径下的applicationContext.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/ldz/conf/spring/applicationContext.xml</param-value>
</context-param>
</web-app>
action层因为还没有链上前台代码,所以没怎么写,现在主要测试一下各个方法是否好用,配制是否写错,接下来就要开始连接前台代码,开始做界面了