激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringMVC之簡單的增刪改查示例(SSM整合)

SpringMVC之簡單的增刪改查示例(SSM整合)

2020-08-23 15:01鎮屌要逆襲 Java教程

本篇文章主要介紹了SpringMVC之簡單的增刪改查示例(SSM整合),這個例子是基于SpringMVC+Spring+Mybatis實現的。有興趣的可以了解一下。

雖然已經在做關于SpringMVC的項目。但是還沒有寫一些比較系統的博客。今天就先來說一說最簡單的增刪改查吧。這個例子是基于SpringMVC+Spring+Mybatis實現的。

環境配置

主要是幾項配置:springmvc的配置,spring的配置,MyBatis的配置,jdbc的配置,和web.xml配置

springmvc.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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的配置)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?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的配置)

?
1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:8888/blog
jdbc.username=root
jdbc.password=123456

web.xml的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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層實現

  

?
1
2
3
4
5
6
7
8
9
@RequestMapping("/list")
 public String UserList(Model model) {
 
   List<User> list =userService.findAll();
   //傳遞數據至前端
   model.addAttribute("list",list);
   //返回對應視圖
   return "itemsList";
 }

對應的Service實現層

?
1
2
3
4
5
6
@Override
public List<User> findAll() {
  UserExample example = new UserExample();
  List<User> list=  userMapper.selectByExample(example);
  return list;
}

前端頁面實現細節

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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層實現

?
1
2
3
4
5
6
7
@RequestMapping("/edit")
public String Edit(Integer iduser,Model model)
{
  User user=userService.findById(iduser);
  model.addAttribute("item",user);
  return "editItem";
}

Service實現層實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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";
}

前端頁面實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<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層實現

?
1
2
3
4
5
6
7
@RequestMapping("/deleteByID")
public String deleteByID(Integer iduser)
{
 
  userService.deleteById(iduser);
  return "redirect:list";
}

Service實現層實現

?
1
2
3
4
5
@Override
public void deleteById(Integer iduser) {
  // TODO Auto-generated method stub
  userMapper.deleteByPrimaryKey(iduser);
}

前端頁面上需要做的修改。已經在上述列表頁面展示過了。在此不再贅述。

新增數據

Controller層實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//超鏈接到對應的頁面
@RequestMapping("/add")
public String Add()
{
  return "AddUser";
}
//保存數據到數據庫后跳轉到列表頁面
@RequestMapping("/addUser")
public String Insert(User user)
{
  userService.insert(user);
  return "redirect:list";
}

Service實現層實現

?
1
2
3
4
5
@Override
public void insert(User user) {
  userMapper.insert(user);
 
}

前端頁面實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 看免费毛片 | 羞羞视频.www在线观看 | 久久免费视屏 | 日韩一级电影在线观看 | 免费一级欧美 | 成人午夜视频免费看 | 久久精品一区二区三 | 欧美一级www片免费观看 | 黄色av网站免费 | 国产一区二区三区视频在线观看 | 精精国产xxxx视频在线野外 | 成人免费看视频 | 久久精品亚洲一区二区三区观看模式 | 91免费视频版 | 成人毛片在线免费观看 | 免费一区二区三区 | 久久国产精品区 | xxx18hd18hd日本| 香蕉久久久| 亚洲国产精品久久久久久久久久 | 91网站链接| 久久精品成人免费国产片桃视频 | 极品销魂一区二区三区 | 在线观看第一区 | 国产免费久久久久 | 欧美日韩亚洲国产 | 嫩嫩的freehdxxx | 美女视频黄a视频免费全过程 | 欧美日本免费一区二区三区 | 国产精品高潮99久久久久久久 | 中国免费黄色 | 直接在线观看的三级网址 | 免费国产不卡午夜福在线 | 人禽l交免费视频 | 成人福利在线免费观看 | 成人爱爱电影 | 91精品国产92久久久久 | 亚洲第一成人在线观看 | 国产精品久久久久久影院8一贰佰 | 蜜桃麻豆视频 | 国产成人综合在线视频 |