虽然现在流行SpringBoot了,但是对于以前的SSM配置地狱的框架搭建整合做一个简单的Demo
首先创建新的项目,选择Maven管理我们的依赖 如图所示:
当然我是为了做列子,所以采用的中文,
然后选择自己的maven地址,这里稍微注意的是,Maven默认读取的是 .m2/setting.xml作为配置文件,
我改动了配置文件的相关下载和配置文件存储路径,所以我选用我自己的配置文件作为启动配置
然后等待下载,下载完之后,项目构建如下:
然后创建java 文件夹,作为我们存放代码的文件,并在下面创建出响应的 package 模块
最后就是这样的
——————————前期的基本准备工作到此为止,下面开始搭建环境————————————
先把最后的样子贴出来,可以先把结构搭起来,再填充内容:
1:优先配置 maven的 pom.xml 导入相关依赖
1 <dependencies>
2 <!--Spring-->
3 <dependency>
4 <groupId>org.springframework</groupId>
5 <artifactId>spring-core</artifactId>
6 <version>5.1.5.RELEASE</version>
7 </dependency>
8 <dependency>
9 <groupId>org.springframework</groupId>
10 <artifactId>spring-beans</artifactId>
11 <version>5.1.5.RELEASE</version>
12 </dependency>
13 <dependency>
14 <groupId>org.springframework</groupId>
15 <artifactId>spring-context</artifactId>
16 <version>5.1.5.RELEASE</version>
17 </dependency>
18 <dependency>
19 <groupId>org.springframework</groupId>
20 <artifactId>spring-jdbc</artifactId>
21 <version>5.0.8.RELEASE</version>
22 </dependency>
23 <dependency>
24 <groupId>org.springframework</groupId>
25 <artifactId>spring-tx</artifactId>
26 <version>5.0.8.RELEASE</version>
27 </dependency>
28 <dependency>
29 <groupId>org.springframework</groupId>
30 <artifactId>spring-web</artifactId>
31 <version>5.0.8.RELEASE</version>
32 </dependency>
33 <dependency>
34 <groupId>org.springframework</groupId>
35 <artifactId>spring-webmvc</artifactId>
36 <version>5.0.8.RELEASE</version>
37 </dependency>
38 <dependency>
39 <groupId>org.springframework</groupId>
40 <artifactId>spring-test</artifactId>
41 <version>5.0.8.RELEASE</version>
42 </dependency>
43
44 <!--MyBatis 依赖-->
45 <dependency>
46 <groupId>org.mybatis</groupId>
47 <artifactId>mybatis</artifactId>
48 <version>3.4.6</version>
49 </dependency>
50 <dependency>
51 <groupId>org.mybatis</groupId>
52 <artifactId>mybatis-spring</artifactId>
53 <version>1.3.2</version>
54 </dependency>
55
56 <!--连接池 采用阿里的druid-->
57 <dependency>
58 <groupId>com.alibaba</groupId>
59 <artifactId>druid</artifactId>
60 <version>1.1.6</version>
61 </dependency>
62 <!--mysql-->
63 <dependency>
64 <groupId>mysql</groupId>
65 <artifactId>mysql-connector-java</artifactId>
66 <version>5.1.44</version>
67 </dependency>
68 <!--lombok 推荐使用这个依赖,注解解决掉set get...-->
69 <dependency>
70 <groupId>org.projectlombok</groupId>
71 <artifactId>lombok</artifactId>
72 <version>1.16.22</version>
73 </dependency>
74 <!--jackson-->
75 <dependency>
76 <groupId>com.fasterxml.jackson.core</groupId>
77 <artifactId>jackson-annotations</artifactId>
78 <version>2.9.7</version>
79 </dependency>
80 <!-- 通用mapper -->
81 <dependency>
82 <groupId>tk.mybatis</groupId>
83 <artifactId>mapper</artifactId>
84 <version>4.1.5</version>
85 </dependency>
86 <!--单元测试 Junit-->
87 <dependency>
88 <groupId>junit</groupId>
89 <artifactId>junit</artifactId>
90 <version>4.12</version>
91 </dependency>
92 <!--日志打印-->
93 <dependency>
94 <groupId>ch.qos.logback</groupId>
95 <artifactId>logback-core</artifactId>
96 <version>1.2.3</version>
97 </dependency>
98 <dependency>
99 <groupId>ch.qos.logback</groupId>
100 <artifactId>logback-classic</artifactId>
101 <version>1.2.3</version>
102 </dependency>
103 <dependency>
104 <groupId>org.slf4j</groupId>
105 <artifactId>slf4j-api</artifactId>
106 <version>1.7.25</version>
107 </dependency>
108 </dependencies>
109
110 <build>
111 <finalName>ChatRobot</finalName>
112 <plugins>
113 <plugin>
114 <groupId>org.apache.maven.plugins</groupId>
115 <artifactId>maven-compiler-plugin</artifactId>
116 <configuration>
117 <!-- 设置JDK版本 -->
118 <source>1.8</source>
119 <target>1.8</target>
120 </configuration>
121 </plugin>
122 </plugins>
123 </build>
然后配置web.xml
<!-- 配置监听器加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringBeans的配置文件所在目录-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- 配置 SpringMVC前端控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 修改默认springmvc加载的配置文件路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!-- 配置Spring的启动时机 >=0随着容器一起启动 <0第一次来请求时启动 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--设置一个默认访问的页面-->
<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>
web.xml中指定的文件,我们按部就班的造出来
依次为: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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置注解扫描 -->
<context:component-scan base-package="com.test.ssm.service" />
<!-- 加载数据源所需要的参数配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 让Spring管理myBatis的会话工厂 SqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--扫描Model包,使用别名-->
<property name="typeAliasesPackage" value="com.test.ssm.model"/>
<!-- 扫描sql配置文件: mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入Dao-->
<property name="basePackage" value="com.test.ssm.dao"/>
<!--注入sqlSession工厂-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--配置事务管理器-->
<bean id="trancationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启声明式事务-->
<tx:annotation-driven transaction-manager="trancationManager"/>
</beans>
其次:springmvc.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
8 http://www.springframework.org/schema/mvc
9 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context-3.2.xsd">
12
13 <!-- 1.配置注解扫描位置 -->
14 <context:component-scan base-package="com.test.ssm.controller" />
15
16 <!-- 2.配置注解处理映射 3.配置适配器-->
17 <mvc:annotation-driven />
18
19 <!-- 4.配置springmvc视图解析器 视图解析器解析的视频路径为:前缀 + 后缀 -->
20 <bean
21 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22 <property name="prefix" value="/WEB-INF/views" />
23 <property name="suffix" value=".jsp" />
24 </bean>
25 </beans>
然后 applicationContext.xml 又依赖新的配置文件,逐一配置出来
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8jdbc.username=rootjdbc.password=root
logback.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <configuration debug="true">
3 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4 <encoder>
5 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6 </encoder>
7 </appender>
8 <root level="INFO">
9 <appender-ref ref="STDOUT"/>
10 </root>
11 </configuration>
运行服务器,看看效果
自动跳转到了默认的index.jsp
测试一下数据库,查询一点数据看看能否连通
发现自己都不太熟悉了,希望以后再也用不上!