SpringMVC之簡單的增刪改查示例(SSM整合)
雖然已經在做關于SpringMVC的項目。但是還沒有寫一些比較系統的博客。今天就先來說一說最簡單的增刪改查吧。這個例子是基于SpringMVC+Spring+Mybatis實現的。
環(huán)境配置
主要是幾項配置: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:默認創(chuàng)建了多個對象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
也就提供對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>
<!-- 第一步:配置數據源 -->
<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。生產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接口代理開發(fā)
* 接口類名和映射文件必須同名
* 接口類和映射文件必須在同一個目錄 下
* 映射文件namespace名字必須是接口的全類路徑名
* 接口的方法名必須和映射Statement的id一致
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhao.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 第三步:事務 -->
<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(數據庫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的配置中已經添加了對數據源的支持。。在基礎的應用中我們并不需要對MyBatis做什么配置。因此基本的配置就是如上所示。
增刪改查的操作
首先是查的操作
列表顯示所有信息
Controller層實現
@RequestMapping("/list")
public String UserList(Model model) {
List<User> list =userService.findAll();
//傳遞數據至前端
model.addAttribute("list",list);
//返回對應視圖
return "itemsList";
}
對應的Service實現層
@Override
public List<User> findAll() {
UserExample example = new UserExample();
List<User> list= userMapper.selectByExample(example);
return list;
}
前端頁面實現細節(jié)
<table width="100%" border=1>
<tr>
<td>ID</td>
<td>用戶名</td>
<td>密碼</td>
<td>昵稱</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>
根據id修改相應的數據
Controller層實現
@RequestMapping("/edit")
public String Edit(Integer iduser,Model model)
{
User user=userService.findById(iduser);
model.addAttribute("item",user);
return "editItem";
}
Service實現層實現
@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);
//跳轉到對應的list路由
return "redirect:list";
}
前端頁面實現
<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>用戶名稱</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>昵稱</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>
上述流程并未對是否查詢成功做對應處理。有興趣的同學可以嘗試將其補充完整
根據id刪除對應的數據
Controller層實現
@RequestMapping("/deleteByID")
public String deleteByID(Integer iduser)
{
userService.deleteById(iduser);
return "redirect:list";
}
Service實現層實現
@Override
public void deleteById(Integer iduser) {
// TODO Auto-generated method stub
userMapper.deleteByPrimaryKey(iduser);
}
前端頁面上需要做的修改。已經在上述列表頁面展示過了。在此不再贅述。
新增數據
Controller層實現
//超鏈接到對應的頁面
@RequestMapping("/add")
public String Add()
{
return "AddUser";
}
//保存數據到數據庫后跳轉到列表頁面
@RequestMapping("/addUser")
public String Insert(User user)
{
userService.insert(user);
return "redirect:list";
}
Service實現層實現
@Override
public void insert(User user) {
userMapper.insert(user);
}
前端頁面實現
<form id="itemForm" action="${pageContext.request.contextPath }/user/addUser" method="post">
商品信息:
<table width="100%" border=1>
<tr>
<td>用戶名稱</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td>昵稱</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>
以上就是一個完整的增刪改查的全部過程。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于java構造方法Vector創(chuàng)建對象源碼分析
這篇文章主要介紹了java構造函數中對Vector源碼及原理的分析,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升職加薪2021-09-09
Java中的CopyOnWriteArrayList容器解析
這篇文章主要介紹了Java中的CopyOnWriteArrayList容器解析,CopyOnWriteArrayList容器允許并發(fā)讀,讀操作是無鎖的,性能較高。至于寫操作,比如向容器中添加一個元素,則首先將當前容器復制一份,然后在新副本上執(zhí)行寫操作,需要的朋友可以參考下2023-12-12

