微信开发准备第二步 springmvc mybatis项目结构搭建
程序员文章站
2024-02-29 23:09:46
前面一篇有说道如何在myeclipse中搭建maven项目,这里将继续介绍如何在搭建好的基础maven项目中引入我们常用的javaweb框架——springmvc!...
前面一篇有说道如何在myeclipse中搭建maven项目,这里将继续介绍如何在搭建好的基础maven项目中引入我们常用的javaweb框架——springmvc!
①在建立好的maven项目中的pom.xml文件引入依赖,代码如下:
<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/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>wechat.cuiyongzhi.com</groupid> <artifactid>wechat</artifactid> <packaging>war</packaging> <version>0.0.1-snapshot</version> <name>wechat</name> <url>http://maven.apache.org</url> <dependencies> <!-- spring --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>3.2.0.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>3.2.0.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>3.2.0.release</version> </dependency> <!-- mybatis --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.1.1</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.1.1</version> </dependency> <!-- mysql --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.21</version> </dependency> <!-- junit测试 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <!-- mysql阿里连接池druid --> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>0.2.9</version> </dependency> <!-- spring aop包 --> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version>1.7.1</version> </dependency> <!-- json包 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.7</version> </dependency> <!-- 文件上传包 --> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.2.2</version> </dependency> <!--servlet包 --> <dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>3.0-alpha-1</version> </dependency> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>jsp-api</artifactid> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <!-- 日志包 --> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> </dependencies> <build> <finalname>wechat</finalname> </build> </project>
②修改项目路径下的web.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee" 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_3_0.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd" id="webapp_id" version="3.0"> <display-name>com.cuiyongzhi.wechat</display-name> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value> <!-- ,classpath:spring-quartz.xml 用于做任务调度 任务定时都可以 --> </context-param> <context-param> <param-name>log4jconfiglocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.log4jconfiglistener</listener-class> </listener> <context-param> <param-name>spring.profiles.active</param-name> <param-value>dev</param-value> </context-param> <context-param> <param-name>spring.profiles.default</param-name> <param-value>dev</param-value> </context-param> <context-param> <param-name>spring.livebeansview.mbeandomain</param-name> <param-value>dev</param-value> </context-param> <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> <listener> <description>spring监听器</description> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 防止内存溢出 --> <listener> <listener-class>org.springframework.web.util.introspectorcleanuplistener</listener-class> </listener> <servlet> <description>spring mvc servlet</description> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>interface_url-init_servlet</servlet-name> <servlet-class>com.cuiyongzhi.web.start.interfaceurlintiservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <session-config> <session-timeout>300</session-timeout> </session-config> <error-page> <error-code>404</error-code> <location>/web-inf/error/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/web-inf/error/error.jsp</location> </error-page> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <filter> <filter-name>druidwebstatfilter</filter-name> <filter-class>com.alibaba.druid.support.http.webstatfilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>druidwebstatfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>druidstatview</servlet-name> <servlet-class>com.alibaba.druid.support.http.statviewservlet</servlet-class> <init-param> <!-- 允许清空统计数据 --> <param-name>resetenable</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- 用户名 --> <param-name>loginusername</param-name> <param-value>cuiyongzhi</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginpassword</param-name> <param-value>123456</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>druidstatview</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!-- 访问监控页面:http://ip:port/projectname/druid/index.html --> <jsp-config> <jsp-property-group> <display-name>jspconfiguration</display-name> <url-pattern>*.jsp</url-pattern> <el-ignored>false</el-ignored> <scripting-invalid>false</scripting-invalid> <include-prelude>/web-inf/common/head.jsp</include-prelude> </jsp-property-group> </jsp-config> </web-app>
③添加数据库配置信息,这里项目配置的数据库为mysql,在 resources下新建config.properties配置文件,设置如下:
validationquery=select 1 jdbc_url=jdbc:mysql://127.0.0.1:3306/wechat?useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull jdbc_username=root jdbc_password=123456789
④在 resources下新建spring.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:context="http://www.springframework.org/schema/context" 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.0.xsd"> <!-- 引入属性文件 --> <context:property-placeholder location="classpath:config.properties" /> <!-- 自动扫描(自动注入) --> <context:component-scan base-package="com.cuiyongzhi.web.service" /> <context:component-scan base-package="com.cuiyongzhi.wechat.*" /> </beans>
⑤在 resources下新建spring-mvc.xml配置文件,设置如下:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.cuiyongzhi.web.controller" /> <!-- 避免ie执行ajax时,返回json出现下载文件 --> <bean id="mappingjacksonhttpmessageconverter" class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter"> <property name="supportedmediatypes"> <list> <value>text/html;charset=utf-8</value> </list> </property> </bean> <!-- 启动spring mvc的注解功能,完成请求和注解pojo的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <list> <ref bean="mappingjacksonhttpmessageconverter" /><!-- json转换器 --> </list> </property> </bean> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver" p:prefix="/web-inf/views/" p:suffix=".jsp" /> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="defaultencoding"> <value>utf-8</value> </property> <property name="maxuploadsize"> <value>32505856</value><!-- 上传文件大小限制为31m,31*1024*1024 --> </property> <property name="maxinmemorysize"> <value>4096</value> </property> </bean> </beans>
⑥在 resources下新建spring-mybatis.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: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 "> <!-- 配置数据源 --> <bean name="datasource" class="com.alibaba.druid.pool.druiddatasource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 --> <property name="initialsize" value="5" /> <!-- 连接池最大使用连接数量 --> <property name="maxactive" value="100" /> <!-- 连接池最大空闲 --> <property name="maxidle" value="10" /> <!-- 连接池最小空闲 --> <property name="minidle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxwait" value="60000" /> <property name="poolpreparedstatements" value="true" /> <property name="maxpoolpreparedstatementperconnectionsize" value="33" /> <property name="validationquery" value="${validationquery}" /> <property name="testonborrow" value="false" /> <property name="testonreturn" value="false" /> <property name="testwhileidle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timebetweenevictionrunsmillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minevictableidletimemillis" value="25200000" /> <!-- 打开removeabandoned功能 --> <property name="removeabandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeabandonedtimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logabandoned" value="true" /> <!-- 监控数据库 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergestat" /> </bean> <!-- mybatis文件 --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <!-- 自动扫描entity目录, 省掉configuration.xml里的手工配置 --> <property name="mapperlocations" value="classpath:com/cuiyongzhi/web/mapping/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.cuiyongzhi.web.dao" /> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> <!-- 注解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionmanager" /> --> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionadvice" transaction-manager="transactionmanager"> <tx:attributes> <tx:method name="add*" propagation="required" /> <tx:method name="append*" propagation="required" /> <tx:method name="insert*" propagation="required" /> <tx:method name="save*" propagation="required" /> <tx:method name="update*" propagation="required" /> <tx:method name="modify*" propagation="required" /> <tx:method name="edit*" propagation="required" /> <tx:method name="delete*" propagation="required" /> <tx:method name="remove*" propagation="required" /> <tx:method name="repair" propagation="required" /> <tx:method name="delandrepair" propagation="required" /> <tx:method name="get*" propagation="supports" /> <tx:method name="find*" propagation="supports" /> <tx:method name="load*" propagation="supports" /> <tx:method name="search*" propagation="supports" /> <tx:method name="datagrid*" propagation="supports" /> <tx:method name="*" propagation="supports" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionpointcut" expression="execution(* com.cuiyongzhi.web.service..*impl.*(..))" /> <aop:advisor pointcut-ref="transactionpointcut" advice-ref="transactionadvice" /> </aop:config> <!-- 配置druid监控spring jdbc --> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.druidstatinterceptor"> </bean> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.jdkregexpmethodpointcut" scope="prototype"> <property name="patterns"> <list> <value>com.cuiyongzhi.web.service.*</value> </list> </property> </bean> <aop:config> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /> </aop:config> </beans>
⑦在 resources下新建log4j.properties配置文件,用于日志的输出等级以及输出位置设置,设置如下:
到这里springmvc+mybatis的基本配置文件基本就完成了,大致的项目结构如下:
本篇主要以代码示例为主,基本的的项目搭建就记录到这里,下一篇我将简述在这个框架下的一些简单应用,感谢你的翻阅,如有疑问可以讨论!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java基础教程之封装与接口