前言:ssm框架是spring+springMvc+Mybatis的縮寫,是繼SSH框架后又一個主流的java EE企業級框架,適用於搭建各種大型的企業級應用系統。這不,我所在公司的產品也是基於該主流框架進行搭建的。初識SSM框架是各種的不適應,不過使用久了發現ssm框架異常順手。使用幾個月下來可以說在開發公司產品項目上已經沒什么大問題了,但是我才發現我做的開發都是在前輩搭建好框架的基礎上進行的開發,其實就是ctrl+c,ctrl+v的模式,這不就是搬運工嘛,我可不想只做搬運工。因為我自己以前也做過業務,深知客戶銷售管理的重要性,所以我就想着干脆自己做一個crm的銷售管理系統吧,順便也把SSM框架重頭到尾自己親手搭建一遍,豈不快哉!
說干就干!
1.首先導入所有ssm所需要用到的包,如下圖所示:(網上搜索ssm框架所需要的jar包一搜一大把,不懂的可私聊我拿包)
還有一個包千萬別忘記導入了:
根據你所選擇的數據庫導入相應的jar包
此步驟雖說簡單,但這個步驟成功了,就向着項目搭建成功邁出了一大步了。
2.創建項目結構:
創建resource文件夾,用於存放mybatis以及spring的xml文件的地方(此步驟一定需要注意,要把resource標注為資源文件包,不然后續啟動服務器,是無法掃描到xml文件的)
static文件夾用於存放靜態的資源文件,比如你項目需要引用的前端后台框架模板或者js,css等文件
下面就開始代碼部分:
1.創建SysUser實體類
1 public class SysUser { 2 private int userId;//用戶ID 3 private String userName;//用戶名 4 private String password;//密碼 5 private String name; //姓名 6 private String rights;//權限 7 private String roleId;//角色 8 private String lastLoginTime;//最后登錄時間 9 private String ip;//登錄IP 10 private String status;//狀態 11 private String bz;//備注 12 private String email;//電子郵箱 13 private String number;//編號 14 private String phone;//手機號 15 private int sysId;//所屬公司 16 public SysUser() { 17 super(); 18 } 19 public SysUser(String userName, String password) { 20 super(); 21 this.userName = userName; 22 this.password = password; 23 } 24 25 public SysUser(int userId) { 26 super(); 27 this.userId = userId; 28 } 29 public int getUserId() { 30 return userId; 31 } 32 . 33 . 34 . 35 . 36 . 37 .getter(),setter()代碼自行補充完整
2.新建mybatis-coonfig.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 6 <typeAliases> 7 <!-- 這里添加生成的實體類 --> 8 <typeAlias type="com.cxkj.entity.system.SysUser" alias="SysUser"/> 9 <!-- 傳參數據,封裝的用於傳入mapping中的類--> 10 <typeAlias type="com.cxkj.util.PageData"alias="pd"/> <!-- 設置的pd的參數,將來在mapper.xml文件中有用到 --> 11 </typeAliases> 12 13 14 </configuration>
3.新建pageData實體類,把要傳入mapping中的代碼封裝在這個實體類當中,該類繼承了hashmap並實現了map接口
1 package com.cxkj.util; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.servlet.http.HttpServletRequest; 7 8 import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut; 9 10 import javassist.expr.Instanceof; 11 12 /** 13 * @className: 14 * @author cxkj-gjc 15 * @version 1.0 16 */ 17 public class PageData extends HashMap implements Map { 18 private static final long serialVersionUID = 1L; 19 Map map = null; 20 HttpServletRequest request; 21 22 public PageData(){ 23 24 this.map = new HashMap(); 25 } 26 27 public Object get(Object key){ 28 Object object = null; 29 if (this.map.get(key) instanceof Object[]){ 30 Object[] arr = (Object[])this.map.get(key); 31 object = this.request.getParameter((String)key) == null ? arr : this.request == null ? arr : arr[0]; 32 }else{ 33 object = this.map.get(key); 34 } 35 36 return object; 37 } 38 39 public String getString(String key){ 40 41 return (String)get(key); 42 } 43 44 public Object put(Object key,Object value){ 45 46 return this.map.put(key, value); 47 48 } 49 50 public Object remove(Object key){ 51 52 return this.map.remove(key); 53 } 54 55 public void clear(){ 56 57 this.map.clear(); 58 } 59 60 public boolean isEmpty(){ 61 return this.map.isEmpty(); 62 63 } 64 public void putAll(Map map){ 65 66 this.map.putAll(map); 67 68 } 69 public int size(){ 70 71 return this.map.size(); 72 } 73 74 public boolean containsKey(Object key){ 75 return this.map.containsKey(key); 76 } 77 78 public boolean containsValue(Object value){ 79 80 return this.map.containsValue(value); 81 } 82 }
4.UserMapper.xml文件,在該文件中編寫sql語句實現用戶登錄的功能:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="UserMapper"> 5 <!--表名 --> 6 <sql id="tableName"> 7 sys_user 8 </sql> 9 10 <!-- 字段 --> 11 <sql id="Field"> 12 <!-- USER_ID, --> 13 USERID, 14 USERNAME, 15 PASSWORD, 16 NAME, 17 RIGHTS, 18 ROLEID, 19 LASTLOGIME, 20 IP, 21 STATUS, 22 BZ, 23 EMAIL, 24 NUMBER, 25 PHONE, 26 SYSID 27 </sql> 28 29 <!-- 字段值 --> 30 <sql id="FieldValue"> 31 #{USERID}, 32 #{USERNAME}, 33 #{PASSWORD}, 34 #{NAME}, 35 #{RIGHTS}, 36 #{ROLEID}, 37 #{LASTLOGIME}, 38 #{IP}, 39 #{STATUS}, 40 #{BZ}, 41 #{EMAIL}, 42 #{NUMBER}, 43 #{PHONE}, 44 #{SYSID} 45 </sql> 46 47 <select id="getUserInfo" parameterType="pd" resultMap="pd"> 48 SELECT 49 <include refid="Field"></include> 50 FROM 51 <include refid="tableName"></include> 52 WHERE 53 1 = 1 54 <if test="USERNAME != null"> 55 AND 56 USERNAME = #{USERNAME} 57 </if> 58 <if test="PASSWORD != null"> 59 AND 60 PASSWORD=#{PASSWORD} 61 </if> 62 </select> 63 </mapper>
5.新建applicationContext-mvc.xml文件,這個文件主要用於springmvc控制器的相關配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.1.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 12 13 <!-- 告知Spring,我們啟用注解驅動 --> 14 <mvc:annotation-driven/> 15 <!-- DispatcherServlet不處理靜態資源,交給服務器默認的servlet處理 --> 16 <mvc:default-servlet-handler/> 17 18 <!-- 指定要掃描的包的位置 --> 19 <context:component-scan base-package="com.cxkj.*" /> 20 21 <!-- 對靜態資源文件的訪問,因為Spring MVC會攔截所有請求,導致jsp頁面中對js和CSS的引用也被攔截,配置后可以把對資源的請求交給項目的 22 默認攔截器而不是Spring MVC--> 23 <mvc:resources mapping="/static/**" location="/,/static/" /> 24 <mvc:resources mapping="/uploadFiles/**" location="/,/uploadFiles/" /> 25 <!-- 配置Spring MVC的視圖解析器 --> 26 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 27 <!-- 有時我們需要訪問JSP頁面,可理解為在控制器controller的返回值加前綴和后綴,變成一個可用的URL地址 --> 28 <property name="prefix" value="/WEB-INF/jsp/"/> 29 <property name="suffix" value=".jsp"/> 30 31 </bean> 32 </beans>
6.applicationContext.xml配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/tx 8 http://www.springframework.org/schema/tx/spring-tx.xsd"> 9 10 <!--數據源-鏈接數據庫的基本信息,這里直接寫,不放到*.properties資源文件中--> 11 <bean id="dataSource" 12 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 13 <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> 14 <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;database=EmpManagerDB" /> 15 <property name="username" value="sa" /> 16 <property name="password" value="caj123" /> 17 </bean> 18 19 20 21 <!-- 配置數據源,加載配置,也就是dataSource --> 22 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 23 <property name="dataSource" ref="dataSource" /> 24 <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> 25 <property name="mapperLocations" value="classpath:mybatis/*/*.xml"></property> 26 </bean> 27 <!-- DAO接口所在包名,Spring會自動查找之中的類 --> 28 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 29 <property name="basePackage" value="com.cxkj.dao" /> 30 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 31 </bean> 32 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 33 <constructor-arg index="0" ref="sqlSessionFactory" /> 34 </bean> 35 36 <!--事務管理--> 37 <bean id="transactionManager" 38 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 39 <!--注入dataSource--> 40 <property name="dataSource" ref="dataSource" /> 41 </bean> 42 <!--開啟事務注解掃描--> 43 <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> 44 </beans>
7.web.xml文件編寫:(主要核心的必須要配置的幾點)
1.配置springMvc核心控制器,將所有請求都交給springMvc進行處理
1 <servlet> 2 <servlet-name>springMvc</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <init-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>classpath*:spring/applicationContext-mvc.xml</param-value> 7 <load-on-startup>1</load-on-startup> 8 </init-param> 9 </servlet>
2.為springmvc建立映射關系,並且把所有的request請求都交給springmvc控制器來處理
1 <servlet-mapping> 2 <servlet-name>springMvc</servlet-name> 3 <!-- 攔截所有請求 --> 4 <url-pattern>/</url-pattern> 5 </servlet-mapping>
3.配置監聽器(該配置一定要寫,不然項目啟動會報異常)
1 <listener> 2 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener>
4.最后在把spring容器加載進來
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>classpath*:spring/applicationContext.xml</param-value> 4 </context-param>
以上步驟千萬要記住認真檢查載入的文件名稱是否正確!!比如:applicationContext 會誤寫成了applicationcontent,這是低級錯誤,但是也是很容易犯的錯誤。
到這里就好了,你可以把你的靜態前端后台框架導入到static里面,(我這里使用的是X-admin前端框架,覺得這個框架不錯的可以到這個地址下載:http://x.xuebingsi.com/)啟動服務器試試吧。
啟動項目效果如下,初步成功搭建ssm框架成功!
趕緊試試吧!
小白通往大鳥的路還有很長很長,革命尚未成功,通知仍需努力。一起加油吧。