1. 程式人生 > >【java】intellij idea搭建SSM框架摘錄

【java】intellij idea搭建SSM框架摘錄

搭什麼搭!直接用腳手架不行嗎?!

下載就能用!!下載就能用!!下載就能用!!

咳咳,開個玩笑,如果本著學習態度的話,那就慢慢啃吧~

搭建空的Maven專案

使用Intellij idea 搭建基本的maven空專案 new->project

image

填寫GroupId和ArtifactId,next,選擇專案的地址,next->new window open 在新的視窗開啟

最後Finish,Maven專案搭建完成,目錄如下:

接下來我們構建專案基本骨架,:
(1)在java下新建com
(2)在com下新建springmvc
(3)在springmvc下新建controller、dao、mapping、pojo、service
(4)在service下新建impl
如下圖:
i

這裡解釋一個mybatis的工具,叫做mybatis-generator-core-1.3.2,它能夠根據資料庫中的表生成dao、mapping、pojo三個型別檔案(後面會在mybatis部分詳細介紹)

目錄解釋如下:
controller:http(GET和POST等)請求的入口,呼叫Service,想客戶端返回資料

dao:介面,有mybatis自動化工具生成的檔案之一,定義了資料表對映成的物件,具有的操作

mapping:具體是sql語句,以xml格式存在,即mybatis的語法檔案

pojo:實體類

service:controller呼叫的服務,service呼叫dao裡面定義的資料庫行為方法,來執行對應mapping裡的mybatis,從而完成與資料庫完整的互動。

service-impl:介面,定義了服務應該具有的函式方法(此處可以理解是提現軟體的設計思想,你要是樂意,直接寫service也可以)

至此:

修改Pom檔案

第一步:配置你的maven
image

setting.xml映象配置:(具體可以百度一下其他文章)

     <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url
>
<mirrorOf>central</mirrorOf> </mirror>

第二步:在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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>hugo</groupId>
  <artifactId>example</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>hugo Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- spring版本號 -->
    <spring.version>4.1.4.RELEASE</spring.version>
    <!-- mybatis版本號 -->
    <mybatis.version>3.2.8</mybatis.version>
    <!-- log4j日誌檔案管理包版本 -->
    <slf4j.version>1.7.7</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <!-- jackson包版本 -->
    <jackson.version>2.5.0</jackson.version>
  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- mybatis核心包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <!-- mybatis/spring包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!--spring單元測試依賴 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>

    <!-- springMVC核心包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- spring核心包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</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>

    <!-- 匯入Mysql資料庫連結jar包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.30</version>
    </dependency>

    <!-- 匯入dbcp的jar包,用來在applicationContext.xml中配置資料庫 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- JSTL標籤類 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- 日誌檔案管理包 -->
    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>


    <!-- 格式化物件,方便輸出日誌 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.1.41</version>
    </dependency>


    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
    <!-- 映入JSON -->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.1.0</version>
    </dependency>

    <!-- 上傳元件包 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.9</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <!-- 表示開發的時候引入,釋出的時候不會載入此包 -->
      <scope>test</scope>
    </dependency>

    <!--Spring test相關依賴-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.8.1</version>
    </dependency>

    <dependency>
      <groupId>net.sourceforge.htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>2.15</version>
    </dependency>


    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.33.0</version>
    </dependency>

    <dependency>
      <groupId>us.codecraft</groupId>
      <artifactId>webmagic-core</artifactId>
      <version>0.6.1</version>
    </dependency>
    <dependency>
      <groupId>us.codecraft</groupId>
      <artifactId>webmagic-extension</artifactId>
      <version>0.6.1</version>
    </dependency>


  </dependencies>
  <build>
    <finalName>hugo</finalName>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
      <filtering>true</filtering>
    </resource>
  </resources>
  </build>

</project>

版本可自行更換,有興趣的同學可以看看具體配置。maven的更新方式可以選擇enable-auto-update(大致意思是這個,當pom檔案改動時,自動更新)

編寫SSM配置檔案

第一部分:與資料庫相關的jdbc.properties

在resources下新建個配置檔案jdbc.properties,內容如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
#定義初始連線數
initialSize=0
#定義最大連線數
maxActive=20
#定義最大空閒
maxIdle=20
#定義最小空閒
minIdle=1
#定義最長等待時間
maxWait=60000

請確保你的mysql服務是開啟的,且存在資料庫project

第二部分:spring-mvc
在resources下新建個配置檔案spring-mvc.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:p="http://www.springframework.org/schema/p"
    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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
    <context:component-scan base-package="com.springmvc.controller" />

    <!--避免IE執行AJAX時,返回JSON出現下載檔案 -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 啟動SpringMVC的註解功能,完成請求和註解POJO的對映 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />   <!-- JSON轉換器 -->
            </list>
        </property>
    </bean>
    <!-- 定義跳轉的檔案的前後綴 ,檢視模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 這裡的配置我的理解是自動給後面action的方法return的字串加上字首和字尾,變成一個 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/page/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 配置檔案上傳,如果沒有使用檔案上傳可以不用配置,當然如果不配,那麼配置檔案中也不必引入上傳元件包 -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 預設編碼 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 檔案大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 記憶體中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean> 

</beans>

請務必仔細看裡面的配置內容,尤其涉及到路徑的地方

第三部分:mybatis-config.xml和spring-mybatis.xml

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="logImpl" value="LOG4J" />
        <setting name="cacheEnabled" value="true"/>
    </settings>
</configuration>

此配置的作用是可以讓mybatis在執行的時候列印日誌,mybatis還有其他很多配置選項,有需求的小夥伴再仔細檢視更詳細的博文。關於mybatis的配置。

spring-mybatis配置:

<?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:p="http://www.springframework.org/schema/p"
    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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自動掃描 -->
    <context:component-scan base-package="com.springmvc" />
    <!-- 引入配置檔案 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化連線大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 連線池最大數量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 連線池最大空閒 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 連線池最小空閒 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 獲取連線最大等待時間 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置對映檔案 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
        <!-- 自動掃描mapping.xml檔案 -->
        <property name="mapperLocations" value="classpath:com/springmvc/mapping/*.xml"></property>
    </bean>

    <!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.springmvc.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

請務必仔細看裡面的配置內容,尤其涉及到路徑的地方很多地方出現錯誤,都是因為spring或mybatis自動掃描的路徑錯誤,導致mapping無法被識別。

第四部分:log4j.xml和log4j.properties
log4j.xml
日誌模組的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://toolkit.alibaba-inc.com/dtd/log4j/log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://jakarta.apache.org/log4j/ ">
    <!-- ===================================================================== -->
    <!--  定義全域性輸出介面卡                                                      -->
    <!-- ===================================================================== -->
    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}]-[%c]-[%t] %x - %m%n"/>
        </layout>
    </appender>

    <!-- Mybatis and SQL -->
    <logger name="org.apache.ibatis" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="org.mybatis.spring" additivity="true">
        <level value="ERROR"/>
        <appender-ref ref="consoleAppender"/>
    </logger>
    <logger name="java.sql" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="java.sql.Connection" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="java.sql.Statement" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="java.sql.PreparedStatement" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <!-- Spring -->
    <logger name="org.springframework" additivity="true">
        <level value="ERROR"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <!-- ActiveMQ -->
    <logger name="org.apache.activemq" additivity="true">
        <level value="ERROR"/>
        <appender-ref ref="consoleAppender"/>
    </logger>
    <!-- ===================================================================== -->
    <!--  根日誌記錄器                                                          -->
    <!-- ===================================================================== -->
    <root>
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </root>
</log4j:configuration>

log4j.properties

#定義LOG輸出級別
log4j.rootLogger=INFO,Console,File
#定義日誌輸出目的地為控制檯
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定日誌輸出格式,下面一行是指定具體的格式