虽然已经在做关于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>
以上就是一个完整的增删改查的全部过程。希望对大家的学习有所帮助,也希望大家多多支持。
# springmvc增删改查
# springmvc的增删改查
# spring
# mvc
# 增删该查
# Spring MVC 框架搭建配置方法及详解
# springMVC几种页面跳转方式小结
# 一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程
# Java简单实现SpringMVC+MyBatis分页插件
# 让你五分钟彻底理解Spring MVC
# 跳转到
# 还没有
# 过了
# 也就
# 多个
# 在此
# 做什么
# 将其
# 有兴趣
# 要对
# 所示
# 不需
# 一说
# 最简单
# 就先
# 第二步
# 第三步
# 几项
# 大家多多
# 主要是
相关文章:
油猴 教程,油猴搜脚本为什么会网页无法显示?
威客平台建站流程解析:高效搭建教程与设计优化方案
视频网站制作教程,怎么样制作优酷网的小视频?
如何通过可视化优化提升建站效果?
交易网站制作流程,我想开通一个网站,注册一个交易网址,需要那些手续?
如何在IIS服务器上快速部署高效网站?
制作网站的基本流程,设计网站的软件是什么?
建站之星如何快速更换网站模板?
如何在宝塔面板中修改默认建站目录?
如何通过VPS建站实现广告与增值服务盈利?
香港服务器部署网站为何提示未备案?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
教育培训网站制作流程,请问edu教育网站的域名怎么申请?
MySQL查询结果复制到新表的方法(更新、插入)
如何做网站制作流程,*游戏网站怎么搭建?
如何在万网主机上快速搭建网站?
详解jQuery中基本的动画方法
Android滚轮选择时间控件使用详解
在线ppt制作网站有哪些,请推荐几个好的课件下载的网站?
Bpmn 2.0的XML文件怎么画流程图
制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?
电商网站制作价格怎么算,网上拍卖流程以及规则?
,网页ppt怎么弄成自己的ppt?
动图在线制作网站有哪些,滑动动图图集怎么做?
完全自定义免费建站平台:主题模板在线生成一站式服务
郑州企业网站制作公司,郑州招聘网站有哪些?
网站建设设计制作营销公司南阳,如何策划设计和建设网站?
建站之星后台密码如何安全设置与找回?
css网站制作参考文献有哪些,易聊怎么注册?
宁波自助建站系统如何快速打造专业企业网站?
沈阳制作网站公司排名,沈阳装饰协会官方网站?
制作营销网站公司,淘特是干什么用的?
如何快速搭建个人网站并优化SEO?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
如何在阿里云香港服务器快速搭建网站?
建站VPS配置与SEO优化指南:关键词排名提升策略
c# await 一个已经完成的Task会发生什么
湖州网站制作公司有哪些,浙江中蓝新能源公司官网?
如何零基础开发自助建站系统?完整教程解析
如何在Ubuntu系统下快速搭建WordPress个人网站?
如何获取上海专业网站定制建站电话?
网站图片在线制作软件,怎么在图片上做链接?
如何高效配置IIS服务器搭建网站?
佛山网站制作系统,佛山企业变更地址网上办理步骤?
潮流网站制作头像软件下载,适合母子的网名有哪些?
建站之星×万网:智能建站系统+自助建站平台一键生成
测试制作网站有哪些,测试性取向的权威测试或者网站?
c# Task.ConfigureAwait(true) 在什么场景下是必须的
建站上传速度慢?如何优化加速网站加载效率?
建站之星后台管理系统如何操作?
*请认真填写需求信息,我们会在24小时内与您取得联系。