目的:从头搭建工程,然后运行起来一个单表。把基本的流程跑通。
搭建父工程
1,创建一个Maven Project
2,将打包模式选择pom
3,选择默认目录结构(就是simple上面那个对号)
分别搭建子工程
1,先搭建utils一个子工程
1.1,选择创建Maven Moudle,打包形式选为jar。
1.2,选择默认目录结构,关联父工程。
2,按照同样方式分别搭建service,dao,domain,exception工程
3,搭建web工程
3.1,打包结构选为war
3.2,创建WEB-INF文件夹和Web文件
应该就不报错了。
导入工具类
具体的工具类位置,我之后试试能不能传到Github上,如果能就方便了。
建表
视频用的是Oracle数据库,我还是选择使用MySQL。这样争取报错改一改,浪费一下时间。这个建表语句是从powerDesigner上取下来的
drop table if exists DEPT_P;
/*==============================================================*/
/* Table: DEPT_P */
/*==============================================================*/
create table DEPT_P
(
DEPT_ID varchar(40) not null,
DEPT_NAME varchar(50),
PARENT_ID varchar(40) comment '自关联',
STATE INT comment '1启用0停用',
primary key (DEPT_ID)
);
建表成功。
操作部门表,写domain
1,正常写个domain就行。
2,写xml文件
在resources文件夹下创建与domain一样的包,这样实体类和对应的mapper文件就会在一个物理文件夹下。
<?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 package="com.liuyang19900520.liussh.domain">
<class name="Dept" table="dept_p">
<id name="id" column="DEPT_ID">
<generator class="uuid"></generator>
</id>
<property name="deptName" column="DEPT_NAME"></property>
<property name="state" column="STATE"></property>
<!-- 自关联及多对一关系 -->
<many-to-one name="parent" class="Dept" column="PARENT_ID"></many-to-one>
</class>
</hibernate-mapping>
关于这些地方的注释,我最后一点一点满满补吧。各类配置文件等等的。先搭起来工程看看效果。
dao工程基础配置
1,base类导入
还是准备上传到GitHub上。
2,添加依赖
点击dao层下面的配置文件,进入到依赖页面,然后依赖utils。
service工程基础配置
1,配置文件
由于为了先运行起来环境,所以service我们不做什么操作,就先导进来一个空的文件
applicationContext-service.xml。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
2,添加依赖
web工程基础配置
1,导入美工文件
把所有的页面相关的内容都放入到工程中,GitHub上见(虽然我不确定自己能不能传得上去)
发现index文件上如下
<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
</head>
<script type="text/javascript">
window.location.href = "login"; //javascript页面跳转
</script>
<body>
</body>
</html>
我也是粘贴别人写好的项目,所以跳转都写好了。大概就是初始化就登录的意思。
虽然现在很少有人用了,但是这个项目是需要用到Struts2的,于是需要配一些配置。
2,Struts2配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="default" namespace="/" extends="struts-default">
<!--全局结果视图-->
<global-results>
<result name="error">/WEB-INF/pages/error.jsp</result>
</global-results>
<!--全局异常处理-->
<global-exception-mappings>
<exception-mapping exception="com.liuyang19900520.liussh.exception.SysException" result="error"></exception-mapping>
<exception-mapping exception="java.lang.Exception" result="error"></exception-mapping>
</global-exception-mappings>
<action name="login" method="login" class="loginAction">
<result name="login">/WEB-INF/pages/sysadmin/login/login.jsp</result>
<result name="success">/WEB-INF/pages/home/fmain.jsp</result>
</action>
<action name="logout" method="logout" class="loginAction">
<result name="logout">/WEB-INF/pages/sysadmin/login/logout.jsp</result>
</action>
<action name="homeAction_*" method="{1}" class="homeAction">
<result name="fmain">/WEB-INF/pages/home/fmain.jsp</result>
<result name="title">/WEB-INF/pages/home/title.jsp</result>
<result name="toleft">/WEB-INF/pages/${moduleName}/left.jsp</result>
<result name="tomain">/WEB-INF/pages/${moduleName}/main.jsp</result>
</action>
</package>
<!-- 分模块开发 -->
<include file="struts2/struts-sysadmin.xml"></include>
<include file="struts2/struts-cargo.xml"></include>
<include file="struts2/struts-stat.xml"></include>
<include file="struts2/struts-activiti.xml"></include>
</struts>
3,Spring整合action配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="loginAction" class="com.liuyang19900520.liussh.action.LoginAction" scope="prototype"></bean>
<bean id="homeAction" class="com.liuyang19900520.liussh.action.HomeAction" scope="prototype"></bean>
</beans>
4,applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 1.dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://127.0.0.1:3306/liussh?characterEncoding=utf8" />
<property name="user" value="root" />
<property name="password" value="1234" />
</bean>
<!-- 2.sessionFactory -->
<bean id="sessionFactoryliu"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!--2.Shiro安全框架产生代理子类的方式: 使用cglib方式 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 3.事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryliu"></property>
</bean>
<!-- 4.txAdvice -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 5.aop -->
<aop:config>
<aop:pointcut id="pointcut"
expression="execution(* com.liuyang19900520.liussh.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<!--组装其它 配置文件 -->
<import resource="classpath*:applicationContext-action.xml"></import>
<import resource="classpath*:applicationContext-dao.xml"></import>
<import resource="classpath*:applicationContext-service.xml"></import>
<!-- <import resource="classpath:applicationContext-shiro.xml"></import>
<import resource="classpath:applicationContext-job.xml"></import> -->
<!-- <import resource="classpath:applicationContext-activiti.xml"></import> -->
</beans>
5,hibernate.cfg.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>
<!-- 驱动配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/liussh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234 </property>
<!-- 选择mysql方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 基本配置 -->
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="hbm2ddl.auto">none</property>
<!-- 懒加载,配合web.xml中配置的 openSessionInViewFilter -->
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<!--校验模式 JPA java persistent api -->
<property name="javax.persistence.validation.mode">none</property>
<!-- 加载映射文件 -->
<mapping resource="com/liuyang19900520/liussh/domain/Dept.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
6,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!--1.spring配置文件的加载的监听 器 -->
<!-- 加载核心配置文件,在核心配置文件中,会导入各个模块的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2.懒加载 OpenSessionInviewFilter noSession or session is closed -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<!-- 这个session是否是单例,不配置默认也是ture -->
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<!-- 这部分是与Spring中SessionFactory相对应 -->
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactoryLiuSSHTest</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3.struts2核心控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--CharacterEncodingFilter进行编码过滤 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
发布工程&日常改错
1,将Web工程发布到Tomcat中,运行,等着审判。
2,改错开始
2.1,Mapping not found
我之前报这个错,重新写了一遍mapping就好了。不要在意截图,截图上的错误是故意复现的。mapping注意好对应的关系,实在不行就找过去比较准的工程粘贴上。
2.2,Failed to instantiate Specified class is an interface
这部分应该是dao工程的配置文件出了问题,之前我将这里配置成了接口,应该配置实现类。
2.3,Cannot find class [com.liuyang19900520.liussh.service.impl.DeptServiceImpl]
这个图不截屏了,应为我们service层不准备写东西,所以先把这个bean注释掉为好。
2.4 Struts配置文件错误
tomcat虽然已经没有问题了,但是跑起来页面却出现了这个问题。
看起来好像是action这个配置文件出了问题,之前的报名我没有写liussh。
3,再次启动
其实已经准备好写2.5了,不过成功了。
充满了培训气息的一个web工程,虽然工作了几年,却不一定有培训生做得好。
一步一步来吧。
4,修正一下菜单
如果导入的源码可能菜单应该是动态加载的。所以执行下面的操作。
这样系统管理就能够跑出来菜单了。
今天就到这吧。我自己想想eclipse如何传git。