SpringMVC和Mybatis的整合 对单表的简单操作

springmvc详解(二):springmvc和mybatis整合 springmvc详解(二):springmvc和mybatis整合 目录 springmvc详解(二):springmvc和mybatis整合 一、环境准备 1.1、案例需求 1.2、导入jar包 1.3、工程结构 二、整合思路 三、三层整合 3.1、整合dao层 3.1.1、mybatis配置文件 3.1.2、dao层spring配置文件applicationContex... 阅读详情

A:导入需要的jar包
在这里插入图片描述
配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>SpringMVC_MyBatisDemo01</display-name>

	<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>DispatcherServlet</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>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	<!-- 配置使用application对象读取的参数信息 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/beans-*.xml</param-value>
	</context-param>
	<!-- 当tomcat服务器启动就会实例化application对象,当监听到application对象实例化后就初始化spring容器 ApplicationContext 
		act=new ClassPathXmlApplicatonContext("classpath:spring/beans-*.xml"); -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<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>
</web-app>

配置springmvc的xml文件 springmvc.xml(放在了src目录下)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.1.xsd
        
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    
	<context:component-scan base-package="com.xu"></context:component-scan>
	
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	   <property name="converters">
	      <set>
	        <!-- 注入自定义的日期类型转换器 -->
	        <bean class="com.xu.convertor.DateTypeConvertor"></bean>
	      </set>
	    </property>
	</bean>
        <!-- 配置类型转换器 -->
	   <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
	   <mvc:annotation-driven/>
</beans>
        

准备好连接数据库的资源文件和log4j文件,将它们放入了一个资源文件夹下
在这里插入图片描述

下面对单表进行操作
t_item表
在这里插入图片描述
将mybaits逆向工程生成的mapper和model文件夹复制到整合的项目中
添加两个扩展类

public class ItemCustom extends Item {

	
}

public class ItemQueryVo {

	  private ItemCustom itemCustom;

	public ItemCustom getItemCustom() {
		return itemCustom;
	}

	public void setItemCustom(ItemCustom itemCustom) {
		this.itemCustom = itemCustom;
	}
	  
}


添加Itemdao和Itemdao.xml

public interface ItemDao {

	List<ItemCustom> getItemList(ItemQueryVo itemQueryVo) throws Exception;
	
	
}

ItemDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xu.dao.ItemDao" >
  
    <sql id="item_where">
     <if test="itemCustom!=null">
        <if test="itemCustom.name!=null and itemCustom.name.length()>0">
          name like '%' #{itemCustom.name} '%'
        </if>
     </if>
  </sql>
  
     <select id="getItemList" parameterType="ItemQueryVo" resultType="ItemCustom"> 
         select * from t_item  
           <where>
                <include refid="item_where"></include>
           </where>
       </select>
  
</mapper>

对ItemDao进行测试

public class ItemDaoTest {
  private ItemDao itemDao;
	@Before
	public void setUp() throws Exception {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/beans-*.xml");
		this.itemDao=(ItemDao) applicationContext.getBean("itemDao");
	}
    @Test
	public void testGetItemList() throws Exception {
		List<ItemCustom> items= this.itemDao.getItemList(null);
		System.out.println(items);
	}

}

添加ItemService和ItemServiceBean

public interface ItemService {
          //獲得商品列表
	    List<ItemCustom> getItemList(ItemQueryVo itemQueryVo) throws Exception;
          //根據id查商品
        public ItemCustom findItemById(int itemId) throws Exception;
           //更新商品
       public boolean updateItem(ItemCustom itemCustom) throws Exception;
}

ItemServiceBean
@Service("itemService")
  public class ItemServiceBean implements ItemService {
	@Resource
    private ItemDao itemDao;
	@Resource
	private ItemMapper itemMapper;
	@Override
	public List<ItemCustom> getItemList(ItemQueryVo itemQueryVo) throws Exception {
		  return itemDao.getItemList(itemQueryVo);
	}
	@Override
	public ItemCustom findItemById(int itemId) throws Exception {
		 Item item=this.itemMapper.selectByPrimaryKey(itemId);
		 ItemCustom itemCustom=new ItemCustom();
		  //BeanUtils.copyProperties(item, itemCustom);这个方法可以完成对象复制,将第一个对象的内容复制到第二个对象当中
		 //将item对象的属性值复制到itemCustom对象的属性值中,类似itemCustom.setName(item.getName());
		 BeanUtils.copyProperties(item, itemCustom);
		 return itemCustom;
	}
	@Override
	public boolean updateItem(ItemCustom itemCustom) throws Exception {
		Item item=new Item();
		BeanUtils.copyProperties(itemCustom, item);
		int count=itemMapper.updateByPrimaryKeyWithBLOBs(item);
		return count>0;
	 }
 }


ItemServiceBeanTest 对ItemService进行测试
public class ItemServiceBeanTest {
    private ItemService itemService;
	@Before
	public void setUp() throws Exception {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/beans-*.xml");
		this.itemService=(ItemService) applicationContext.getBean("itemService");
	}
	@Test
	public void testGetItemList() throws Exception {
		  
		 ItemQueryVo itemQueryVo=new ItemQueryVo();
		 ItemCustom itemCustom=new ItemCustom();
		 itemCustom.setName("i");
		 itemQueryVo.setItemCustom(itemCustom);
		 List<ItemCustom> items=this.itemService.getItemList(itemQueryVo); 
		 System.out.println(items);
	 }
}

添加controller类 (Handler处理器)

@Controller
@RequestMapping("/item")
public class ItemController {

	@Resource
	private ItemService itemService;
	
	/**
	 * http://localhost:8080/SpringMVCAndMybatisDemo04/item/item_list.action
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/item_list.action")
	public ModelAndView findItems()throws Exception{
		ItemQueryVo itemQueryVo=new ItemQueryVo();
		ItemCustom itemCustom=new ItemCustom();
		itemCustom.setName("机");
		itemQueryVo.setItemCustom(itemCustom);
		List<ItemCustom> itemCustomList=itemService.findItemList(itemQueryVo);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("itemList", itemCustomList);
		modelAndView.setViewName("/WEB-INF/item/item_list.jsp");
		
		
		return modelAndView;
	}
	
 
          @RequestMapping("/findItemById.action")
          public String findItemById(HttpServletRequest request,HttpServletResponse response,int itemId) throws Exception{
        	   request.setCharacterEncoding("utf-8");  
        	   response.setContentType("text/html;charset=utf-8");
        	  ItemCustom itemCustom=this.itemService.findItemById(itemId);
        	  System.out.println("========点击修改跳转到更新页面:"+itemCustom.getName());
			  request.setAttribute("itemCustom", itemCustom);
        	  
        	  return "forward:/WEB-INF/item/item_update_input.jsp";
        	   
        	  
          }
          
          @RequestMapping("/updateItem.action")
          public String updateItem(HttpServletRequest request,HttpServletResponse response,ItemCustom itemCustom) throws Exception{
        	   request.setCharacterEncoding("utf-8");  
        	   response.setContentType("text/html;charset=utf-8");
        	  boolean bool= itemService.updateItem(itemCustom); 
        	  if(!bool){
        		  System.out.println("修改失败");
        		  return "forward:/WEB-INF/item_update_input.jsp";
        	  }
        	  System.out.println("======="+itemCustom);
        	  return "forward:item_list.action";
        	  
        	  
          }

}

日期转换类型的类

public class DateTypeConvertor  implements Converter<String,Date> {

	@Override
	public Date convert(String source) {
		try {
			return DateFormat.getDateInstance().parse(source);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

以上,SpringMVC和Mybatis整合之后,对单表的简单操作

SSM整合的小项目(简单单表CRUD操作) 标题Spring+SpingMVC+Mybatis的集成 1.创建项目 这个小项目是简单的单表的CRUD,没用到vo包,它只是继承了domian里面的类。 2.导包 log4j mybatis以及mysql驱动包 3.创建springmvc.xml spring+springmvc集成配置 <?xml version="1.0" encoding="UTF-8"?> &lt... 阅读详情

相关推荐

Java SpringMVC 和 MyBatis 整合关键配置详解

在 Java Web 开发中,整合 SpringMVC 和 MyBatis 框架可以构建高效、灵活且可维护的 Web 应用。配置数据源以连接数据库,确保 MyBatis 能够识别和加载 Mapper 接口。设置 SpringMVC 的核心组件,如控制器扫描和视图解析器。通过一个完整示例展示如何在 SpringMVC 中使用 MyBatis 进行数据库操作,包括实体类、Mapper 接口、Mapper XML 文件、Service 类和控制器的实现。开发者可以轻松整合这两个框架,构建功能强大的 Web 应用。

C_V_Better的博客 1490

Springmvc和mybatis整合

Springmvc和Mybatis整合

Ledis的博客 297

5.Spring+Mybatis+SpringMVC整合

前端时间搭建了一个关于新闻的后台,使用Restful风格,向前台提供json格式数据。 对于刚学习的时候会觉得整合三个框架是一件很难的事情,但是当自己都熟悉了之后再来整理下思路其实也没有那么难。 就是把三个模块整合运行起来,然后写上自己的业务逻辑就形成了一个项目。 今天把就思路整一下,加深自己的印象,首先谈一下这三个东西分别是什么。 spring  Spring是一个开源框架,Sprin

YingZiBo的博客 3270

SpringMVC和MyBatis的整合完成商品列表查询

该模型优点:架构清晰,有现成的MVC落地的框架供我们使用,而且对于业务逻辑相对简单的项目而言,开发效率非常高。整合过程:1、整合DAO层MyBatis和Spring整合,通过Spring管理Mapper接口通过Mapper的扫描器自动扫描mapper接口并在Spring中进行注册。2、整合Service层通过Spring管理Service接口。使用配置方式将service接口配置在Spring配置文件中。实现事务控制。(使用AOP控制事务)3、整合SpringMVC。

weixin_65544634的博客 666

SpringMVC Spring Mybatis整合步骤

一.配置mybatis   1.在pom.xml中导入mybatis3.4.5的依赖,导入mysql-connector-java-5.1.43      2.写mybatis-config-xml的配置文件,里面包含连接数据库的信息和映射文件   3.实体类和对应的映射文件(person,person.xml)   4.dao接口   5.写一个测试类测试mybatis是否可以使用

qq_40158032的博客 2828

FAST-LIO2实战:如何在无人机上部署并实现厘米级定位(附ROS配置全流程)

本文详细介绍了如何在无人机上部署FAST-LIO2激光雷达惯性里程计系统,以实现厘米级精确定位。内容涵盖从硬件选型(如激光雷达与机载计算机)、软件环境配置(ROS Noetic)、传感器标定,到FAST-LIO2参数调优、与PX4飞控集成以及实际飞行测试的全流程。重点解决了实时定位中的外参标定、数据同步等关键问题,为无人机在无GPS或复杂光照环境下提供稳定可靠的定位解决方案。

weixin_29062865的博客 249

SSM整合Mybatis、SpringMVC练习

高级参数绑定 数组类型 直接接收 条件:Input框中的name属性名和Controller方法中形参名一致 使用POJO包装对象 条件:Input框中的name属性名和Controller方法中形参上POJO包装对象中属性名一致 List集合类型 批量修改 只能POJO包装对象,在POJO包装对象中定义集合属性 条件:Input框中的name属性名和Controller方法上形参中POJO包装对象的集合属性名的每一个元素的属性名的属性...

woaini886353的博客 317

FPGA,关于安装使用libero IDE V9.2 及其gold floating license配置

3、license安装根据,下面的提示进行,跟随setting up the license server on a host教程操作。1、从官网下载Libero IDE v9.2(如下图中的Libero IDE v9.2 software)4、下载完成license解压包后还需要下载 Daemons license ,地址如下。(2)第一行并不是在内填写,而是直接用您电脑名替换掉(1)修改license.bat前四行是,其务必保持和案例位置相同(避免出错)............

JasonK的博客 4294

SpringMVC+MyBatis实现单表增删改查

SpringMVC+MyBatis实现单表增删改查 一. 创建Maven项目 Maven项目的创建及SpringMVC的配置:https://blog.youkuaiyun.com/qq_46062119/article/details/120373122?spm=1001.2014.3001.5501 加载完Maven之后,在pom.xml中<dependencies></dependencies>里引入所需依赖: 注:由于 Maven 的传递性,我们不必将所有需要的包全部配置依赖,而是配置最

qq_46062119的博客 2986

[Unity Demo]从零开始制作空洞骑士Hollow Knight第四集:制作更多的敌人

大家好久不见,终于放完中秋假期可以回自己家里打代码了,上一期我介绍了为什么和如何导入2D toolkit以及制作一个完整的敌人(就是那只蚊子包括了精灵,动画,状态机行为以及脚本)现在不妨趁热打铁制作更多的敌人。

dangoxiba的博客 1972

向华为学习——解读158页 华为敏捷园区网技术解决方案【附全文阅读】

华为敏捷园区网解决方案通过策略随行和体验随身技术,满足移动办公场景下的网络需求。该方案采用敏捷交换机和防火墙自动执行策略配置,实现用户身份认证与业务策略的无缝衔接,且策略配置与IP地址解耦,简化了管理流程。相比传统技术,该方案有效解决了用户分级、QoS保障、无线接入等关键问题,确保用户在移动过程中获得一致的网络体验。

cdfunlove的博客 379

SpringMVC快速入门第四讲——Spring、MyBatis和SpringMVC的整合

之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBatis框架的学习(六)——MyBatis整合Spring。本文我再来讲SpringMVC和MyBatis整合开发的方法,这样的话,Spring、MyBatis和SpringMVC三大框架的整合开发我们就学会了。这里我使用的Spring是Spring4.1.3这个版本(SpringMVC自然也是这个版本),MyBatis是

李阿昀的博客 9332

Vue3 - Element Plus 全局修改更换主题色配置详细教程,element-plus自定义主题色全局替换主色调强力解决方案,支持动态修改主题颜色/动态皮肤,将默认蓝色主题色更换为其他颜色!

vue3,nuxt3,element-plus,组件库,主题色,修改主题颜色,更换主题色,如何修改组件默认主题蓝色,项目主题色定制,修改失败,主题色覆盖不了,:root直接修改,hover颜色没改,自定义主题色,vue3全局设置elementPlus主题色最新详细修改教程,怎么修改全局的颜色,scss变量,覆盖原来的主题色改为自己的颜色,统一替换,有没有好办法,颜色不变,失败,无效,动态主题切换,主题颜色怎么在项目里直接改,plus主题色更换,覆盖默认主题色,最强最好用,设置主题色,换肤,皮肤,动态更换

🏆 前端领域优质创作者 1万+

springboot整合springmvc+mybatis+thymeleaf完成单表的crud操作

springboot整合springmvc+mybatis+thymeleaf完成单表的crud操作。 1、创建项目(12-spring-boot-springmvc-mybatis) 2、pom.xml <dependencies> <!-- springBoot 的启动器 --> <dependency> <groupId&...

dh_sheep的博客 786

训练YOLOv11 | Pytorch和Pycharm的安装教程适用W11系统(附详细的系统变量添加过程)

根据你之前下载的版本选择对应的选项即可,然后复制底下的绿色代码到Anaconda的命令行输入即可(这里要在你安装的python环境里进行安装,例如我的环境名字是PyTorch,那么就是activate PyTorch里进行安装)然后同之前一样会加载一会,然后会询问我们是否下载配件,我们输入Y等待即可(下载过程中我们要确保网络通常如果有波动可能会下载失败)。类似于我的电脑是Windows11选择上面红框内的选项即可点击后即下载,(注意这里下载的是zip文件)下载完成后复制下面几个文件。

Snu77的博客 1万+

OpenClaw安卓手机攻略

想在安卓手机上安装 OpenClaw,目前主要有两种主流方法:一种是使用专门的,操作简单直观;另一种是在命令行环境下进行,自由度更高。你可以根据自己的偏好和动手能力来选择。

suny8的专栏 2899

Spring、SpringMVC、Mybatis 整合

Spring、SpringMVC、Mybatis 整合 文章目录Spring、SpringMVC、Mybatis 整合1、Pom.xml配置1.1 Maven依赖的导入1.2 静态资源导出配置2、Spring和Mybatis整合2.1 创建applicationContext.xml2.2 创建mybatis-config.xml2.3 创建database.properties2.4 创建spring-dao.xml3 SpringMVC层3.1 配置web.xml3.2 创建spring-mvc.xml

xiaoxiao66668的博客 1467

Java SpringMVC与MyBatis整合

本文介绍了如何在 Java Web 开发中整合 SpringMVC 和 MyBatis 框架,以构建高效、可维护的 Web 应用。首先,搭建了基本的 Maven 项目结构,包括控制器、服务层、数据访问层和模型等模块。接着,通过 pom.xml 文件添加了 SpringMVC、MyBatis 和数据库驱动等依赖。随后,配置了数据源、MyBatis 工厂和事务管理器等关键组件,并通过 XML 文件进行 Spring 和 SpringMVC 的配置。文章通过一个简单的用户管理示例,详细展示了如何将这两个框架整合

C_V_Better的博客 1141

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Mybatis使用之SpringMVC整合

摘要: 主要记录Mybatis+SpringMVC最新版整合、补充部分有事务不起作用的原因说明。

Oscar Chen 3465

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【SpringMVC】SpringMVC整合Mybatis

pom.xml 项目目录 3.整合步骤 1.dao层 目录结构 mybatis配置文件 spring配置文件 配置: 逆向工程生成po类及mapper(dao层) 方法参见 [《Mybatis Generator超详细配置》](…/Mybatis/Mybatis Generator超详细配置.md)针对综合查询mapper,一般情况会有关联查询,建议自定义mapperItemsMapperCustom.xml ItemsMapperCustom.java po类 输入pojo的包装类

Robin的博客 1416

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Spring整合SpringMVC和Mybatis框架

搞定算法,面试字节再不怕,有需要文章中分享的这些二叉树、链表、字符串、栈和队列等等各大面试高频知识点及解析最后再分享一份终极手撕架构的大礼包(学习笔记):分布式+微服务+开源框架+性能优化《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!外链图片转存中…(img-tJgoNF5L-1713426181509)][外链图片转存中…(img-sBdecCO2-1713426181509)]

2401_83703866的博客 1021

springmvc和mybatis整合

使用springmvc和mybatis完成商品列表查询。 springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口。 使用mapper的扫描器自动扫描mapper接口在spring中进行注册。 第二步:整合service层 通过spring管理 ...

nonpricklycactus的博客 446

springmvc和mybatis的整合

学过springmvc和m它们mybatis的配置之后,整合它们就很简单了。 springMVC的配置     mybatis的配置   首先,我们springmvc的配置文件是springmvc-config.xml,mybatis的配置文件是mybatis.xml. 我们要知道整合这两个框架所能达到的效果是什么? 我们使用mybatis的时候对数据库的操作是通过**mappe

果子狸要好好活着 530

mybatis和springMVC整合

需求 使用springmvc和mybatis完成商品列表查询。 整合思路 springMVC和mybatis的系统架构 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口。 使用mapper的扫描器自动扫描mapper接口在spring中进行注册。 第二步:整合service层 通过spring管理 service接口。 使用配置方式将service接口...

weixin_43361158的博客 373
上一篇: SpringMVC入门 注解的和非注解的处理器映射器和处理器适配器
下一篇: 生产者-消费者限定请求 -笔记
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值