下麵整合SpringMVC和MyBatis框架,並做一個小案例 創建資料庫springmvc,並創建兩張表,加入一些數據: 兩張表:商品表,用戶表 新建Dynamic Web Project: 導包: 先把簡單的資料庫配置完成: db.properties: MyBatis的配置文件sqlMapCo ...
下麵整合SpringMVC和MyBatis框架,並做一個小案例
創建資料庫springmvc,並創建兩張表,加入一些數據:
兩張表:商品表,用戶表
CREATE DATABASE springmvc; CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL COMMENT '商品名稱', `price` float(10,1) NOT NULL COMMENT '商品定價', `detail` text COMMENT '商品描述', `pic` varchar(64) DEFAULT NULL COMMENT '商品圖片', `createtime` datetime NOT NULL COMMENT '生產日期', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `items` VALUES ('1', '台式機', '3000.0', '該電腦質量非常好!!!!', null, '2016-02-03 13:22:53'); INSERT INTO `items` VALUES ('2', '筆記本', '6000.0', '筆記本性能好,質量好!!!!!', null, '2015-02-09 13:22:57'); INSERT INTO `items` VALUES ('3', '背包', '200.0', '名牌背包,容量大質量好!!!!', null, '2015-02-06 13:23:02'); CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL COMMENT '用戶名稱', `birthday` date DEFAULT NULL COMMENT '生日', `sex` char(1) DEFAULT NULL COMMENT '性別', `address` varchar(256) DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', '王五', null, '2', null); INSERT INTO `user` VALUES ('10', '張三', '2014-07-10', '1', '北京市'); INSERT INTO `user` VALUES ('16', '張小明', null, '1', '河南鄭州'); INSERT INTO `user` VALUES ('22', '陳小明', null, '1', '河南鄭州'); INSERT INTO `user` VALUES ('24', '張三豐', null, '1', '河南鄭州'); INSERT INTO `user` VALUES ('25', '陳小明', null, '1', '河南鄭州'); INSERT INTO `user` VALUES ('26', '王五', null, null, null);
新建Dynamic Web Project:
導包:
先把簡單的資料庫配置完成:
db.properties:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8 jdbc.username=root jdbc.password=12345
MyBatis的配置文件sqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="org.dreamtech.springmvc.pojo" /> </typeAliases> </configuration>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:property-placeholder location="classpath:db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:sqlMapConfig.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.dreamtech.mybatis.springmvc.dao" /> </bean> <!-- 註解事務 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 開啟註解 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc-mybatis</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="org.dreamtech"/> <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>
Controller:
package org.dreamtech.springmvc.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.dreamtech.springmvc.pojo.Items; import org.dreamtech.springmvc.service.ItemService; /** * 商品管理 */ @Controller public class ItemController { @Autowired private ItemService itemService; @RequestMapping(value = "/item/itemlist.action") public ModelAndView itemList(){ List<Items> list = itemService.selectItemsList(); ModelAndView mav = new ModelAndView(); mav.addObject("itemList", list); mav.setViewName("itemList"); return mav; } }
另外:RequestMapping註解的使用:
限定Get或Post方法:
@RequestMapping(value = "/deletes.action",method=RequestMethod.GET) public ModelAndView deletes(Integer[] ids) { ModelAndView mav = new ModelAndView(); mav.setViewName("success"); return mav; }
如果要想兩項都支持:
@RequestMapping(value = "/deletes.action", method = { RequestMethod.GET, RequestMethod.POST }) public ModelAndView deletes(Integer[] ids) { ModelAndView mav = new ModelAndView(); mav.setViewName("success"); return mav; }
當然,如果不寫的話:支持八大請求方式
如果當前Controller之下的所有URL都是同一個目錄下:
可以將它提出,給當前類一個RequestMapping(value="/xxx")註解
另外:RequestMapping的value是一個String[]類型,意味著可以多個URL交到一起處理
至於Service層的介面和實現類,Dao層MyBatis的Mapper動態代理,POJO類,頁面簡單的JSP代碼......
這些與SpringMVC無關,於是省略了,現在啟動項目
訪問:
http://localhost:8080/springmvc-mybatis1/item/itemlist.action
成功從資料庫讀出數據,完成了SpringMVC和MyBatis的整合