带你从零开始,开发一个阅读站第四章,后台框架搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带你从零开始,开发一个阅读站第四章,后台框架搭建相关的知识,希望对你有一定的参考价值。
首先,万分抱歉,据上一篇更新后,时隔已近一个月。虽然博主不日念起此事,但实在是最近繁事缠身,说这些也是想告诉对此文有所期待的朋友,博主一定会更完到最后一章,不尽人意之处,还请各位多担待。如果不出意外,博主还是努力一周更两篇。
前面,我们看过项目实际运行的功能介绍,表结构也有所了解。今天,我们就正式进入编码工作。代码写到哪,博主会将代码实时提交到git,有需要的朋友,可以自行前往下载。地址:https://git.oschina.net/input4hua/input4hua_reader.git
首先我们在的开发工具,也就是eclipse上新建一个maven项目,如果这一步不清楚的同学,可以网上找下这方面的资料。我们的项目也是用maven构建的。
我这里给出了项目的包结构,相应的也给出了每个包的说明。
domain是和实体类相关,其下有
dao.base,dao,dao.impl,这三个包是数据持久层,即这一层与数据库打交道。
infra提供了各层公用模块,包括其下有
utils工具包,VO展示对象
web即网络访问层,其下controller包即是控制层,接收通过网络发送过来的请求
整体流程就是controller控制层接收请求,将请求数据交由service服务层处理。服务层里处理逻辑,并在有必要的时候调用dao层从数据库请求数据,dao层从数据库得到数据后,通过entity层相应的实体类返回给service层。service层得到dao层返回来的实体类数据,做逻辑处理并转换封装成VO类,返回给controller层。controller层调用视图解析器(我们这里是freemaker模板引擎),将数据展现到页面,如下图所示
接下来,我们来看一下pom文件,maven项目通过pom来管理整个项目所需jar包依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.input4hua</groupId> <artifactId>myreader</artifactId> <version>1.0.0</version> <packaging>war</packaging> <name>myreader</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>javaee-api</artifactId> <version>5.0-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!-- mysql数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <!-- aspectjweaver.jar这是Spring AOP所要用到的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.1</version> </dependency> <!-- hibernate4 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.2.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.2.5.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.3.RELEASE</version> </dependency> <!-- freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> <!-- spring3 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.0.RELEASE</version> </dependency> <!--jsp页面使用的jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <!-- 数据源 c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5-pre8</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.12</version> </dependency> <!-- Jackson JSON Mapper --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.10</version> </dependency> <!-- Redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8081</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <finalName>myreader</finalName> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> </project>
从这个pom文件,大家就能看到我们这个项目所用到的一些组件了。需要说的是
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8081</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <finalName>myreader</finalName> <server>tomcat7</server> </configuration> </plugin>
这个插件配置后,我们便可以不再在eclipse里配置tomcat了
具体操作如下所示:
如上图所示,点击debug后的小倒三角形,打开Debug Configuations面板,在左侧Maven Build上点击右键New,如下图顺序
在上方Name:取名,这里随意。
在Glals:后填入clean install注意这里有空格
而后,点击Workspace…按钮选择要关联的项目,这里是myreader
然后,切换tab页到JRE ,如下图选择jdk为1.7
接下来,切换tab页到Source关联一个源码项目,如下图,这里选择myreader
最后Apply应用
这一步完成之后,我们先暂且放在这里,因为这个时候点击Debug是会报错的。因为我们的项目可能缺少文件。
如下图所示,我们需要添加一个web.xml文件在webapp下的WEB-INF
接下来我们就结合web.xml里的内容来看看resource下的各个配置文件
首先是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" version="2.5"> <display-name></display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-hibernate.xml</param-value> </context-param> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
我们配置上下文参数contextConfigLocation来指定spring配置文件路径
配置一个openSessionInViewFilter过滤器
配置一个springMVC相关联的servlet,并制定springMVC配置文件路径
encodelingFiter是编码过滤器
下面是配置文件的介绍
common.properties
#flie save place 文件保存路径,这里指的是图书每章内容的文本保存根目录
contentBasePath=F:/mrFile/
#image save place 普通保存路径,即图书封面图片保存的根目录
imageServerPath=http://192.168.11.9:8085/imgPath/
#每页章节数目
menu_pageSize=100
#每页图书数目
book_pageSize=20
#首页图书数目
book_homeSize=50
#模糊查询匹配图书数目
book_likeSize=5
#我的书架图书cookie时间
book_storeCookie_time=31536000
#config for redis 以下是redis的配置
redis.pool.maxActive=512
redis.pool.maxIdle=100
redis.pool.maxWait=100000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.ip=127.0.0.1
redis.port=6379
redis.expire=3600
config.properties
#MySQL \\u7684\\u914D\\u7F6E hibernate.dialect=org.hibernate.dialect.MySQLDialect driverClassName=com.mysql.jdbc.Driver validationQuery=SELECT 1 jdbc_url=jdbc\\:mysql://localhost:3306/myreader?useUnicode\\=true&characterEncoding\\=utf-8 jdbc_username=root jdbc_password=root hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false sessionInfoName=sessionInfo uploadFieldName=filedata uploadFileMaxSize=20971520 uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid uploadDirectory=attached
这里是heibernate的一些配置,包括jdbc数据库连接的参数
freemarker.properties
#\\u8BBE\\u7F6E\\u6807\\u7B7E\\u7C7B\\u578B\\uFF1Asquare_bracket:[] auto_detect:[]<> tag_syntax=auto_detect #\\u6A21\\u7248\\u7F13\\u5B58\\u65F6\\u95F4\\uFF0C\\u5355\\u4F4D\\uFF1A\\u79D2 template_update_delay=0 default_encoding=UTF-8 output_encoding=UTF-8 locale=zh_CN #\\u8BBE\\u7F6E\\u6570\\u5B57\\u683C\\u5F0F \\uFF0C\\u9632\\u6B62\\u51FA\\u73B0 000.00 number_format=\\# #\\u53D8\\u91CF\\u4E3A\\u7A7A\\u65F6\\uFF0C\\u4E0D\\u4F1A\\u62A5\\u9519 classic_compatible=true #\\u8FD9\\u4E2A\\u8868\\u793A\\u6BCF\\u4E2Afreemarker\\u7684\\u89C6\\u56FE\\u9875\\u9762\\u90FD\\u4F1A\\u81EA\\u52A8\\u5F15\\u5165\\u8FD9\\u4E2Aftl\\u6587\\u4EF6\\u3002\\u91CC\\u9762\\u5B9A\\u8BAE\\u7684\\u5C31\\u662F\\u4E00\\u4E9B\\u5B8F\\uFF0C\\u5982text\\u6587\\u672C\\u6846\\uFF0C\\u5404\\u79CDform\\u5143\\u7D20 #auto_import="/WEB-INF/templates/index.ftl" as do
log4j.properties
log4j.rootLogger = WARN , stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = /usr/local/tomcat/tomcat8080/logs/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = /usr/local/tomcat/tomcat8080/logs/error.log log4j.appender.D.Append = true log4j.appender.D.Threshold = ERROR log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
spring-hibernate.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <!-- JNDI(通过Tomcat)方式配置数据源 --> <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- 引入属性文件 --> <context:property-placeholder location="classpath:config.properties" /> <!-- 配置数据源 这里class可以使用不同的驱动--> <!-- <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> </bean> --> <!-- (重要步骤)c3p0连接池配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClassName}"></property> <property name="jdbcUrl" value="${jdbc_url}"></property> <property name="user" value="${jdbc_username}"></property> <property name="password" value="${jdbc_password}"></property> </bean> <!-- 配置hibernate session工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> --> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="current_session_context_class">thread</prop> <prop key="hibernate.connection.driver_class" >${driverClassName}</prop> </props> </property> <!-- 自动扫描注解方式配置的hibernate类文件 --> <property name="packagesToScan"> <list> <!-- 此处与entity实体路径对应 --> <value>com.input4hua.domain.entity</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
注意在hibernate配置里
<!-- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> -->
注释掉后让hibernate不为我们能建立表外键关系
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 支持注解 --> <mvc:annotation-driven /> <!--自动装配DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> <mvc:default-servlet-handler /> <!-- 设置自动扫描的路径,用于自动注入bean 这里的路径与自己的项目目录对应--> <!-- 扫描controller路由控制器 --> <context:component-scan base-package="com.input4hua.web.controller" /> <context:component-scan base-package="com.input4hua.domain.dao,com.input4hua.domain.service" /> <!-- 设置freeMarker的配置文件路径 --> <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:freemarker.properties"/> </bean> <!-- 配置freeMarker的模板路径 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="freemarkerSettings" ref="freemarkerConfiguration"/> <property name="templateLoaderPath"> <value>/WEB-INF/ftl/</value> </property> </bean> <!-- 配置freeMarker视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html; charset=utf-8"/> <property name="cache" value="true"/> </bean> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="suffix" value=".jsp" /> <property name="prefix" value="/WEB-INF/views" /> </bean> </beans>
这里配置了自动扫描的包路径,和freemarker整合,freemarker的模板路径(再下一章节的时候,我们具体说说),还有jsp的视图解析器。这里我们的项目其实并没有用到。
到这里我们今天的任务也就差不多了,最后在上文中,我们配置了myreader_cleaninstll这个配置,就是下图所示这个
现在点击Debug,项目便会打包,并下载相应的jar存放在本地仓库,这个过程可能需要些时间,如果jar下载出错,可以去本地仓库把对应出错的jar包删除的,重新保存pom文件,重新cleaninstll打包。当出现下面所示即表示打包成功:
注意配置的数据库存在,并已建立相应的表
接下来按照上午配置,再配置一个maven run,如下图:
注意这里Goals:配置的是tomcat7:run,除了Name不同外,其余配置与myreader_cleaninstall一样,包括JRE页的配置和Source页的配置
最后,点击Debug运行,首次运行的话会下载tomcat-maven运行插件,需要些时间,稍等片刻下载完成,并运行,如下图所示,便代表本项目配置运行成功
箭头所指便是我们在pom文件里配置的运行在tomcat插件上的项目访问路径
到这里,本章就结束了。
以上是关于带你从零开始,开发一个阅读站第四章,后台框架搭建的主要内容,如果未能解决你的问题,请参考以下文章