java+springmvc+vo,springmvc+mybatis的实例详解
前面講到:Spring+SpringMVC+MyBatis深入學習及搭建(十三)——SpringMVC入門程序(二)
1.需求
使用springmvc和mybatis完成商品列表查詢。
2.整合思路
springmvc+mybatis的系統架構:
第一步:整合dao層
mybatis和spring整合,通過spring管理mapper接口。
使用mapper的掃描器自動掃描mapper接口在spring中進行注冊。
第二步:整合service層
通過spring管理service接口。
使用配置方式將service接口配置在spring配置文件中。
實現事務控制。
第三步:整合springMvc
由于springmvc是spring的模塊,不需要整合。
3.環境準備
數據庫環境:mysql5.6
java環境:
jdk1.7
MyEclipse2014
springmvc版本:spring3.2
所需要的jar包:
數據庫驅動包
mybatis的jar包
mybatis的spring的整合包
log4j包
dbcp數據庫連接池包
spring3.2所有jar包
jstl包
過程結構目錄:
4.整合dao
mybatis和spring進行整合。
4.1 db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisdemo
jdbc.username=root
jdbc.password=
4.2 log4j.properties
# Global logging configuration,建議開發環境要用debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4.3 sqlMapConfig.xml
在classpath下創建mybatis/sqlMapConfig.xml。
<?xml version="1.0" encoding="UTF-8"?>/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
4.4 applicationContext-dao.xml
在classpath下創建spring/applicationContext-dao.xml。配置:數據源、事務管理、SqlSessionFactory、mapper掃描器。
4.5逆向工程生成po類及mapper(即單表增刪改查)
詳情見:Spring+SpringMVC+MyBatis深入學習及搭建(十)——MyBatis逆向工程
將生成的文件拷貝至工程中。
4.6手動定義商品查詢mapper
針對綜合查詢mapper,一般情況會有關聯查詢,建議自定義mapper。
4.6.1 ItemsMapperCustom.xml
sql語句:
SELECT * FROM items WHERE items.name LIKE '%筆記本%'
<?xml version="1.0" encoding="UTF-8" ?>items.name LIKE '%${itemsCustom.name}%'
resultType="joanna.yan.ssm.po.ItemsCustom">SELECT items.* FROM items
4.6.2 ItemsMapperCustom.java
public interface ItemsMapperCustom {//商品查詢列表public List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
5.整合service
讓spring管理service接口。
5.1定義service接口
package joanna.yan.ssm.service;import java.util.List;import joanna.yan.ssm.po.ItemsCustom;import joanna.yan.ssm.po.ItemsQueryVo;public interface ItemsService {//商品查詢列表public List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
package joanna.yan.ssm.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import joanna.yan.ssm.mapper.ItemsMapperCustom;import joanna.yan.ssm.po.ItemsCustom;import joanna.yan.ssm.po.ItemsQueryVo;import joanna.yan.ssm.service.ItemsService;public class ItemsServiceImpl implements ItemsService{
@Autowiredprivate ItemsMapperCustom itemsMapperCustom;
@Overridepublic List findItemsList(ItemsQueryVo itemsQueryVo)throws Exception {//通過ItemsMapperCustom查詢數據庫return itemsMapperCustom.findItemsList(itemsQueryVo);
}
}
5.2在spring容器配置service(applicationContext-service.xml)
在classpath下創建spring/applicationContext-service.xml,文件中配置service。
5.3事務控制(applicationContext-transaction.xml)
在classpath下創建spring/applicationContext-service.xml,文件中使用spring聲明式事務控制方法。
6.整合springmvc
6.1 springmvc.xml
在classpath下創建spring/springvc.xml文件,配置處理器映射器、適配器、視圖解析器。
6.2配置前端控制器
參考入門程序:Spring+SpringMVC+MyBatis深入學習及搭建(十二)——SpringMVC入門程序(一)
在web.xml中配置:
SpringMVC_MyBatis
index.jsp
springmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring/springmvc.xml1
springmvc*.action
6.3編寫Controller(就是Handler)
@Controllerpublic class ItemsController {
@Autowiredprivate ItemsService itemsService;//商品查詢http://localhost:8080/SpringMVC_MyBatis/queryItems.action@RequestMapping("/queryItems")public ModelAndView queryItems() throws Exception{//調用service查找數據庫,查詢商品列表List itemsList=itemsService.findItemsList(null); //返回ModelAndViewModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsList", itemsList);//指定視圖// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");//下邊的路徑,如果在視圖解析器中配置jsp路徑的前綴和jsp路徑的后綴,修改為modelAndView.setViewName("items/itemsList");return modelAndView;
}
}
6.4編寫jsp
查詢商品列表查詢條件:| 商品名稱 | 商品價格 | 生產日期 | 商品描述 | 操作 |
| ${item.name } | ${item.price } | ${item.detail } | 修改 |
7.加載spring容器
將mapper、service、controller加載到spring容器中。
建議使用通配符加載上邊的配置文件。
在web.xml中添加spring容器監聽器,加載spring容器。
contextConfigLocation/WEB-INF/classes/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
8.商品查詢調試
訪問地址:http://localhost:8080/SpringMVC_MyBatis/queryItems.action
查詢結果:
總結
以上是生活随笔為你收集整理的java+springmvc+vo,springmvc+mybatis的实例详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP能不能让一串代码现实,一段让你认清
- 下一篇: 个盘子的汉诺塔需要移动几步_看漫画学C+