前言: 在上一篇的环境设置完成后,现在我们可以开始来搭建ssm框架,如果现在MyEclipse还没有配置,可以参考我之前的博文,戳我
第一步
现在基本上都是用mvn,全名是Maven来管理jar包,mvn能够很方便的帮你管理项目报告,生成站点,管理JAR文件等等,所以我们首先要先配置mvn,如果还没下载的同学可以到官网下载一下戳我,这里我主要说一下如何配置:
- 首先,下载好之后要配置环境,配置方法和java类似,在系统变量中新建一个MAVEN_HOME 将解压出来后的Maven路径放在这个变量里,然后再PATH 路径后面上如== %MAVEN_HOME%\bin ==路径。
- 测试是否配置成功,可以在cmd里面输入 echo %MAVEN_HOME%查看指向Maven存放安装目录是否正确,输入mvn-v查看能否找到正确的mvn执行脚本。
- 初次安装完后输入mvn help:system的命令,此时会打印出所有的Java系统属性和环境变量,然后Maven会执行一个真正的任务将相应的所需的构件下载到本地仓库中包括pom和jar文件,然后用户在C:\Users\Administrator.m2下就能看到本地仓库了
第二步,MyEclipse配置Maven
在第一步确定mvn成功安装后,我们就可以开始在MyEclipse中配置mvn。
- 在本地创建一个文件夹叫MavenRepository,并在文件夹下创建一个repository。
- 然后再解压出来的mvn中的conf文件夹下面复制settings.xml配置文件,复制到刚刚创建的MavenRepository文件夹下
- 打开MavenRepository文件夹下面的settings.xml配置文件并找到localRepository标签,去掉注释以及把中间路径换成刚刚创建的MavenRepository中的repository中,如下图所示:
4.然后就在自己的MyEclipse中的Preferences中进行配置mvn,如下图所示:
然后把User Settings设置成之前修改的setting.xml,如下所示
5.验证:回到我们的MyEclipse中,点击File–>New–>others,收缩maven,如果看到小如下所示则代表配置成功:
第三步:使用mvn创建一个web项目
跟着图片操作:
创建完是这样,因为Myeclipse8出了点问题,所以我用Myeclipse2018代替:
第四步:修改配置
为了让mvn项目能够挂在Tomcat上运行,我们得修改一些配置,首先,右击项目然后点击Properties,
然后这个时候我们会发现pom.xml报错了,错误的原因是因为没有web.xml。
第五步,添加web.xml
右击项目,选中 JaveEE.Tools ,然后选中 Generate Deployment Descriptor Stub ,然后我们就可以看到Myeclipse自动帮我们创建好了web.xml,如下图所示:
第六步,利用mvn仓库引入相关jar包
首先我们要先把maven的窗口打开,然后重建索引,点击 window,然后点击 show view ,点击 other ,输入maven,然后点击第一个maven窗体就出来了,如图所示:
然后如下图操作:
右击选中ReBuild index,这样本地仓库下面的所有jar包就都在了。
根据上图操作选择自己需要的jar包就可以了。
第七步:在src/main/resources下面添加配置文件
总体配置完如下所示:
我将在下面写出各个配置文件的详细信息:
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xandl</groupId>
<artifactId>ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<webVersion>4.0</webVersion>
</properties>
<!-- 要用到的jar包 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<type>pom.lastUpdated</type>
</dependency>
<!-- spring框架包 start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- spring框架包 end -->
<!-- mybatis框架包 start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mybatis框架包 end -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<type>pom.lastUpdated</type>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<type>pom.lastUpdated</type>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
<type>pom.lastUpdated</type>
</dependency>
</dependencies>
<!-- 由于mvn默认是他自己定义的版本,所以通过这个改成我们想要的 -->
<build>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
mybatis/SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 根据需要编写 -->
</configuration>
spring/applicationContext-dao.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 获取数据库配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="drivateClassName" value="${jdbc.driver}"/>
<property name="maxActive" value="10"/>
<property name="minIndle" value="5"/>
</bean>
<!-- 让spring管理sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- myBatis的全局配置文件 -->
<property name="configLacation" value="classpath:mybatis/SqlMapConfig.xml"></property>
</bean>
<!-- 自动扫描Mapping扫描 -->
<bean calss="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="#{路径}" />
</bean>
</beans>
spring/applicationContext-service.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
设置service扫描
<context:component-scan base-package="#{扫描}"></context:component-scan>
</beans>
spring/applicationContext-trans.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
事务管理器
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
数据源
<property name="dataSource" ref="dataSource" />
</bean>
通知
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
传播行为
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
切面
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.service.*.*(..))" />
</aop:config>
</beans>
spring/springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 设置扫描controller -->
<context:component-scan base-package="#{扫描conteoller}" />
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 对静态资源放行 -->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
至此配置到一段落,剩下就可以正式开始编写。