spring4 學習4 spring MVC+mybatis+Mysql

来源:http://www.cnblogs.com/mkijnbhop/archive/2016/01/25/5157021.html
-Advertisement-
Play Games

在前面搭建的基礎上,引入新的jar包如下:aopalliance-1.0.jaraspectjweaver-1.8.8.jarmybatis-3.3.0.jarmybatis-spring-1.2.3.jarmysql-connector-java-5.1.31.jarspring-aop-4.2....


在前面搭建的基礎上,引入新的jar包如下:

aopalliance-1.0.jar
aspectjweaver-1.8.8.jar
mybatis-3.3.0.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.31.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-jdbc-4.2.4.RELEASE.jar
spring-orm-4.2.4.RELEASE.jar
spring-oxm-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar

代碼結構如下:下載地址 

 


localConfig.properties

Java代碼  收藏代碼
  1. #datasource properties  
  2. jdbc.url=jdbc:mysql://localhost:3306/world  
  3. jdbc.username=root  
  4. jdbc.password=root  

 

spring-dataSource.xml

 

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  8.     http://www.springframework.org/schema/aop   
  9.     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd    
  10.     http://www.springframework.org/schema/tx   
  11.     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">  
  12.   
  13.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  14.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  15.         <property name="url" value="${jdbc.url}"></property>  
  16.         <property name="username" value="${jdbc.username}"></property>  
  17.         <property name="password" value="${jdbc.password}"></property>  
  18.     </bean>  
  19.       
  20.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  21.         <property name="dataSource" ref="dataSource" />  
  22.     </bean>  
  23.   
  24.     <bean id="transactionManager"  
  25.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  26.         <property name="dataSource" ref="dataSource" />  
  27.     </bean>  
  28.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  29.         <tx:attributes>  
  30.             <tx:method name="*" />  
  31.         </tx:attributes>  
  32.     </tx:advice>  
  33.     <aop:config>  
  34.         <aop:pointcut expression="execution(* com.xx.demo.bsh.*.*.*(..))"  
  35.             id="myPointcut" />  
  36.         <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />  
  37.     </aop:config>  
  38. </beans>  

           配置玩事務後先檢查一下mysql中的表的存儲引擎是否是innoDB。若是MyISAM,要改成InnoDB,因為MyISAM是事務不安全的。

          查看命令:show create table city;

          修改命令:alter table city engine = InnoDB;

spring-applicationContext.xml

Java代碼  收藏代碼
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.              http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  5.              http://www.springframework.org/schema/context  
  6.              http://www.springframework.org/schema/context/spring-context-4.1.xsd">  
  7.     <context:annotation-config />  
  8.     <context:component-scan base-package="com.xx.demo.dao"/>  
  9.     <context:component-scan base-package="com.xx.demo.bsh" />  
  10.     <context:property-placeholder location="classpath:config/env/localConfig.properties" />    
  11.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
  12.         <property name="basePackage" value="com.xx.demo.dao" />    
  13.     </bean>   
  14.     <import resource="classpath:config/spring-dataSource.xml"/>  
  15. </beans>  

 ICityDao.java

Java代碼  收藏代碼
  1. package com.xx.demo.dao.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.ibatis.annotations.Delete;  
  6. import org.apache.ibatis.annotations.Select;  
  7. import org.springframework.stereotype.Repository;  
  8.   
  9. import com.xx.demo.entity.test.CityEO;  
  10.   
  11. @Repository("cityDao")   
  12. public interface ICityDao {  
  13.       
  14.     @Select(value = "select count(1) as count from city")   
  15.     public long countAll();  
  16.   
  17.     @Delete(value="delete from city where id=#{id}")  
  18.     public void deleteCityById(long id);  
  19.       
  20.     @Select(value="select * from city")  
  21.     public List<CityEO> getAllCitys();  
  22.       
  23. }  

CityEO.java

   EO類屬性有資料庫列名一致

Java代碼  收藏代碼
  1. public class CityEO {  
  2.     private int id;  
  3.     private String name;  
  4.     private String countryCode;  
  5.     private String district;  
  6.     private long population;  
  7. ...  
  8. }  

  TestService.java

Java代碼  收藏代碼
  1. package com.xx.demo.bsh.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.springframework.stereotype.Service;  
  8.   
  9. import com.xx.demo.dao.test.ICityDao;  
  10. import com.xx.demo.entity.test.CityEO;  
  11.   
  12. @Service("testService")  
  13. public class TestService {  
  14.       
  15.     @Resource  
  16.     private ICityDao cityDao;  
  17.       
  18.     public void print(){  
  19.         System.out.println("這是服務層方法");  
  20.     }  
  21.       
  22.     public long getCityCount(){  
  23.         return cityDao.countAll();  
  24.     }  
  25.   
  26.     public long deleteCityById(long id) {  
  27.         cityDao.deleteCityById(id);  
  28.         return id;  
  29.     }  
  30.       
  31.     public List<CityEO> getAllCitys(){  
  32.         return cityDao.getAllCitys();  
  33.     }  
  34. }  

 TestController.java

Java代碼  收藏代碼
  1. package com.xx.demo.web.test;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import javax.annotation.Resource;  
  8. import javax.servlet.http.HttpServletRequest;  
  9.   
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.ui.ModelMap;  
  12. import org.springframework.web.bind.annotation.RequestMapping;  
  13. import org.springframework.web.bind.annotation.ResponseBody;  
  14.   
  15. import com.xx.demo.bsh.test.TestService;  
  16. import com.xx.demo.entity.test.CityEO;  
  17.   
  18. @Controller  
  19. public class TestController {  
  20.     @Resource  
  21.     private TestService testService;  
  22.       
  23.     @RequestMapping("/firstPage")  
  24.     public String testMethod(ModelMap model){  
  25.         testService.print();  
  26.         model.put("msg", "velocity 測試");  
  27.         return "test";  
  28.     }  
  29.       
  30.     @RequestMapping("/getCityCount")  
  31.     @ResponseBody  
  32.     public String getCityCount(){  
  33.         Map<String,Object> result = new HashMap<String,Object>();  
  34.         long count = testService.getCityCount();  
  35.         return String.valueOf(count);  
  36.     }  
  37.       
  38.     @RequestMapping("/deleteCityById")  
  39.     @ResponseBody  
  40.     public String deleteCityById(HttpServletRequest request){  
  41.         long id = Long.valueOf(request.getParameter("id"));  
  42.         long result = testService.deleteCityById(id);  
  43.         return "delete--OK--"+result;  
  44.     }  
  45.       
  46.     @RequestMapping("/getAllCitys")  
  47.     public String getAllCitys(HttpServletRequest request,ModelMap model){  
  48.         List<CityEO> citys = testService.getAllCitys();  
  49.         model.put("citys", citys);  
  50.         return "showCitys";  
  51.     }  
  52. }  

 

運行結果:

只貼 了 getAllCitys

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • /*首先~命名空間的存在是為瞭解決 引用不同類中命名衝突問題的*//*1.定義命名空間*/ namespace My; class A{}; namespace Your; class A{}; /*2.在同一個項目中引用命名空間*/ namespace My; class A{};...
  • 利用Python語言實現Grib數據可視化主要依靠三個庫——pygrib、numpy和matplotlib。pygrib是歐洲中期天氣預報中心(ECMWF)的GRIG API C庫的Python介面,通過這個庫可以將Grib數據讀取出來;numpy是Python的一種開源的數值計算擴展,這種工具可用...
  • 上述if語句的等值判斷,可以用switch來代替。註意每個case後面一般要添加break,表示當前這個case執行完了;防止出現case穿透,即繼續執行case,直到遇到break才跳出。下麵例子反過來利用了case穿透現象。【例子】JDK7.0新特性:增強switch在JDK7之前,switch...
  • #!/bin/env python # -*- coding: UTF-8 -*- # 必須以root許可權運行 import socket import sys import timeimport random from struct import * # 計算校驗和 def ...
  • 回到目錄dynamic這個動態類型早在.net3.5時就已經出現了,當時是伴隨的Linq一起讓我們認識的,但在使用時總覺得有點彆扭,因為它是internal的,所以不能跨程式集使用,這對於分層開發的我們來說顯然是不能接受的,所以把dynamic了冷落了很久,應該說是5年吧,哈哈,這幾天在睡覺時,突然...
  • 值對象(value object)===================== 什麼是值對象 維基百科的定義 In computer science, a value object is a small object that represents a simple entity whose equ....
  • 序言在上一篇配置iis負載均衡中我們使用啦微軟的ARR,我在那篇文章也中提到了網站的高可用性,但是ARR只能做請求入口的消息分發服務,這樣如果我們的消息分發伺服器給down掉啦,那麼做再多的應用服務集群也都枉然。這篇文章我主要針對解決這一問題來做分析,引入NLB,相對於ARR來說,ARR算是應用級別...
  • ---單例設計模式之餓漢式--- 創建SingleInstance類 1 /** 2 * 單例設計模式之餓漢式 3 */ 4 public class SingleInstance { 5 /** 6 * 私有化構造方法 7 */ 8 priv...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...