最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年03月03日 10:02:58   作者:鎮(zhèn)屌要逆襲  
本篇文章主要介紹了SpringMVC之簡(jiǎn)單的增刪改查示例(SSM整合),這個(gè)例子是基于SpringMVC+Spring+Mybatis實(shí)現(xiàn)的。有興趣的可以了解一下。

雖然已經(jīng)在做關(guān)于SpringMVC的項(xiàng)目。但是還沒有寫一些比較系統(tǒng)的博客。今天就先來說一說最簡(jiǎn)單的增刪改查吧。這個(gè)例子是基于SpringMVC+Spring+Mybatis實(shí)現(xiàn)的。

環(huán)境配置

主要是幾項(xiàng)配置: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:默認(rèn)創(chuàng)建了多個(gè)對(duì)象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
    也就提供對(duì)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>

  <!-- 第一步:配置數(shù)據(jù)源 -->
  <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>

  <!-- 第二步:創(chuàng)建sqlSessionFactory。生產(chǎn)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接口代理開發(fā)
    * 接口類名和映射文件必須同名
    * 接口類和映射文件必須在同一個(gè)目錄 下
    * 映射文件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>


  <!-- 第三步:事務(wù) -->
  <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(數(shù)據(jù)庫(kù)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的配置中已經(jīng)添加了對(duì)數(shù)據(jù)源的支持。。在基礎(chǔ)的應(yīng)用中我們并不需要對(duì)MyBatis做什么配置。因此基本的配置就是如上所示。

增刪改查的操作

首先是查的操作

列表顯示所有信息

Controller層實(shí)現(xiàn)

  

 @RequestMapping("/list")
  public String UserList(Model model) {

    List<User> list =userService.findAll();
    //傳遞數(shù)據(jù)至前端
    model.addAttribute("list",list);
    //返回對(duì)應(yīng)視圖
    return "itemsList";
  }

對(duì)應(yīng)的Service實(shí)現(xiàn)層

  @Override
  public List<User> findAll() {
    UserExample example = new UserExample();
    List<User> list=  userMapper.selectByExample(example);
    return list;
  }

前端頁面實(shí)現(xiàn)細(xì)節(jié)

<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>

根據(jù)id修改相應(yīng)的數(shù)據(jù)

Controller層實(shí)現(xiàn)

  @RequestMapping("/edit")
  public String Edit(Integer iduser,Model model)
  {
    User user=userService.findById(iduser);
    model.addAttribute("item",user);
    return "editItem";
  }

Service實(shí)現(xiàn)層實(shí)現(xiàn)

  @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);
    //跳轉(zhuǎn)到對(duì)應(yīng)的list路由
    return "redirect:list";
  }

前端頁面實(shí)現(xiàn)

<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>

上述流程并未對(duì)是否查詢成功做對(duì)應(yīng)處理。有興趣的同學(xué)可以嘗試將其補(bǔ)充完整

根據(jù)id刪除對(duì)應(yīng)的數(shù)據(jù)

Controller層實(shí)現(xiàn)

  @RequestMapping("/deleteByID")
  public String deleteByID(Integer iduser)
  {

    userService.deleteById(iduser);
    return "redirect:list";
  }

Service實(shí)現(xiàn)層實(shí)現(xiàn)

  @Override
  public void deleteById(Integer iduser) {
    // TODO Auto-generated method stub
    userMapper.deleteByPrimaryKey(iduser);
  }

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

新增數(shù)據(jù)

Controller層實(shí)現(xiàn)

  //超鏈接到對(duì)應(yīng)的頁面
  @RequestMapping("/add")
  public String Add()
  {
    return "AddUser";
  }
  //保存數(shù)據(jù)到數(shù)據(jù)庫(kù)后跳轉(zhuǎn)到列表頁面
  @RequestMapping("/addUser")
  public String Insert(User user)
  {
    userService.insert(user);
    return "redirect:list";
  }

Service實(shí)現(xiàn)層實(shí)現(xiàn)

  @Override
  public void insert(User user) {
    userMapper.insert(user);

  }

前端頁面實(shí)現(xiàn)

<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>

以上就是一個(gè)完整的增刪改查的全部過程。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過程

    SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過程

    SpringSecurity是通過過濾器鏈來完成的,接下來的驗(yàn)證碼,可以嘗試創(chuàng)建一個(gè)過濾器放到Security的過濾器鏈中,在自定義的過濾器中比較驗(yàn)證碼,本文通過實(shí)例代碼介紹SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過程,感興趣的朋友一起看看吧
    2023-12-12
  • 關(guān)于@Value注解取不到值的幾種情況

    關(guān)于@Value注解取不到值的幾種情況

    這篇文章主要介紹了關(guān)于@Value注解取不到值的幾種情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot結(jié)合Maven項(xiàng)目依賴版本沖突問題解決

    SpringBoot結(jié)合Maven項(xiàng)目依賴版本沖突問題解決

    本文主要介紹了SpringBoot結(jié)合Maven項(xiàng)目依賴版本沖突問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java開發(fā)工具Eclipse使用技巧全局搜索和更替

    Java開發(fā)工具Eclipse使用技巧全局搜索和更替

    這篇文章主要介紹了Java開發(fā)工具Eclipse使用技巧全局搜索和更替,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • JAVA SPI機(jī)制詳解使用方法

    JAVA SPI機(jī)制詳解使用方法

    Java定義了一套JDBC的接口,但并未提供具體實(shí)現(xiàn)類,而是在不同云廠商提供的數(shù)據(jù)庫(kù)實(shí)現(xiàn)包。這篇文章給大家介紹Java的SPI機(jī)制,感興趣的朋友一起看看吧
    2022-07-07
  • Spring boot整合Mybatis實(shí)現(xiàn)級(jí)聯(lián)一對(duì)多CRUD操作的完整步驟

    Spring boot整合Mybatis實(shí)現(xiàn)級(jí)聯(lián)一對(duì)多CRUD操作的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring boot整合Mybatis實(shí)現(xiàn)級(jí)聯(lián)一對(duì)多CRUD操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼

    Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼

    這篇文章主要介紹了Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼,歸并排序是建立在歸并操作上的一種有效的排序算法,該算法是采用分治法的一個(gè)非常典型的應(yīng)用,將已有序的子序列合并,得到完全有序的序列,需要的朋友可以參考下
    2023-10-10
  • 詳解JAVA 時(shí)間處理相關(guān)類

    詳解JAVA 時(shí)間處理相關(guān)類

    這篇文章主要介紹了JAVA 時(shí)間處理相關(guān)類的知識(shí),文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Java數(shù)據(jù)結(jié)構(gòu)之順序表詳解

    Java數(shù)據(jù)結(jié)構(gòu)之順序表詳解

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之順序表詳解,線性表在邏輯上是線性結(jié)構(gòu),也就說是連續(xù)的一條直線。但是在物理結(jié)構(gòu)上并不一定是連續(xù)的,線性表在物理上存儲(chǔ)時(shí),通常以數(shù)組和鏈?zhǔn)浇Y(jié)構(gòu)的形式存儲(chǔ),需要的朋友可以參考下
    2023-07-07
  • Mybatis基于xml配置實(shí)現(xiàn)單表的增刪改查功能

    Mybatis基于xml配置實(shí)現(xiàn)單表的增刪改查功能

    這篇文章主要介紹了Mybatis基于xml配置實(shí)現(xiàn)單表的增刪改查,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04

最新評(píng)論

普定县| 同仁县| 林甸县| 宜兰市| 红安县| 正阳县| 汾西县| 鄂托克前旗| 土默特左旗| 衡东县| 樟树市| 阜新| 法库县| 福州市| 新营市| 洞口县| 巴林右旗| 梅州市| 塔城市| 云龙县| 巴东县| 崇明县| 裕民县| 罗平县| 宜君县| 天台县| 武川县| 阜新市| 怀集县| 加查县| 抚远县| 故城县| 合阳县| 两当县| 勃利县| 温宿县| 淮安市| 苗栗市| 高密市| 多伦县| 夏河县|