1.打開idea,新建項目,選擇maven骨架,而後填寫包名和項目名稱,一直下一步。css
接着下一步,配置項目的maven環境前端
建立完成以後,以下圖:(上邊的項目名字只是例子,具體的以本身搭建的項目名爲準)java
能夠看新建完成的項目src/main/webapp的文件夾,還少兩個root文件夾,分別是:src/main/java 和 src/main/resources 文件夾。接下來建立:mysql
建立後以下,這只是一個簡單的文件夾,而一般這兩個文件夾都是用來放咱們的java文件和資源文件的,因此必須將他們呢標記爲root,linux
標記完成以下:web
其中java中創建包文件放咱們項目中的java文件也就是一些類,resources中放的是一些資源文件如各類xml等,這兩個文件編譯後統一都會編譯到項目的根目錄。spring
項目創建完成以後,就開始咱們的後續工做了,因爲這是搭建一個簡單的SSM項目,我們就先把須要的依賴給添加一下。sql
在pom.xml添加依賴,這樣maven就會去中央倉庫下載jar包,提供給項目使用。數據庫
簡單的ssm框架包括的依賴無非就是這幾種,spring家族的依賴,mybatis的依賴,mybatis-spring關聯的依賴,數據庫鏈接以及鏈接池依賴,java的servlet依賴等。express
下面粘貼相出pom中所需的依賴(spring相關依賴的版本須要保持一直,否側會有衝突)
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>4.3.18.RELEASE</spring.version> <mybatis.version>3.4.1</mybatis.version> </properties> <dependencies> <!--Spring系列--> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!--MyBatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!--Mybatis-spring-connection--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--java 基本依賴--> <!-- javaee-api --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- druid數據庫鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies>
接下里在java目錄下建立package,resources下建立xml
conrtroler控制器創建控制層代碼,主要用於接受前端的訪問和返回數據,
service:controller層接受到請求進行業務處理的一層,直接到serviceimpl中進行業務邏輯的編寫
dao: mybatis的dao文件生成的地方
util:一些工具類相關
spring中存放的是與spring相關的配置文件,如spring-context,spring-mvc.spring-datasources等
如今先開始編寫配置文件:
直接粘貼處各個配置文件:
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" 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"> <context:component-scan base-package="com.webforlinux.cn" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:property-placeholder location="classpath:*.properties" ignore-unresolvable="true"/> <context:property-placeholder location="classpath*:*/*.properties" ignore-unresolvable="true"/> <import resource="spring-datesource.xml"/> <import resource="spring-mvc.xml"/> </beans>
spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/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"> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <!--靜態資源放行不攔截--> <!--<mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/fonts/**" location="/font/"/> <mvc:resources mapping="/css/**" location="/css/"/>--> <!--視圖解析--> <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp" /> </bean>--> </beans>
spring-datasource.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/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.xsd"> <!--durid鏈接池--> <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="clone"> <property name="driverClassName" value="${jdbc.driverCLassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="${jdbc.initialSize}"/> <property name="minIdle" value="${jdbc.minIdle}"/> <property name="maxActive" value="${jdbc.maxActive}"/> <!--鏈接超時等待時間--> <property name="maxWait" value="${maxWait}"/> <!--檢測須要關閉的空閒鏈接--> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/> <!--一個鏈接在池中最小的生存空間--> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/> <property name="testWhileIdle" value="true"/> </bean> <!--配置SqlSessionFactory對象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dateSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!--掃描dao包--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.*.*.*"/> -----根據本身dao所在的包名編寫 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--事務--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dateSource"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
其中spring-datasource.xml配置文件中須要引入數據庫鏈接的配置文件,還須要在resources目錄下建立jdbc.properties
jdbc.properties
jdbc.driverCLassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true jdbc.username=root jdbc.password=123456 jdbc.initialSize=1 jdbc.minIdle=1 jdbc.maxActive=10 maxWait=10000 jdbc.timeBetweenEvictionRunsMillis=60000 jdbc.minEvictableIdleTimeMillis=300000
reosurce目錄下建立Mybatsi自動生成文件
generatorConfig.xml
以上紅框內根據本身的配置本身修改,分別是數據庫連接信息,和 生成mapper,dao,以及xml的地址,自行修改,能夠用本身建好的包,沒有新建的話,會自動生成。
classPathEntry 標籤內的是須要一個連接的jar包的,去官網下載而後放到一個目錄,而後指向它。
使用其以前先創建好數據庫以及數據庫表:
<?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> <classPathEntry location="C:\work\enviroment\resources/mysql-connector-java-5.1.47.jar"/> <context id="DB2Tables1" targetRuntime="MyBatis3"> <!-- 配置pojo的序列化 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/shop" userId="root" password="0000"/> <javaTypeResolver type=""> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.eva.cn.pojo" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources"> <property name="enableSuzbPackages" value="true"/> </sqlMapGenerator> <!-- ANNOTATEDMAPPER XMLMAPPER --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.eva.cn.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="user" domainObjectName="User" enableSelectByExample="false" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table> </context> </generatorConfiguration>
創建完數據庫和數據庫表以後,要確認和jdbc.properties的信息一致,以及和generatorConfig.xml的信息一致,而後開始使用自動生成文件。(小項目也能夠本身手寫,可是這確實是一個這個能夠幫咱們作不少事)
首先在pom中添加該插件的依賴:
<build> <finalName>webforlinux</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> </plugin> </plugins> </build>
此時idea的右側邊欄目中的maven菜單裏賣打開插件會發現已經有了
接下里,雙擊插件,出現以下
如上,關於mybatis的相關文件上傳文件生成成功(包括 實體類+dao + dao.mapper):
作到這,咱們能夠配置一下web.xml,而後先跑一下項目,看是否能夠跑起來了,以確保添加配置文件都沒問題。
跑的沒問題了再開始搭建一個很簡單的頁面看一下SSM的執行流程:
想run任何一個java項目都得有應用服務器,應用服務器會讀取項目的web.xml,來啓動項目作一些初始化的配置,如今粘貼出web.xml的配置:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--加載spring--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/applicationContext.xml</param-value> </context-param> <context-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--springMVC--> <servlet> <servlet-name>SpringDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
接下來配置tomcat:
添加tomcat成功後,點擊run或者 debug便可
如上,系統啓動成功::
至此,SSM框架搭建完成,接下里本身就能夠在controller層寫控制層代碼等,有點累,就先寫到這吧。下次有時間在分享。謝謝你們的查看,鄙人拙見,有什麼問題,還望指出。