1.创建一个Maven工程

2.通过Maven加入jar包:查询jar包的坐标添加到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.zhiyou100.maven</groupId>

  <artifactId>maven_SSM</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

 

  <!-- 添加依赖jar包 -->

  <dependencies>

 

  <!-- spring webMVC -->

       <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-webmvc</artifactId>

           <version>4.3.7.RELEASE</version>

       </dependency>

      

       <!-- 切面:Aspects -->

       <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-aspects</artifactId>

           <version>4.3.7.RELEASE</version>

       </dependency>

      

       <!-- spring jdbc -->

       <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-jdbc</artifactId>

           <version>4.3.7.RELEASE</version>

       </dependency>

      

       <!-- mysql -->

       <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

       <dependency>

           <groupId>mysql</groupId>

           <artifactId>mysql-connector-java</artifactId>

           <version>5.1.46</version>

       </dependency>

      

       <!-- c3p0 -->

       <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->

       <dependency>

           <groupId>com.mchange</groupId>

           <artifactId>c3p0</artifactId>

           <version>0.9.5.2</version>

       </dependency>

      

       <!-- mybatis -->

       <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->

       <dependency>

           <groupId>org.mybatis</groupId>

           <artifactId>mybatis</artifactId>

           <version>3.4.6</version>

       </dependency>

      

       <!-- mybatis和spring整合的jar包 -->

       <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->

       <dependency>

           <groupId>org.mybatis</groupId>

           <artifactId>mybatis-spring</artifactId>

           <version>1.3.2</version>

       </dependency>

      

       <!-- mybatis generator:逆向工程 -->

       <dependency>

           <groupId>org.mybatis.generator</groupId>

           <artifactId>mybatis-generator-core</artifactId>

           <version>1.3.5</version>

       </dependency>

      

       <!-- pagehelper:分页助手 -->

       <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->

       <dependency>

           <groupId>com.github.pagehelper</groupId>

           <artifactId>pagehelper</artifactId>

           <version>5.1.10</version>

       </dependency>

 

       <!-- log4j:日志 -->

       <!-- https://mvnrepository.com/artifact/log4j/log4j -->

       <dependency>

           <groupId>log4j</groupId>

           <artifactId>log4j</artifactId>

           <version>1.2.17</version>

       </dependency>

      

       <!-- jstl -->

       <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>jstl</artifactId>

           <version>1.2</version>

       </dependency>

      

       <!-- servlet-api:可以用jsp,HttpServlet -->

       <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>javax.servlet-api</artifactId>

           <version>3.1.0</version>

           <scope>provided</scope>

       </dependency>

      

       <!-- fileupload:文件上传下载 -->

       <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->

       <dependency>

           <groupId>commons-fileupload</groupId>

           <artifactId>commons-fileupload</artifactId>

           <version>1.3.1</version>

       </dependency>

      

       <!-- jackson:ajax -->

       <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->

       <dependency>

           <groupId>com.fasterxml.jackson.core</groupId>

           <artifactId>jackson-databind</artifactId>

           <version>2.9.8</version>

       </dependency>

      

       <!-- hiberbate validator:后台数据校验 -->

       <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->

       <dependency>

           <groupId>org.hibernate</groupId>

           <artifactId>hibernate-validator</artifactId>

           <version>5.1.3.Final</version>

       </dependency>

      

       <!-- commons-codec:MD5加密类 -->

       <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->

       <dependency>

           <groupId>commons-codec</groupId>

           <artifactId>commons-codec</artifactId>

           <version>1.10</version>

       </dependency>

      

       <!-- 短信验证阿里云jar包 -->

       <dependency>

           <groupId>com.aliyun</groupId>

           <artifactId>aliyun-java-sdk-core</artifactId>

           <version>3.3.1</version>

       </dependency>

       <dependency>

           <groupId>com.aliyun</groupId>

           <artifactId>aliyun-java-sdk-dysmsapi</artifactId>

           <version>1.0.0</version>

       </dependency>

 

 

  </dependencies>

 

</project>

 

3.在/src/main/resources下分别创建spring和mybatis合在一起的配置文件applicationContext.xml(可以把spring,mybatis,事务管理的配置文件分开)、springMVC的配置文件springMVC-servlet.xml和数据源属性文件和log4j属性文件。

a)      springMVC配置文件:springMVC-servlet.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:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

              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.3.xsd">

      

       <!-- springMVC包扫描controller:若spring和springMVC扫描同一个包可能会导致事务失效 -->

       <context:component-scan base-package="com.zhiyou100.kfs.controller"/>

      

       <!-- 开启注解驱动:ajax返回java字符串是中文时需要配置编码 -->

       <mvc:annotation-driven>

              <mvc:message-converters>

                     <bean class="org.springframework.http.converter.StringHttpMessageConverter">

                            <constructor-arg index="0" value="utf-8"></constructor-arg>

                     </bean>

              </mvc:message-converters>

       </mvc:annotation-driven>

      

       <!-- 静态资源释放:可能要在web.xml中设置一下 -->

       <mvc:default-servlet-handler/>

      

       <!-- 配置文件上传解析器 -->

       <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

              <!-- 设置最大文件字节 -->

              <property name="maxInMemorySize" value="200000"></property>

       </bean>

      

       <!-- 配置拦截器 -->

       <mvc:interceptors>

              <mvc:interceptor>

                     <mvc:mapping path="/user/**"/><!-- 拦截的请求路径,**:表示user下的请求地址以及子目录 -->

                     <mvc:exclude-mapping path="/user/login"/><!-- 不拦截的请求路径 -->

                     <bean class="com.zhiyou100.kfs.interceptor.LoginInterceptor"></bean>

              </mvc:interceptor>

       </mvc:interceptors>

      

       <!-- 视图解析器 -->

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="prefix" value="/"/>

              <property name="suffix" value=".jsp"/>

       </bean>

 

</beans>

 

b)      数据源属性文件:db.properties

#数据源信息,user会被认成win系统的管理员名称不要用

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/test

jdbc.username=root

jdbc.password=123

 

c)      log4j日志文件:log4j.properties

#log4j:日志属性文件

log4j.properties,

log4j.rootLogger=DEBUG, 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

log4j.logger.org.apache=INFO

log4j.logger.java.sql.Connection=DEBUG

log4j.logger.java.sql.Statement=DEBUG

log4j.logger.java.sql.PreparedStatement=DEBUG

 

d)      spring配置文件: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"

       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.3.xsd">

      

       <!-- spring包扫描所有bean -->

       <context:component-scan base-package="com.zhiyou100.kfs"/>

      

       <!-- 引入属性文件 -->

       <context:property-placeholder location="classpath:*.properties"/>

      

       <!-- 配置数据源:c3p0 -->

       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

              <property name="driverClass" value="${jdbc.driver}"/>

              <property name="jdbcUrl" value="${jdbc.url}"/>

              <property name="user" value="${jdbc.username}"/>

              <property name="password" value="${jdbc.password}"/>

       </bean>

 

</beans>

 

e)      spring事务配置文件:applicationContext-transaction.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: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.xsd

              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

      

       <!-- 定义事务管理类:DataSourceTransactionManager -->

       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

              <property name="dataSource" ref="dataSource"/>

       </bean>

      

       <!-- 定义属性 -->

       <tx:advice transaction-manager="transactionManager" id="advice">

              <tx:attributes>

                     <tx:method name="select*" read-only="true"/>

                     <tx:method name="*"/>

              </tx:attributes>

       </tx:advice>

      

       <!-- 配置AOP切面 -->

       <aop:config>

              <!-- 配置切点 -->

              <aop:pointcut expression="execution(* com.zhiyou100.kfs.service.*.*(..))" id="pointcut"/>

              <!--  -->

              <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>

       </aop:config>

 

</beans>

 

f)       spring整合mybatis配置文件:applicationContext-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:context="http://www.springframework.org/schema/context"

       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.3.xsd">

      

       <!-- 配置SqlSessionFactory -->

       <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

              <!-- 数据源 -->

              <property name="dataSource" ref="dataSource"/>

              <!-- mapper映射文件 -->

              <property name="mapperLocations" value="classpath:com/zhiyou100/kfs/mapper/*.xml"/>

              <!-- pagehelp拦截器 -->

              <property name="plugins">

                  <array>

                    <bean class="com.github.pagehelper.PageInterceptor">

                      <property name="properties">

                        <!--使用下面的方式配置参数,一行配置一个 -->

                        <value>

                          reasonable=true

                        </value>

                      </property>

                    </bean>

                  </array>

              </property>

       </bean>

      

       <!-- 接口生成实现类:用MapperScannerConfigurer使dao接口的包和SqlSessionFactory对象中的mapper对应 -->

       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

              <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>

              <property name="basePackage" value="com.zhiyou100.kfs.dao"/>

       </bean>

 

</beans>

 

4.在src/main/java下建立相应的bean,dao,service,controller,mapper。

5.可以继续写ssm程序

posted on 2019-09-18 22:06  kfsrex  阅读(199)  评论(0编辑  收藏  举报