SpringMVC之简单的增删改查示例(SSM整合)
本篇文章主要介绍了SpringMVC之简单的增删改查示例(SSM整合),这个例子是基于SpringMVC+Spring+Mybatis实现的。有兴趣的可以了解一下。
虽然已经在做关于SpringMVC的项目。但是还没有写一些比较系统的博客。今天就先来说一说最简单的增删改查吧。这个例子是基于SpringMVC+Spring+Mybatis实现的。
环境配置
主要是几项配置: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:默认创建了多个对象: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>
<!-- 第二步:创建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接口代理开发
* 接口类名和映射文件必须同名
* 接口类和映射文件必须在同一个目录 下
* 映射文件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;
}
前端页面实现细节
<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>
以上就是一个完整的增删改查的全部过程。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
原文链接:http://www.jianshu.com/p/9f00cf73cd8e
SpringMVC之简单的增删改查示例(SSM整合)的更多相关文章
- springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能
转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包 数据库表 数据库表就不用教大家了,一张表,很简 ...
- springboot(十五):springboot+jpa+thymeleaf增删改查示例
这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个demo来试试它的效果,越简单越容易上 ...
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
1.通过 pip 安装 pymysql 进入 cmd 输入 pip install pymysql 回车等待安装完成: 安装完成后出现如图相关信息,表示安装成功. 2.测试连接 import ...
- (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...
- Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个 Demo 来试试它的效果,越 ...
- BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块
NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...
- salesforce 零基础学习(五十一)使用 Salesforce.com SOAP API 实现用户登录以及简单的增删改查(JAVA访问salesforce)
此篇请参看:https://resources.docs.salesforce.com/202/latest/en-us/sfdc/pdf/salesforce_developer_environme ...
- MyBatis学习--简单的增删改查
jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: Public static void main(String[] args) ...
- 通过JDBC进行简单的增删改查
通过JDBC进行简单的增删改查(以MySQL为例) 目录 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JDBC基本操 ...
随机推荐
- Spring Security入门(3-3)Spring Security 手工配置并注入 authenticationProvider 和 异常信息传递
特别注意的是 这样就能保证抛出UsernameNotFoundException时,前台显示出错信息: 另外,ps:
- vue2.0项目引入element-ui
在项目中,为了方便我们工作和开发效率,常常引入一些框架来帮助我们完成高效的工作,今天我们就用vue来搭建一下框架,并且引入element-ui这个框架.安装流程也是我从失败中摸索到的,希望能帮助大家. ...
- jq中的表单验证插件------jquery.validate
今天我们来说一下表单验证,有人说我们在进行表单验证的时候使用正则来验证是非常麻烦的,现在我来给大家介绍一下表单验证的插件:jquery.validate.min.js 它是与jquery一起结合用来使 ...
- POJ-3421 X-factor Chains---求因子+递推 或 素因子+组合数学
题目链接: https://vjudge.net/problem/POJ-3421 题目大意: 给你一个数X,将X分解成1~X的因子数列,前一个数可以整数后一个数,求满足条件的最大链长以及有多少条这样 ...
- 关于web XSS注入问题
对web安全方面的知识非常薄弱,这篇文章把Xss跨站攻击和sql注入的相关知识整理了下,附带公司写的一个filer. 对于防止sql注入发生,我只用过简单拼接字符串的注入及参数化查询,可以说没什么好经 ...
- STL源码剖析 — 空间配置器(allocator)
前言 以STL的实现角度而言,第一个需要介绍的就是空间配置器,因为整个STL的操作对象都存放在容器之中. 你完全可以实现一个直接向硬件存取空间的allocator. 下面介绍的是SGI STL提供的配 ...
- [原创]手把手教你写网络爬虫(4):Scrapy入门
手把手教你写网络爬虫(4) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 上期我们理性的分析了为什么要学习Scrapy,理由只有一个,那就是免费,一分钱都不用花! 咦?怎么有人扔西红柿 ...
- [LeetCode] Zuma Game 祖玛游戏
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...
- servlet之session设置
商品对象,购物车对象,servlet的实现 商品: package app02d;public class Product { private int id; private String ...
- Ubuntu安装及配置virtualenv,virtualenvwrapeer
安装virtualenv pip install virtualenv 如果下载较慢,可以添加豆瓣源: pip install -i https://pypi.douban.com/simple/ v ...