欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

java+springmvc+vo,springmvc+mybatis的实例详解

发布时间:2023/12/2 c/c++ 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的实例详解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。