SSM整合配置
阿新 • • 發佈:2020-09-02
一、匯入相關依賴
<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>5.0.2.RELEASE</spring.version><!-- <mysql.version>5.1.6</mysql.version> --> <log4j.version>1.6.6</log4j.version> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.6</version> </dependency> <!-- 事務 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- jdbc模板 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- mvc json--> <!-- json --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> <!-- c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</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>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- oracle --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.4.0-atlassian-hosted</version> </dependency> <!-- mysql <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> --> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- 整合mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.3</version> </dependency> <!-- log4j start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${log4j.version}</version> </dependency> <!-- log4j end --> <!-- 單元測試 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>compile</scope> </dependency> </dependencies>
二、建立三層架構目錄
在main目錄下建立java和resources目錄
三層架構存放在java目錄下
-
Controller
-
Service
-
Mapper
resources目錄存放配置及xml
-
application.xml
-
jdbcConfig.properties
-
log4j.properties
-
springmvc.xml
mapper的xml(編寫sql使用)
2.1. 編寫web.xml
在webapp目錄(沒有自行建立)下建立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的監聽器 預設只加載WEB-INF下的applicationContext.xml檔案 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 設定配置檔案的路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 載入配置檔案 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 伺服器啟動時建立 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 設定字串編碼 --> <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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
三、編寫配置類
-
application.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 開啟註解掃描 只掃描spring和mybatis --> <context:component-scan base-package="com.hopu" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:property-placeholder location="classpath:jdbcConfig.properties"></context:property-placeholder> <!-- spring 整合 mybatis--> <!-- 1.配置連線池 C3P0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 2.配置SqlSessionFactory工廠 --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入資料來源 --> <property name="dataSource" ref="dataSource" /> <!-- 實體類起別名 --> <property name="typeAliasesPackage" value="com.hopu.domain"></property> <!-- 指定mapper介面所對應的xml--> <property name="mapperLocations" value="classpath:com/hopu/mapper"></property> </bean> <!-- 3.配置介面AccountDao所在的包 對映掃描配置類 mvc配置 --> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 介面所在的包 --> <property name="basePackage" value="com.hopu.mapper" /> <!-- 掃描工廠並注入sqlSessionFactoryBeanName --> <property name="sqlSessionFactoryBeanName" value="sessionFactory" /> </bean> <!-- 配置spring宣告式事務管理 --> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 傳入連線池物件 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事務通知 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="ins*" /> <tx:method name="up*" /> <tx:method name="del*" /> <!-- 只讀的事務 --> <tx:method name="find*" read-only="true"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置AOP增強 --> <aop:config> <aop:pointcut id="mypoint" expression="execution(* com.hopu.service.impl.*.*(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="mypoint"></aop:advisor> </aop:config> </beans>
3.1. 編寫jdbc.properties
1、oracle資料庫
jdbc.driver = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:XE/oracle
jdbc.username = wk123
jdbc.password = 123456
2、Mysql資料庫
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 jdbc.username = root jdbc.password = 123
四、編寫mvc配置
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 開始註解掃描,只掃描COntroller --> <context:component-scan base-package="com.hopu"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置檢視解析器 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 不攔截的靜態資源 --> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/image/**" location="/image/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/pages/**" location="/pages/" /> <mvc:resources mapping="/jsp/**" location="/jsp/" /> <mvc:resources mapping="/layui/**" location="/layui/" /> <!-- 支援mvc註解 --> <mvc:annotation-driven /> </beans>
五、編寫log4j
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory= CONSOLE , Debug, LOGFILE, info warn error fatal
log4j.rootCategory=INFO,CONSOLE,
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# RollingFileAppender 代表檔案 ConsoleAppender 代表控制檯
#日誌輸出到控制檯
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n 輸出內容 (%F:%L) 具體哪一行
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm:ss}] [%15.15t] ===> %m (%F:%L) \n
#日誌輸出到檔案
#log4j.appender.LOGFILE=org.apache.log4j.FileAppender
#log4j.appender.LOGFILE.Encoding=UTF-8
#log4j.appender.LOGFILE.File=E:/A/log4j.log
#log4j.appender.LOGFILE.Append=true
#log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.LOGFILE.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm:ss}] [%15.15t] ===> %m
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- mapper類名 --> <mapper namespace="com.hopu.mapper.IAccount"> <!-- 配置引用 減少重複程式碼 id=方法名 --> <sql id="findall"> select * from account </sql> <!-- 查詢所有 resultType=返回的引數型別 --> <select id="findAll" resultType="account" > <include refid="findall"></include> </select> <!-- 新增賬戶 parameterType=接受的類名 --> <insert id="SaveAccount" parameterType="account" > insert into account(name,money) values (#{name},#{money}) </insert> </mapper>