SSM
Spring+SpringMVC+MyBatis 三个框架的基础项目整合环境搭建
使用Maven工程进行依赖管理
搭建环境: IDEA 2021.2.3 (Ultimate Edition), Windows10 64位系统
IntelliJ IDEA 2021.2.3 (Ultimate Edition) Build #IU-212.5457.46, built on October 12, 2021 Runtime version: 11.0.12+7-b1504.40 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 16
具体流程 操作分为五个步骤:
创建一个maven工程
引入项目依赖的jar包
Spring
SpringMVC
MyBatis
mybatis
mybatis-spring
mybatis-generator-core
c3p0 – 数据库连接池
jstl
servlet-api (provided)
junit4 测试单元 (实际使用spring-test)
mysql驱动
下载并引入bootstrap前端框架
创建整合SSM相关配置文件
测试
创建maven工程 …
引入依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 5.3.8</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > 4.3.20.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > 5.3.8</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.4.5</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > 2.0.6</version > </dependency > <dependency > <groupId > com.mchange</groupId > <artifactId > c3p0</artifactId > <version > 0.9.5.5</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.25</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > javax.servlet-api</artifactId > <version > 3.0.1</version > <scope > provided</scope > </dependency > <dependency > <groupId > javax.servlet.jsp</groupId > <artifactId > jsp-api</artifactId > <version > 2.2</version > <scope > provided</scope > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.13.2</version > </dependency > <dependency > <groupId > org.mybatis.generator</groupId > <artifactId > mybatis-generator-core</artifactId > <version > 1.4.0</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-test</artifactId > <version > 5.3.8</version > </dependency > <dependency > <groupId > com.github.pagehelper</groupId > <artifactId > pagehelper</artifactId > <version > 5.2.1</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.12.3</version > </dependency > <dependency > <groupId > org.hibernate.validator</groupId > <artifactId > hibernate-validator</artifactId > <version > 7.0.1.Final</version > </dependency > </dependencies >
…
bootstrap前端框架 …
整合SSM 创建webapp文件夹,
—>创建web.xml
文件在WEB-INF下
配置web.xml
配置springMVC.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <context:component-scan base-package ="com.anonymous.woj" > <context:include-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="suffix" value =".jsp" /> <property name ="prefix" value ="/WEB-INF/pages/" /> </bean > <mvc:annotation-driven /> <mvc:default-servlet-handler />
创建mybatis-config.xml配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <configuration > <settings > <setting name ="mapUnderscoreToCamelCase" value ="true" /> </settings > <typeAliases > <package name ="com.anonymous.woj.bean" /> </typeAliases > <plugins > <plugin interceptor ="com.github.pagehelper.PageInterceptor" > <property name ="reasonable" value ="true" /> </plugin > </plugins > </configuration > <configuration > <settings > <setting name ="mapUnderscoreToCamelCase" value ="true" /> </settings > <typeAliases > <package name ="com.anonymous.woj.bean" /> </typeAliases > <plugins > <plugin interceptor ="com.github.pagehelper.PageInterceptor" > <property name ="reasonable" value ="true" /> </plugin > </plugins > </configuration >
配置applicationContext.xml (Spring配置文件) 1 2 3 4 5 <!-- spring控制除了跳转逻辑的其他组件--> <context:component-scan base-package="com.atguigu.crud"> <!-- 不扫描控制器--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
1 <context:property-placeholder location="classpath:dbconfig.properties"/>
1 2 3 4 5 6 7 <!-- 配置数据源, 事务控制, ...--> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
1 2 3 4 5 6 7 <!-- mybatis 整合--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="dataSource" ref="pooledDataSource"></property> <!-- 指定mapper文件--> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean>
1 2 3 4 5 6 7 8 9 <!-- 配置扫描器, 讲mybatis接口实现加入到ioc容器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.atguigu.crud"/> </bean> <!-- 配置可以批量的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> <constructor-arg name="executorType" value="BATCH"/> </bean>
1 2 3 4 5 <!-- 事务控制--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="pooledDataSource"/> </bean>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!-- 开启基于注解事务, 或者xml的配置事务--> <aop:config> <!-- 切入点表达式--> <aop:pointcut id="txPointCut" expression="execution(* com.atguigu.crud.service..*(..))"/> <!-- 配置事务增强--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> <!--配置事务增强, 事务如何切入--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 代表切入的所有方法都是事务方法--> <tx:method name="*"/> <!-- 代表以get开始的所有方法 只读--> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice>
…
测试 …
end…