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基本操 ...
随机推荐
- HTTP协议扫盲(二)HTTP协议的请求方法、请求头和响应头
一.HTTP请求方法 Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET,POST,PUT,DELETE. 一个URL地址用于描述一个网络上的资源,而HTTP中的GET, POST ...
- PHP基础(2)
测试模板 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Spring 环境与profile(三)——利用maven的resources、filter和profile实现不同环境使用不同配置文件
基本概念 profiles定义了各个环境的变量id filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值 resources中是定义哪些目录下的文件会被配置文 ...
- vue computed 原理
vue computed 主要依靠数据依赖来更新,这里不展示computed源代码,只展示核心思想. computed: { a(){ return this.b ++ } } data:{ b: 1 ...
- Python之Scrapy爬虫框架 入门实例(一)
一.开发环境 1.安装 scrapy 2.安装 python2.7 3.安装编辑器 PyCharm 二.创建scrapy项目pachong 1.在命令行输入命令:scrapy startproject ...
- git初试
在gitLab上新建一个项目,creat项目文件之后,进入到项目的路径之后,复制命令git clone ‘git@gitlab.touzila.com:xiacaixiang/gitgitTest1. ...
- JPA(一):简介
JPA是什么 Java Persistence API:用于对象持久化的API Java EE 5.0平台标准的ORM规范,使得应用程序以统一的方式访问持久层. JPA和Hibernate的关系 JP ...
- 开源协议瞎扯淡,什么是 MIT 协议?[转]
图片来源:http://ruby-china.org/topics/15979
- Now trying to drop the old temporary tablespace, the session hangs.
1.描述 问题描述:删除临时表空间时,会话Hangs挂起 SQL> drop tablespace TEMP_B including contents and datafiles; 2.故障诊断 ...
- Spring Cloud Eureka 自我保护机制
Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果 ...