SpringMVC之簡(jiǎn)單的增刪改查示例(SSM整合)
雖然已經(jīng)在做關(guān)于SpringMVC的項(xiàng)目。但是還沒(méi)有寫(xiě)一些比較系統(tǒng)的博客。今天就先來(lái)說(shuō)一說(shuō)最簡(jiǎn)單的增刪改查吧。這個(gè)例子是基于SpringMVC+Spring+Mybatis實(shí)現(xiàn)的。
環(huán)境配置
主要是幾項(xiàng)配置:springmvc的配置,spring的配置,MyBatis的配置,jdbc的配置,和web.xml配置
springmvc.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 文件掃描 -->
<context:component-scan base-package="com.zhao"></context:component-scan>
<!-- annotation-driven:默認(rèn)創(chuàng)建了多個(gè)對(duì)象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
也就提供對(duì)json格式支持
-->
<mvc:annotation-driven/>
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
beans.xml(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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="com.zhao"></context:component-scan>
<!-- 第一步:配置數(shù)據(jù)源 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 第二步:創(chuàng)建sqlSessionFactory。生產(chǎn)sqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<!-- 配置mybatis接口代理開(kāi)發(fā)
* 接口類(lèi)名和映射文件必須同名
* 接口類(lèi)和映射文件必須在同一個(gè)目錄 下
* 映射文件namespace名字必須是接口的全類(lèi)路徑名
* 接口的方法名必須和映射Statement的id一致
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhao.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 第三步:事務(wù) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置攔截service -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zhao.service.*.*(..))"/>
</aop:config>
</beans>
jdbc.properties(數(shù)據(jù)庫(kù)jdbc的配置)
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:8888/blog jdbc.username=root jdbc.password=123456
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" xmlns:web="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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <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.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring的配置中已經(jīng)添加了對(duì)數(shù)據(jù)源的支持。。在基礎(chǔ)的應(yīng)用中我們并不需要對(duì)MyBatis做什么配置。因此基本的配置就是如上所示。
增刪改查的操作
首先是查的操作
列表顯示所有信息
Controller層實(shí)現(xiàn)
@RequestMapping("/list")
public String UserList(Model model) {
List<User> list =userService.findAll();
//傳遞數(shù)據(jù)至前端
model.addAttribute("list",list);
//返回對(duì)應(yīng)視圖
return "itemsList";
}
對(duì)應(yīng)的Service實(shí)現(xiàn)層
@Override
public List<User> findAll() {
UserExample example = new UserExample();
List<User> list= userMapper.selectByExample(example);
return list;
}
前端頁(yè)面實(shí)現(xiàn)細(xì)節(jié)
<table width="100%" border=1>
<tr>
<td>ID</td>
<td>用戶(hù)名</td>
<td>密碼</td>
<td>昵稱(chēng)</td>
<td>電子郵箱</td>
<td>操作</td>
</tr>
<c:forEach items="${list}" var="item">
<tr>
<td>
<input type="checkbox" name="iduser" value="${item.iduser}">
</td>
<td>${item.username }</td>
<td>${item.password }</td>
<td>${item.nickname }</td>
<td>${item.email }</td>
<td><a href="${pageContext.request.contextPath }/user/edit?iduser=${item.iduser}" rel="external nofollow" >修改</a>
<a href="${pageContext.request.contextPath }/user/deleteByID?iduser=${item.iduser}" rel="external nofollow" >刪除</a>
</td>
</tr>
</c:forEach>
根據(jù)id修改相應(yīng)的數(shù)據(jù)
Controller層實(shí)現(xiàn)
@RequestMapping("/edit")
public String Edit(Integer iduser,Model model)
{
User user=userService.findById(iduser);
model.addAttribute("item",user);
return "editItem";
}
Service實(shí)現(xiàn)層實(shí)現(xiàn)
@RequestMapping("/edit")
public String Edit(Integer iduser,Model model)
{
User user=userService.findById(iduser);
//將要修改的值傳遞到前端
model.addAttribute("item",user);
return "editItem";
}
@RequestMapping(value ="/saveOrUpdate",method = RequestMethod.POST)
public String saveOrUpdate(User user)
{
//保存修改的值
userService.update(user);
//跳轉(zhuǎn)到對(duì)應(yīng)的list路由
return "redirect:list";
}
前端頁(yè)面實(shí)現(xiàn)
<form id="itemForm" action="${pageContext.request.contextPath }/user/saveOrUpdate" method="post">
<input type="hidden" name="iduser" value="${item.iduser }"/>
修改商品信息:
<table width="100%" border=1>
<tr>
<td>用戶(hù)名稱(chēng)</td>
<td><input type="text" name="username" value="${item.username }"/></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" name="password" value="${item.password}"/></td>
</tr>
<tr>
<td>昵稱(chēng)</td>
<td><input type="text" name="nickname" value="${item.nickname}"/></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email" value="${item.email}"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
上述流程并未對(duì)是否查詢(xún)成功做對(duì)應(yīng)處理。有興趣的同學(xué)可以嘗試將其補(bǔ)充完整
根據(jù)id刪除對(duì)應(yīng)的數(shù)據(jù)
Controller層實(shí)現(xiàn)
@RequestMapping("/deleteByID")
public String deleteByID(Integer iduser)
{
userService.deleteById(iduser);
return "redirect:list";
}
Service實(shí)現(xiàn)層實(shí)現(xiàn)
@Override
public void deleteById(Integer iduser) {
// TODO Auto-generated method stub
userMapper.deleteByPrimaryKey(iduser);
}
前端頁(yè)面上需要做的修改。已經(jīng)在上述列表頁(yè)面展示過(guò)了。在此不再贅述。
新增數(shù)據(jù)
Controller層實(shí)現(xiàn)
//超鏈接到對(duì)應(yīng)的頁(yè)面
@RequestMapping("/add")
public String Add()
{
return "AddUser";
}
//保存數(shù)據(jù)到數(shù)據(jù)庫(kù)后跳轉(zhuǎn)到列表頁(yè)面
@RequestMapping("/addUser")
public String Insert(User user)
{
userService.insert(user);
return "redirect:list";
}
Service實(shí)現(xiàn)層實(shí)現(xiàn)
@Override
public void insert(User user) {
userMapper.insert(user);
}
前端頁(yè)面實(shí)現(xiàn)
<form id="itemForm" action="${pageContext.request.contextPath }/user/addUser" method="post">
商品信息:
<table width="100%" border=1>
<tr>
<td>用戶(hù)名稱(chēng)</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td>昵稱(chēng)</td>
<td><input type="text" name="nickname" /></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
以上就是一個(gè)完整的增刪改查的全部過(guò)程。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于java構(gòu)造方法Vector創(chuàng)建對(duì)象源碼分析
這篇文章主要介紹了java構(gòu)造函數(shù)中對(duì)Vector源碼及原理的分析,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升職加薪2021-09-09
Ubuntu 15下安裝Eclipse經(jīng)驗(yàn)分享
這篇文章主要為大家分享了Ubuntu 15下安裝Eclipse經(jīng)驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
springboot+idea熱部署的實(shí)現(xiàn)方法(自動(dòng)刷新)
這篇文章主要介紹了springboot+idea熱部署的實(shí)現(xiàn)方法(自動(dòng)刷新),本文分步驟通過(guò)實(shí)例代碼截圖相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
學(xué)生視角看Java 面向?qū)ο蟮睦^承本質(zhì)
繼承是java面向?qū)ο缶幊碳夹g(shù)的一塊基石,因?yàn)樗试S創(chuàng)建分等級(jí)層次的類(lèi)。繼承就是子類(lèi)繼承父類(lèi)的特征和行為,使得子類(lèi)對(duì)象(實(shí)例)具有父類(lèi)的實(shí)例域和方法,或子類(lèi)從父類(lèi)繼承方法,使得子類(lèi)具有父類(lèi)相同的行為2022-03-03
SprintBoot深入淺出講解場(chǎng)景啟動(dòng)器Starter
本篇文章將和大家分享一下 Spring Boot 框架中的 Starters 場(chǎng)景啟動(dòng)器的內(nèi)容,關(guān)于 Starters 具體是用來(lái)做什么的,以及在開(kāi)發(fā) Spring Boot項(xiàng)目前,要如何自定義一個(gè) Starters 場(chǎng)景啟動(dòng)器2022-06-06
Java中的CopyOnWriteArrayList容器解析
這篇文章主要介紹了Java中的CopyOnWriteArrayList容器解析,CopyOnWriteArrayList容器允許并發(fā)讀,讀操作是無(wú)鎖的,性能較高。至于寫(xiě)操作,比如向容器中添加一個(gè)元素,則首先將當(dāng)前容器復(fù)制一份,然后在新副本上執(zhí)行寫(xiě)操作,需要的朋友可以參考下2023-12-12
Java開(kāi)發(fā)實(shí)現(xiàn)人機(jī)猜拳游戲
這篇文章主要為大家詳細(xì)介紹了Java開(kāi)發(fā)實(shí)現(xiàn)人機(jī)猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Java Web之限制用戶(hù)多處登錄實(shí)例代碼
本篇文章主要介紹了Java Web之限制用戶(hù)多處登錄實(shí)例代碼,可以限制單個(gè)用戶(hù)在多個(gè)終端登錄。非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
Java基礎(chǔ)強(qiáng)化訓(xùn)練輸入錯(cuò)誤即結(jié)束進(jìn)程
本文主要介紹了Java編程的基礎(chǔ)知識(shí)強(qiáng)化應(yīng)用,文中實(shí)例涉及到了許多基礎(chǔ)知識(shí),new對(duì)象,控制臺(tái)輸入,if語(yǔ)句等。很實(shí)用,需要的朋友可以參考下2017-09-09

