初学JAVA,针对入门集成SSM框的步骤
1.选择骨架 webApp,创建一个maven工程,next
2.选择好储存的目录,parent选择None,next
3.点击这个“+”号,添加name=archetypeCatalog和value=internal的键值可使创建maven更快,添加完毕点击finish完成创建
4.创建完成后,main目录下默认是没有java和resources的,需要我们手动在main目录下new一个java和resources,创建完成后,右键相对应的包,选择Mark Directory as --将java和resources包转换成多目录结构
5.编写相关的目录包,domain,dao,service,controller,serviceimpl,test 以及相关的代码。(目录结构可按实际需要设置)
6.编写导入pom.xml相关信息(根据实际需要引入相关信息,这里不完全截出)
7.配置Spring容器,在resources目录下创建applicationContext.xml文件,将以下约束覆盖创建xml时原有的不完整的约束
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd”>
8.在applicationContext.xml下执行第一步操作
8.1
开启注解的扫描,只处理service和dao包下的文件,controller不需要Spring框架去处理
<context:component-scan base-package=“包的全路径”>
<context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
exclude-filter:表示除去某一个包不扫描
include-filter:表示只扫描某一个包
这里我们配置spring选择exclude-filte,不去处理controller下的文件。
8.2
在resources目录下我们添加log4j.properties的配置文件。
ps:此时我们便可以在service的实现类上加上Service(“名称”)的注解.
9.在web.xml文件下我们需要去配置三个器来读取我们自己配置的xml文件,
9.1 配置前端控制器
9.2 配置解决中文乱码的过滤器
9.3 配置spring的监听器
10.我们在第九步时配置前端控制器提到了一个springmvc.xml,那么我们就需要在resources目录下创建一个springmvc.xml,创建完毕后,复制以下内容覆盖原有约束
xmlns=“http://www.springframework.org/schema/beans”
xmlns:mvc=“http://www.springframework.org/schema/mvc” xmlns:context=“http://www.springframework.org/schema/context”
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
10.1
开启注解扫描,只扫描Controller控制器的内容
<context:component-scan base-package=“包的全路径”>
<context:include-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
exclude-filter:表示除去某一个包不扫描
include-filter:表示只扫描某一个包
10.2
配置视图解析器
10.3
过滤静态资源
<mvc:resources location="/css/" mapping="/css/" />
<mvc:resources location="/images/" mapping="/images/" />
<mvc:resources location="/js/" mapping="/js/**" />
ps: 目录文件夹自行确认
10.4
开启SpringMVC注解的支持
< mvc:annotation-driven/>
ps:这个可别忘了!!!!!!很重要
配置完成我们便可以在控制器Controller类加上
@Controller
@RequestMapping的注解.
11.集成mybatis,以往我们需要去编写SqlMapConfig文件,但是为了很好的管理,
我们可以在applicationContext.xml文件下,让Spring去整合mybatis.
11.1
首先我们需要配置连接池
< bean id=“dataSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>
< property name=“driverClass” value=“com.mysql.cj.jdbc.Driver”/>
< property name=“jdbcUrl” value=“jdbc:mysql:///ssm?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false”/>
< property name=“user” value=“root”/>
< property name=“password” value=“数据库密码”/>
< /bean>
ps:这里有两个配置不一样的地方在:
driverClass:在mysql 8之后路径为 com.mysql.cj.jdbc.Driver ,8以下为com.mysql.jdbc.Driver
jdbcUrl: 8之后
“jdbc:mysql:///ssm?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false”
8之前
“jdbc:mysql:///ssm?useUnicode=true&characterEncoding=utf-8”
(这里的&,在xml文件下为 “&;”)
11.2
配置SqlSessionFactory工厂
< bean id=“sqlSessionFactory” class=“org.mybatis.spring.SqlSessionFactoryBean”>
< property name=“dataSource” ref=“dataSource” />
< /bean>
11.3
配置dao目录接口所在包的位置
< bean id=“mapperScannerConfigurer” class=“org.mybatis.spring.mapper.MapperScannerConfigurer”>
< property name=“basePackage” value=“cn.test.dao”/>
< /bean>
12.配置Spring框架声明式事务管理
12.1
配置事务管理器
< bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
< property name=“dataSource” ref=“dataSource” />
< /bean>
12.2
配置事务通知
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
tx:attributes
<tx:method name="find" read-only=“true”/>
<tx:method name="" isolation=“DEFAULT”/>
</tx:attributes>
</tx:advice>
ps:这里的name值 根据自己设定配置
12.3
配置AOP增强
< aop:config>
<aop:advisor advice-ref=“txAdvice” pointcut="execution( cn.test.service.impl.ServiceImpl.(…))"/>
</aop:config>*