SpringMVC之简单的增删改查示例(SSM整合)
程序员文章站
2024-03-31 19:20:34
虽然已经在做关于springmvc的项目。但是还没有写一些比较系统的博客。今天就先来说一说最简单的增删改查吧。这个例子是基于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>
以上就是一个完整的增删改查的全部过程。希望对大家的学习有所帮助,也希望大家多多支持。