eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录
老求了,好多东西记不住了,再不记以后怕是记不住了。
eclipse JAVAEE for web Version: Mars.2 Release (4.5.2)
tomcat 7.0.29
spring3.1.1
freemarker-2.3.22
commons-io-2.5.jar 没这个东西 request.getParameter() 会报错
算了 来个JAR包截图
源码结构截图
新建tomcat server服务器
打开server管理面板,拖到左边去
window-show view -servers
servers面板空白处右键
new - server
选7.0 再取个酷炫的名字
直接finish完成 就可以在servers面板中看到了
双击myserver打开这个服务的配置
主要说下 server locations 这里的配置,相信很多新手对于这三个鬼是
一脸蒙蔽的,多半会进行鬼畜设置,然后各种报错....
先说 use custom location 如果你选择了这项 并像下面配置
你会发现..你的webapps目录下有个transfeServer 目录和跟tomacat根目录下一样的conf目录
如果你这样设定。。那么当你的tomcat运行的时候 读取是这里的server.xml 和web.xml..
tomcat根目录下conf目录的设置就没效了。。
(transfeServer是我自己取的名字)
所以各位果断选择第二个 use tomcat installation
deploy path 设置 webapps,因为需要将项目发布到tomcat 的webapps下
两个重点
当你建好server时,工作空间(eclipse workspace) 中会多出 servers/Myserver-config(我建TOMCAT server名)的目录,里面的目录和tomcat的conf目录内容一样的
什么意思呢?
就是说当RUN或者DEBUG的时候这里的配置文件会作为基准文件自动拿去覆盖
tomcat下的conf目录下的所有文件。
那么以后就在此改配置就可以了
另外一个重点
选择 use tomcat installation 后,eclipse工作空间基准配置文件servers/Myserver-config/server.xml中的 <host> 标签里面会自动多出一个虚拟目录的设置,果断删掉。。别问为什么。。
一般会在下图的位置出现
以上内容配置好后可以着手 Spring Mvc的配置
先配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | <?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" xmlns:web= "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" id= "WebApp_ID" version= "3.0" > <display-name>transferServer</display-name> <!-- 容器上下文配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/*.xml </param-value> </context-param> <!-- 加载Spring容器配置 --> <!--Spring 上下文监听器 - --> <listener> <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class > </listener> <!-- <listener> <listener- class >org.springframework.web.context.request.RequestContextListener</listener- class > </listener> --> <!-- 防止Spring内存溢出监听器 --> <listener> <listener- class >org.springframework.web.util.IntrospectorCleanupListener</listener- class > </listener> <!-- SPRINGMVC拦截器 --> <servlet> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/servlet/*.xml </param-value> </init-param> <load- on -startup>1</load- on -startup> </servlet> <!-- 使用SPRINGMVC拦截器 --> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符编码配置 --> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- <listener> <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class > </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> --> <!-- 激活Tomcat的defaultServlet来处理静态文件 --> <servlet-mapping> <servlet-name> default </servlet-name> <url-pattern>*.jpg</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>*.png</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>*.css</url-pattern> </servlet-mapping> </web-app> |
分解下web.xml中的配置

context-param 关于这个参数解释可以看这http://www.cnblogs.com/cfas/p/7851899.html
所有与Spring容器对象相关的配置都放到WEB-INF/spring/目录下
如果没有定义具体的xxx.xml 则默认会先找 applicationContext.xml 这个配置
所以WEB-INF/spring/ 需要有 applicationContext.xml 这里到底定义什么我们待会儿再说
接着配置spring 监听器 两个都要
=========================偷懒分割线===========================
还是觉的没必要解释XML的配置了。直接上XML内容吧
WEB.XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | <?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" xmlns:web= "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" id= "WebApp_ID" version= "3.0" > <display-name>transferServer</display-name> <!-- 容器上下文配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/*.xml </param-value> </context-param> <!-- 加载Spring容器配置 --> <!--Spring 上下文监听器 - --> <listener> <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class > </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener- class >org.springframework.web.util.IntrospectorCleanupListener</listener- class > </listener> <!-- spring 需要log4日志 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- 定义LOG4J监听器 --> <listener> <listener- class > org.springframework.web.util.Log4jConfigListener </listener- class > </listener> <!-- SPRINGMVC拦截器 --> <servlet> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/servlet/*.xml </param-value> </init-param> <load- on -startup>1</load- on -startup> </servlet> <!-- 使用SPRINGMVC拦截器 --> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符编码配置 --> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 激活Tomcat的defaultServlet来处理静态文件 --> <servlet-mapping> <servlet-name> default </servlet-name> <url-pattern>*.jpg</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>*.png</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>*.css</url-pattern> </servlet-mapping> </web-app> |
servlet/springMcvServlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <?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" xmlns:mvc= "http://www.springframework.org/schema/mvc" 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-4.1.xsd http: //www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能(除去带@Service注解的类) --> <context:component-scan base -package= "com.cn.xql.controller" > <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Service" /> </context:component-scan> <!-- don't handle the static resource --> <mvc: default -servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- 拦截器 --> <!--<mvc:interceptors> --> <!-- 多个拦截器,顺序执行 --> <!-- 登录认证拦截器 --> <!-- <mvc:interceptor> <mvc:mapping path= "/**" /> <bean class = "com.cn.xql.Handle" /> </mvc:interceptor> </mvc:interceptors> --> <!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能(除去带@Service注解的类) --> <!--通用视图解析器--> <bean id= "viewResolverCommon" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/views/jsp/" /> <property name= "suffix" value= ".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> <property name= "viewClass" > <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> <property name= "order" value= "2" /> </bean> <bean id= "viewResolverFreemarker" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" > <property name= "order" value= "1" ></property> <property name= "suffix" value= ".html" ></property> <property name= "contentType" value= "text/html;charset=utf-8" ></property> <property name= "viewClass" > <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value> </property> </bean> <!--freemarker配置 --> <bean id= "freemarkerConfig" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" > <property name= "templateLoaderPath" > <value>/WEB-INF/tpl/</value> </property> <property name= "freemarkerSettings" ><!-- 设置FreeMarker环境属性 --> <props> <prop key= "template_update_delay" >5</prop><!--刷新模板的周期,单位为秒 --> <prop key= "default_encoding" >UTF-8</prop><!--模板的编码格式 --> <prop key= "locale" >UTF-8</prop><!--本地化设置 --> <prop key= "datetime_format" >yyyy-MM-dd HH:mm:ss</prop> <prop key= "time_format" >HH:mm:ss</prop> <prop key= "number_format" >0.####</prop> <prop key= "boolean_format" > true , false </prop> <prop key= "whitespace_stripping" > true </prop> <prop key= "tag_syntax" >auto_detect</prop> <prop key= "url_escaping_charset" >UTF-8</prop> </props> </property> </bean> <!-- 协商视图配置 spring4 才有这个功能 这里用到了 ContentNegotiatingViewResolver ,“内容协商视图解析器”, 其实就是根据返回什么类型的视图,就协商使用哪种视图解析器。 如果返回jsp就使用InternalResourceViewResolver视图解析器, 如果返回JSON格式就使用MappingJackson2JsonView来处理。 如此而已。在 <property name= "viewResolvers" > 下的<list> 标签下, 还可以加入其他的各种视图解析器的配置。--> </beans> |
spring/applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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:aop= "http://www.springframework.org/schema/aop" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xmlns:tx= "http://www.springframework.org/schema/tx" xsi:schemaLocation="http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 支持@Autowired注解导入 --> <context:annotation-config /> <!-- 测试模型 --> <bean id= "User" class = "com.cn.xql.data.domain.User" ></bean> </beans> |
下面 这个是 mybits用的
spring/applicationContext-dao.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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- JDBC配置参数 -->
<context:property-placeholder location="classpath:jdbc.properties" order="1" ignore-unresolvable="true"/>
<!-- 认证 支持注释的事务声明 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource1"/>
<!-- 数据链接 -->
<bean id="dataSource1" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver.class0}" />
<property name="jdbcUrl" value="${jdbc.url0}" />
<property name="username" value="${jdbc.username0}"/>
<property name="password" value="${jdbc.password0}"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="idleMaxAge" value="240"/>
<property name="maxConnectionsPerPartition" value="30"/>
<property name="minConnectionsPerPartition" value="10"/>
<property name="partitionCount" value="3"/>
<property name="acquireIncrement" value="5"/>
<property name="statementsCacheSize" value="100"/>
<property name="releaseHelperThreads" value="3"/>
</bean>
<!-- 数据源 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath*:com/cn/xql/data/dao/*.xml" />
</bean>
<!-- 数据 自动生成部分-->
<!-- 用户操作日志 -->
<bean id="UserOperationLogMapperImpl" class="com.cn.xql.data.dao.impl.UserOperationLogMapperImpl" p:sqlSessionFactory-ref="sqlSessionFactoryBean"/>
</beans>
注意的问题
本次配置spring 和mybaits 都用的是3.1.1
但是为了使用mybaits 的简单日志功能 ,启用了logImpl...但是..mybaits3.1.1这个设置是没用的,
只有3.2.2才行,所以 最后配置成spring 3.1.1 mybatis3.2.3
这样配置后,运行项目时,启动会有点点延迟。。
这是mybatis-config.xml
java新手自学群 626070845
java/springboot/hadoop/JVM 群 4915800
Hadoop/mongodb(搭建/开发/运维)Q群481975850
GOLang Q1群:6848027
GOLang Q2群:450509103
GOLang Q3群:436173132
GOLang Q4群:141984758
GOLang Q5群:215535604
C/C++/QT群 1414577
单片机嵌入式/电子电路入门群群 306312845
MUD/LIB/交流群 391486684
Electron/koa/Nodejs/express 214737701
大前端群vue/js/ts 165150391
操作系统研发群:15375777
汇编/辅助/破解新手群:755783453
大数据 elasticsearch 群 481975850
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南