javaEE之ssm框架(有完整的專案可以下載,包括資料庫)
阿新 • • 發佈:2019-01-04
SSM框架整合操作(有完整專案可以下載)
1.建立web專案:我用的是Eclipse:
2.jar包匯入:
3.目錄結構:
4.配置檔案:
spring框架配置檔案
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 讀取db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置資料來源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <!-- 資料庫驅動 --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 連線資料庫的url --> <property name="url" value="${jdbc.url}" /> <!-- 連線資料庫的使用者名稱 --> <property name="username" value="${jdbc.username}" /> <!-- 連線資料庫的密碼 --> <property name="password" value="${jdbc.password}" /> <!-- 最大連線數 --> <property name="maxTotal" value="${jdbc.maxTotal}" /> <!-- 最大空閒連線 --> <property name="maxIdle" value="${jdbc.maxIdle}" /> <!-- 初始化連線數 --> <property name="initialSize" value="${jdbc.initialSize}"></property> </bean> <!-- 事務管理器,依賴於資料庫 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 開啟事務註解 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 配置MyBatis工廠SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入資料來源 --> <property name="dataSource" ref="dataSource" /> <!-- 指定MyBatis和核心配置檔案位置 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!-- 配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="ssm.dao" /> </bean> <!-- 掃描Service 對包下的所有bean進行掃描,將其註冊進spring容器--> <context:component-scan base-package="ssm.service" /> </beans>
資料庫連線:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/member
jdbc.username=root
jdbc.password=123
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
mybatis配置檔案:
<?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> <!-- 這裡寫配置內容 --> <!-- 別名定義 --> <typeAliases> <package name="ssm.po"/> </typeAliases> </configuration>
springmvc配置檔案:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 配置包掃描器,掃描@Controller註解的類 --> <context:component-scan base-package="ssm.controller" /> <!-- 載入註解驅動:不是必須的,預設會載入一個過時的驅動 --> <mvc:annotation-driven /> <!-- 配置檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<!-- 配置載入Spring檔案的監聽器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 編碼過濾器 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置springmvc前端核心控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 配置伺服器啟動後立即載入Spring MVC配置檔案 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /:攔截所有請求(無後綴名的),除了.jsp,可以攔截.html -->
<!-- /*:攔截所有,包括帶有後綴名的 -->
<!-- 攔截到的走controller -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5.前端檔案:
web.xml攔截(應該是:攔截到之後走springmvc規定的路徑,也就是通過controller轉發)所有路徑,除了.jsp(就是web.xml配置的/)等頁面。
在web-inf下面的直接訪問不到,只能通過轉發等才能訪問到。
6.執行路徑:(大概的路徑,可能不太準確,助理解)
po->dao->service->controller->jsp頁面
7.給前端頁面賦初值,用jstl
匯入ar包:
引用:
使用: