第一階段: 1、用PowerDesign建數據模型,並導出SQL文件; 2、將SQL文件導入到MySQL客戶端,建立表格; MySQL數據遠程訪問:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'mypassword' WITH GRA ...
第一階段:
1、用PowerDesign建數據模型,並導出SQL文件;
2、將SQL文件導入到MySQL客戶端,建立表格;
MySQL數據遠程訪問:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
如果是固定IP:grant all privileges on *.* to 'root'@'192.168.41.100'identified by '123456' with grant option;
//推送設置到記憶體或重啟伺服器也行
mysql>FLUSH PRIVILEGES
3、使用MyBatis框架,搭建DAO層:----------------------------Mybatis----------------------
1)資料庫連接的信息:驅動類、連接地址、用戶名、密碼
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.41.100:3306/testtest" userId="root" password="123456"></jdbcConnection>
2)targetPackage="com.softjx.model"
3)把表名與類名對應
<table schema="testtest" tableName="school" domainObjectName="School"></table>
4)運行GeneratorSqlmap.java生成dao和model
第二階段:
1、在MyEclipse上新建一個Web項目,並導入ssm所需的jar包,放在WEB-INF/lib目錄下;
2、將第一階段通過Mybatis框架生成的model和dao複製到當前項目;----------------------------Dao層----------------------
3、搭建Service層:建com.softjx.service與com.softjx,service,impl包;----------------------------spring層----------------------
4、將需要相關的配置文件(mybatisconfig.xml,dbconfig.properties,applicationContext.xml,log4j.properties)放到src目錄下;
1)修改dbconfig.properties文件,ip,資料庫,用戶名,密碼
2)修改applicationContext.xml中的包名,目錄名
5、在com.softjx.service包中寫介面,在com.softjx.service.impl寫介面的實現。
註意:com.softjx.service.impl寫介面的實現,
@Service("studentService")
@Transactional
7.單元測試:
要註意mysql資料庫中主鍵要自增,這一步要我們去mysql中設置。
studentService = (StudentService) context.getBean("studentService");
這個"studentService"是從@Service("studentService")
第三階段:
1、修改WebRoot目錄下的web.xml如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <display-name></display-name> 8 9 10 11 <!-- 配置啟動 Spring IOC 容器的 Listener,啟動spring容器 --> 12 <context-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:applicationContext.xml</param-value> 15 </context-param> 16 17 <listener> 18 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 19 </listener> 20 21 22 <!-- 表單提交controller獲得中文參數後亂碼解決方案 註意: jsp頁面編碼設置為UTF-8 form表單提交方式為必須為post,get方式下麵spring編碼過濾器不起效果 --> 23 24 <filter> 25 <filter-name>characterEncodingFilter</filter-name> 26 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 27 <init-param> 28 <param-name>encoding</param-name> 29 <param-value>UTF-8</param-value> 30 </init-param> 31 <init-param> 32 <param-name>forceEncoding</param-name> 33 <param-value>true</param-value> 34 </init-param> 35 </filter> 36 <filter-mapping> 37 <filter-name>characterEncodingFilter</filter-name> 38 <url-pattern>/*</url-pattern> 39 </filter-mapping> 40 41 42 <!-- 可以把 POST 請求轉為 DELETE 或 PUT 請求 --> 43 44 <filter> 45 <filter-name>HiddenHttpMethodFilter</filter-name> 46 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 47 </filter> 48 49 <filter-mapping> 50 <filter-name>HiddenHttpMethodFilter</filter-name> 51 <url-pattern>/*</url-pattern> 52 </filter-mapping> 53 54 55 56 <!-- 配置 DispatcherServlet --> 57 <servlet> 58 <servlet-name>dispatcherServlet</servlet-name> 59 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 60 61 <init-param> 62 <param-name>contextConfigLocation</param-name> 63 <param-value>classpath:springmvc.xml</param-value> 64 </init-param> 65 66 <load-on-startup>1</load-on-startup> 67 68 </servlet> 69 70 <servlet-mapping> 71 <servlet-name>dispatcherServlet</servlet-name> 72 <url-pattern>/</url-pattern> 73 </servlet-mapping> 74 75 76 77 78 79 80 <welcome-file-list> 81 <welcome-file>index.jsp</welcome-file> 82 </welcome-file-list> 83 </web-app>
2、在src目錄下建立springmvc.xml文件,文件內容如下:----------------------------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: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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定掃描的包 --> <context:component-scan base-package="com.softjx" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置視圖解析器: 如何把 handler 方法返回值解析為實際的物理視圖 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 處理靜態資源 --> <mvc:default-servlet-handler/> <!-- 預設配置方案。 並提供了:數據綁定支持,@NumberFormatannotation支持, @DateTimeFormat支持,@Valid支持,讀寫XML的支持(JAXB), 讀寫JSON的支持(Jackson)。 後面,我們處理響應ajax請求時,就使用到了對json的支持。 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>
3、在src目錄下新建com.softjx.action包,在包里建Action類;
註意:1)@Controller
@RequestMapping("/student")
2)每個方法:
@RequestMapping("/studentAddInput")
4、在WEB-INF目錄下創建views文件,用來放置所有.jsp文件,這個jsp文件名是作為Action中方法 return的值。;
註意:1)在WEB-INF目錄下的jsp頁面只能通過程式來訪問,外部訪問不到。(安全)
2)添加頁面的jsp,要註意控制項的屬性名是javabean的屬性名。