您好,登錄后才能下訂單哦!
小編給大家分享一下javaWeb開發(fā)ssm框架如何搭建,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
介紹
在學(xué)習(xí)javaweb開發(fā)中,常用框架的學(xué)習(xí)是很重要的,通過使用框架進(jìn)行開發(fā),可以很大程度的提升開發(fā)效率,減少開發(fā)者的工作量。隨著互聯(lián)網(wǎng)高速發(fā)展,軟件開發(fā)中使用的框架也是層出不窮,目前使用最廣的框架應(yīng)該是屬于ssm這三大框架,下面就來收悉一下ssm框架的使用。
注:在使用SSM搭建框架之前,需要收悉一下springmvc、spring、mybatis。
SSM框架搭建步驟
注:本文采用idea開發(fā)工具搭建,eclipse工具都差不多
1.創(chuàng)建一個(gè)javaweb maven項(xiàng)目,具體創(chuàng)建方式可參考上一篇“IDEA通過maven構(gòu)建JavaWeb項(xiàng)目步驟詳解”
2.修改項(xiàng)目結(jié)構(gòu),如下圖:
3.根據(jù)上圖結(jié)構(gòu)添加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> <!--開啟駝峰命名自動(dòng)轉(zhuǎn)換 一般數(shù)據(jù)庫命名規(guī)則一般是user_name,而我們實(shí)體類中一般使用userName, 開啟后就可以自動(dòng)映射了 --> <settings> <setting name="mapUnderscoreToCamelCase" value="true"></setting> </settings> </configuration>
4.添加mapper.xml 文件
<?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 namespace="com.cn.itcdns.mapper.UserMapper"> <!--id的值:要和接口UserMapper下的方法名一致 resultType:1.要和方法返回值類型一樣,或者是返回值類型的集合 2.原本需要寫返回值類型的權(quán)限定名路徑, 但是我們?cè)赼pplicationContext里配置了別名所屬的包 所以只要寫類名即可 --> <select id="findAllUsers" resultType="User"> select * from user </select> </mapper>
5.添加jdbc.properties 文件
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm_db jdbc.username=root jdbc.password=root
6.添加applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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"> <!-- 引入外部的屬性資源文件,把我們剛剛配置的jdbc配置文件引入進(jìn)來 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 掃包,只需要掃service的即可,Controller的包springMVC會(huì)掃描, mapper層的,下面類會(huì)幫我們掃描 --> <context:component-scan base-package="com.cn.itcdns.service"> </context:component-scan> <!-- 使用專門掃描mapper接口的類 --> <bean> <property name="basePackage" value="com.cn.itcdns.mapper"></property> </bean> <!-- 配置數(shù)據(jù)源 ,這里了用的是阿里巴巴的德魯伊數(shù)據(jù)源--> <bean id="dataSource"> <property name="driverClassName" value="${jdbc.driverClass}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--sqlSession工廠,我們對(duì)數(shù)據(jù)庫增刪改查就靠他--> <bean id="sqlSessionFactory"> <!-- 注入配置的數(shù)據(jù)源 --> <property name="dataSource" ref="dataSource"></property> <!-- 注入mybatis-config配置文件 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"> </property> <!-- 注入mapper.xml文件位置, 這里的/**/*.xml寫法,只要是/mybatis/mapper下的都可以注入進(jìn)來, 哪怕里面又加了幾個(gè)文件夾--> <property name="MapperLocations" value="classpath:mybatis/mapper/**/*.xml"> </property> <!-- 配置別名所屬的包名,配置了這個(gè),我們UserMapper.xml文件下的 resultType就只需要寫類名即可--> <property name="typeAliasesPackage" value="com.cn.itcdns.domain"> </property> </bean> <!-- 配置事務(wù)管理器 --> <bean id="transactionManager" > <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事務(wù)屬性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--配置事務(wù)的屬性,如果方法名是find開頭,只允許查詢業(yè)務(wù), 不允許增刪改業(yè)務(wù),這樣可以防錯(cuò),如果寫了刪除或者增加功能, 則會(huì)拋出異常,提醒開發(fā)人員--> <tx:method name="find*" read-only="true"/> <!--其他方法做增刪改功能--> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置切面 配置了事務(wù)后,我們肯定要告訴spring,哪些方法需要加上事務(wù) --> <aop:config> <!--配置切點(diǎn)--> <aop:pointcut expression="execution(* com.agu.service.impl.*.*(..))" id="pointcut"/> <!--將advice注入程序中pointcut位置的代碼--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
7.添加springmvc-servlet.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啟注解掃描,掃描controller包 --> <context:component-scan base-package="com.cn.itcdns.controller"> </context:component-scan> <!-- mvc注解驅(qū)動(dòng) --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置springMVC視圖解析器 --> <bean> <!--有時(shí)我們需要將數(shù)據(jù)響應(yīng)到某個(gè)頁面,例如響應(yīng)到/jsp/users.jsp, controller只需要返回"users"--> <!--url路徑的前綴--> <property name="prefix" value="/WEB-INF/jsp/"></property> <!--url路徑的后綴--> <property name="suffix" value=".jsp"></property> </bean> <!-- 處理靜態(tài)資源攔截問題,如果是靜態(tài)資源的請(qǐng)求,則默認(rèn)服務(wù)器的servlet處理, 如果不是,則交給DispatcherServlet處理--> <mvc:default-servlet-handler/> </beans>
8.修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 配置字節(jié)碼過濾器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 指定spring配置文件名 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <!-- 工程啟動(dòng)時(shí),創(chuàng)建IOC容器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- springmvc入口 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- 指定springmvc配置文件路徑 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc-servlet.xml</param-value> </init-param> <!--項(xiàng)目啟動(dòng)時(shí),就加載此servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 攔截除了jsp以外的所有請(qǐng)求(不是/*) --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
9.創(chuàng)建實(shí)體Bean,例如:User.java
package com.cn.itcdns.domain; import java.io.Serializable; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; // 用戶名 private String userName; // 密碼 private String password; // 姓名 private String name; // 年齡 private Integer age; // 性別,1男性,2女性 private Integer sex; // 出生日期 @DateTimeFormat(pattern = "yyyy-MM-dd") private Date birthday; // 創(chuàng)建時(shí)間 private Date created; // 更新時(shí)間 private Date updated; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName +".password=" + password + ", name=" + name + ", age=" + age + ", sex=" + sex +",birthday=" + birthday + ", created=" + created + ", updated=" + updated + "]"; } }
10.創(chuàng)建DAO,例如:UserMapper.java
package com.cn.itcdns.mapper; import com.cn.itcdns.domain.User; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserMapper{ List<User> findAllUsers(); }
11.定義service接口,例如:UserService.java
package com.cn.itcdns.service; import com.cn.itcdns.domain.User; import java.util.List; public interface UserService { /** * 查詢所有用戶信息 * @return */ List<User> findAllUsers(); }
12.定義UserService接口實(shí)現(xiàn),例如:UserServiceImpl.java
package com.cn.itcdns.service.impl; import com.cn.itcdns.domain.User; import com.cn.itcdns.mapper.UserMapper; import com.cn.itcdns.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> findAllUsers() { List<User> list = userMapper.findAllUsers(); return list; } }
13.定義controller控制器,例如:UserController.java
package com.cn.itcdns.controller; import java.util.List; import com.cn.itcdns.domain.User; import com.cn.itcdns.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("user") public class UserController { @Autowired private UserService userService; @RequestMapping("list") public String test1(Model model) { List<User> users = userService.findAllUsers(); model.addAttribute("userList", users); return "userList"; } }
14.創(chuàng)建jsp頁面,例如:userList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <link rel="stylesheet" type="text/css" href="/css/user.css"/> </head> <body> <table id="customers" border="1px"> <tr> <th>編號(hào)</th> <th>用戶名</th> <th>姓名</th> <th>年齡</th> <th>性別</th> <th>出生日期</th> <th>創(chuàng)建時(shí)間</th> <th>更新時(shí)間</th> </tr> <c:forEach items="${userList}" var="user"> <tr> <td>${user.id}</td> <td>${user.userName}</td> <td>${user.name}</td> <td>${user.age}</td> <td> <c:choose> <c:when test="${user.sex==1}">男</c:when> <c:otherwise>女</c:otherwise> </c:choose> </td> <td> <fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"/> </td> <td> <fmt:formatDate value="${user.created}" pattern="yyyy-MM-dd HH:mm:ss"/> </td> <td> <fmt:formatDate value="${user.updated}" pattern="yyyy-MM-dd HH:mm:ss"/> </td> </tr> </c:forEach> </table> </body> </html>
到此,項(xiàng)目創(chuàng)建完畢,將項(xiàng)目編譯后打包放入到tomcat中啟動(dòng),訪問得到如下結(jié)果:
以上是“javaWeb開發(fā)ssm框架如何搭建”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。