使⽤Eclipse搭建SSM框架(Spring+SpringMVC+Mybatis)1.创建项⽬
1)打开Eclipse,点击File --> New --> Other
2)输⼊maven,到Maven Project
3)然后⼀直按Next,直到出现⼀下界⾯:
4)输⼊web后点击Aftifact Id为maven-archetype-webapp的选项,然后继续Next
5)然后输⼊你的Group Id和项⽬名,点击finish
6)项⽬创建好之后可能会出现如下情况:
这是因为项⽬中缺少Tomcat有关jar包导致的,接下来我们就来解决这个问题,右键单击项⽬,选择Build Path -->  Add Library -->  Server Runtime,然后点击Next
然后选择你要的Tomcat容器,点击Finish
这个时候问题就解决了
7)更改Dynamic Web Module和jdk版本号。更改之前是这样的:
⽽我电脑⾥配的Java环境是jdk1.8的,Tomcat的版本是9.0,可以将Dynamic Web Module版本改成3.1,但是不能直接在上⾯更改,接下来我就详细讲⼀下怎么更改这两个版本:
⾸先到你那个项⽬所在的路径,打开那个项⽬⽂件夹之后到.settings⽂件夹,这个在你那⾥也可能被隐藏,记得打开显⽰隐藏⽂件夹的选项
然后进⼊.settings⽂件夹,到lipse.wstmon.ml⽂件
打开⽂件后改成你对应的版本
    更改前》》》》》》》》
    更改后》》》》》》》
然后回到eclipse中,到l⽂件,将其修改为(对应Dynamic Web Module修改后的版本,我这⾥是3.1):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="/xml/ns/javaee" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd"
jdk怎么使用version="3.1" metadata-complete="true">
</web-app>
第四部修改l⽂件(对应修改后的jdk版本),将其修改为:
<build>
<plugins>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
最后右击项⽬,选择Maven --> Update Project
弹出界⾯后勾选你当前的项⽬点击OK即可
2.导⼊SSM项⽬所需要的jar包
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId&st</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url></url>
<properties>
<!-- Spring版本号 -->
<spring.version>4.3.8.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring相关包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- AOP相关包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.0</version>
<!-- MyBatis相关包 -->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- MySQL相关包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.20</version>
</dependency>
<!-- Spring集成MyBatis -->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
<!-- JSP标准标签库 -->
<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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!-- 单元测试相关包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring对测试框架的简单封装功能 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</plugins>
</build>
</project>
3.完善项⽬结构
相信⼤家细⼼的话都会发现,之前创建的项⽬⽬录少了⼀些东西:src/main/java 、src/test/java 、src/test/resources
如果⼤家的情况和我⼀样的话,⾸先我们右击项⽬,选择 Build Path --> Configure Build Path,进⼊界⾯将有错误的⽂件Remove后点击Apply And Close
然后右击项⽬,选择 New --> Source Folder
输⼊名字后点击Finish,依次将缺失的⼏个补齐
补齐之后,再右击项⽬,选择 Build Path --> Configure Build Path,进⼊界⾯,到先前补齐的⽂件,更改Output Folder输出路径
依次更改好之后点击Apply And Close
将以上步骤做好之后,再右击项⽬,选择Properties,到Deployment Assembly,移除以下两个⽂件夹后点击Apply And Close 4.创建配置⽂件
log4j.properties⽂件:
#USE THIS SETTING FOR OUTPUT MYBATIS`s SQL ON THE CONSOLE
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 设置别名,对应实体类包名 -->
<typeAliases>
<package name="com.zqz.pojo"/>
</typeAliases>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="
/schema/beans /schema/beans/spring-beans.xsd
/schema/context /schema/context/spring-context.xsd
/schema/mvc /schema/mvc/spring-mvc.xsd">
<!-- 启动⾃动扫描 -->
<context:component-scan base-package="com.zqz.*"/>
<!-- 注册MVC注解驱动 -->
<mvc:annotation-driven />
<!-- 静态资源可访问的设置⽅式 -->
<mvc:default-servlet-handler />
<!-- 配置视图解析器,可以显式设置,也可以不设置,不设置会依据SpringMVC的默认设置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter" class="org.verter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:context="/schema/context"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
">
<!-- 指定扫描的包,对应service的实现类的包 -->
<context:component-scan base-package="com.zqz.students.service.impl"/>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。