SSM整合步驟
SSM- CRUD
SSM : SpringMVC+Spring+Mybatis
Create (新建) +Retrieve (查詢) +Update(更新)+Delete(刪除)
功能點
1:分頁
2:數據校驗:JQuery前端校驗+JSR後端校驗。
3:ajax
4:Rest風格URL:使用HTTP協議請求方式的動詞,來表示對對資源的操作:GET(查詢)、POST(新增)、PUT(修改)、DELETE(刪除)。
技術點
1:基礎框架-SSM (SpringMVC+Spring+MyBatis)
2:數據庫-MySQL 5
3:前端框架-bootstrap快速搭建簡潔美觀的前端界面。
4:項目的依賴管理-Maven。
5:分頁插件-pagehelper
6:逆向工程-MyBatis Generator
基礎環境搭建
1:創建一個maven工程
2:引入項目依賴的jar包
- spring
- springMVC
- Mybatis
- 數據庫連接池,驅動包
- 其它包[ jstl , servlet-api , junit ]
maven 配置文件增加配置
阿裏雲下載鏡像
<!-- 中央倉庫阿裏雲鏡像下載-->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
版本
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<repositories>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</repositories>
</profile>
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>cn.yi</groupId> <artifactId>ssm-crud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- 引入項目依賴的jar包 --> <!-- SpringMVC Spring --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.12.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.12.RELEASE</version> </dependency> <!-- Spring aspect 面向切面編程 --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- Mybatis --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- Mybatis Spring 整合適配包 --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- 數據庫連接池c3p0 --> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1</version> </dependency> <!-- 數據庫連接驅動包 --> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> <!-- 其它包[ jstl , servlet-api , junit ] --> <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mortbay.jetty/servlet-api --> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>servlet-api</artifactId> <version>2.5-6.0.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- SpringTest Jar包 --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.12.RELEASE</version> <scope>test</scope> </dependency> <!-- 使用mybatis的逆向工程生成對應的bean及mapper --> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </project>
3: 引入bootstrap前端框架
4: 編寫SSM整合的關鍵配置文件
web.xml spring,springMVC mybatis 使用mybatis的逆向工程生成對應的bean以及mapper
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 1: 啟動Spring容器 --> <!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 2: 配置Springmvc的前端控制器,攔截所有請求 --> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 3: 字符編碼過濾器 ,一定要放在所有過濾器前面--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 4: 使用Rest風格URL ,將頁面普通的post請求轉換為指定的delete或者put請求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
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" 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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:component-scan base-package="cn.yi"> <!-- 只掃描Controller控制器 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- Spring的配置,這裏主要配置和設置業務邏輯 --> <!-- ==================數據源,事務控制 ……===================== --> <context:property-placeholder location="classpath:dbconfig.properties" /> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${db.url}" /> <property name="driverClass" value="${db.driver}" /> <property name="user" value="${db.user}" /> <property name="password" value="${db.password}" /> </bean> <!-- ===================配置和 Mybatis 的整合==================== --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis 全局配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-Config.xml"></property> <property name="dataSource" ref="pooledDataSource"></property> <!-- 指定mybatis mapper 文件的位置 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 配置掃描器,將mybatis接口的實現加入到IOC容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--掃描所有dao接口的實現,加入IOC容器中 --> <property name="basePackage" value="cn.yi.crud.dao"></property> </bean> <!-- 配置一個可以執行批量的sqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean> <!-- ======================事務控制的配置========================== --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 控制數據源 --> <property name="dataSource" ref="pooledDataSource"></property> </bean> <!-- 開啟基於註解的事務,也可以使用xml配置形式的事務(一般使用配置式) --> <aop:config> <!-- 切入點表達式 --> <aop:pointcut expression="execution(* cn.yi.crud.service..*(..))" id="txpoint" /> <!-- 配置事務增強 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txpoint" /> </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> <!-- spring配置文件的核心點(數據源、與mybatis整合、事務控制) --> </beans>
dispatcherServlet-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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Springmvc 的配置文件,包含網站跳轉邏輯的控制 --> <context:component-scan base-package="cn.yi" use-default-filters="false"> <!-- 只掃描Controller控制器 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 配置視圖解析器,方便頁面返回 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置前綴 --> <property name="prefix" value="/WEB-INF/views/"></property> <!-- 配置後綴 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 兩個標準配置 --> <!-- 將springmvc不能處理的請求要個tomcat處理 --> <mvc:default-servlet-handler/> <!-- 能支持springmvc 更高級功能,JSR303校驗,快捷ajax……--> <mvc:annotation-driven/> </beans>
mybatis-Config.xml
<?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> <settings> <!-- 駝峰面命規則 --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <typeAliases> <package name="cn.yi.crud.bean" /> </typeAliases> </configuration>
mbg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 不生成xml註釋 --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--配置數據庫連接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm-crud" userId="root" password=""> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--指定javaBean生成的位置 --> <javaModelGenerator targetPackage="cn.yi.crud.bean" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--指定SQL映射文件,生成的位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--指定dao接口生成的位置,mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.yi.crud.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--table指定每個表生成策略 --> <table tableName="emp" domainObjectName="Employee"></table> <table tableName="dept" domainObjectName="Department"></table> </context> </generatorConfiguration>
SSM整合步驟