spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件

2024-06-24 01:18

本文主要是介绍spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这篇文章用来总结一下spring,springmvc,spring+mybatis,spring+hibernate的配置文件

1.web.xml

要使用spring,必须在web.xml中定义分发器等信息,基本的配置信息如下:

<?xml version="1.0" encoding= "UTF-8"?>
<web-app version= "3.0"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" ><!-- 配置spring分发器,spring表示对应的 servlet【名可以改】配置文件为spring-servlet.xml --><servlet ><servlet-name >spring </servlet-name ><servlet-class >org.springframework.web.servlet.DispatcherServlet </servlet-class ></servlet ><servlet-mapping ><!-- 会拦截.do请求--><servlet-name >spring </servlet-name ><url-pattern >*.do </url-pattern ></servlet-mapping >< display-name></display-name > < welcome-file-list><welcome-file >index.jsp </welcome-file ></ welcome-file-list>
</web-app>

2.spring配置文件

<!-- 该配置文件为spring的基本配置文件, springmvc,aop ,transaction等的配置均在此基础上进行 -->
<beans xmlns= "http://www.springframework.org/schema/beans"xmlns:context= "http://www.springframework.org/schema/context"xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd" ><!--  对com包下进行扫描,以完成对bean的创建和自动依赖注入 --><context:component-scan base-package ="com"/></beans>

3.springmvc配置文件

<!-- 该配置文件为 springmvc的基本配置文件 -->
<!-- 相比较spring,增加了 mvc的命名空间与注解驱动 -->
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" ><!--  对com包下进行扫描,以完成对bean的创建和自动依赖注入 --><context:component-scan base-package ="com"/><!--  mvc层提供的注解驱动[注册用于解析@ResponseBody注解的类]当controller中的方法需要返回 json数据时,需要用到@ResponseBody注解,此时需呀添加此驱动 --><mvc:annotation-driven /></beans>

4.springmvc整合hibernate

以下为springmvc+hibernate的配置文件,去掉mvc命名空间等配置即为spring+hibernate的配置文件

<!-- 该配置文件为 springmvc+hibernate 的基本配置文件 -->
<!-- 相比较springmvc,增加了hibernate 的配置 -->
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" ><!--  对com包下进行扫描,以完成对bean的创建和自动依赖注入 --><context:component-scan base-package ="com"/><!--  mvc层提供的注解驱动[注册用于解析@ResponseBody注解的类]当controller中的方法需要返回 json数据时,需要用到@ResponseBody注解,此时需呀添加此驱动 --><mvc:annotation-driven /><!-- 配置hibernate 开始 --><bean id ="ht" class= "org.springframework.orm.hibernate3.HibernateTemplate" ><!-- 指向session工厂 --><property name ="SessionFactory" ref= "sf"></property ></bean ><!-- 配置session工厂a setAnnotatedClasses(Class[] claes)指向映射实体bean列表每在工程中添加一个映射实体bean,就需要在list元素下添加一个value子元素指向该实体beanb setPackagesToScan(String package)扫描实体bean所在的包结构,在包下查找所有的映射实体--><bean name ="sf" class= "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" ><property name ="dataSource" ref="ds"></ property><!-- 映射实体bean 配置bean所在的包--><property name ="packagesToScan" value= "com.po,com.ngsh.bean"></property ><!-- 如果有多个包有映射实体,都在value中写,用逗号隔开 --><property name ="hibernateProperties"><props ><prop key= "hibernate.show_sql">true</prop ></props ></property ></bean ><!-- hibernate的数据源 --><bean id ="ds" class= "org.springframework.jdbc.datasource.DriverManagerDataSource" ><property name ="driverClassName" value= "com.mysql.jdbc.Driver"></property ><property name ="url" value= "jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8" ></property ><property name ="username" value="root"></ property><property name ="password" value="root"></ property></bean ></beans>

5.springmvc整合mybatis配置文件

去掉mvc的相关配置即为spring+mybatis的配置文件

<!-- 该配置文件为 springmvc+mybatis 的基本配置文件 -->
<!-- 相比较springmvc,增加了mybatis 的配置 -->
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" ><!--  对com包下进行扫描,以完成对bean的创建和自动依赖注入 --><context:component-scan base-package ="com"/><!--  mvc层提供的注解驱动[注册用于解析@ResponseBody注解的类]当controller中的方法需要返回 json数据时,需要用到@ResponseBody注解,此时需呀添加此驱动 --><mvc:annotation-driven /><!-- 配置mybatis 开始 --><!-- 在ioc容器中配置sqlSessionFactory --><bean id ="ssf" class= "org.mybatis.spring.SqlSessionFactoryBean" ><!-- 配置数据源 指向 ds --><property name ="dataSource" ref="ds"></property><!-- 配置映射文件 当有多个时 在list中添加--><property name ="mapperLocations"><list ><!-- classpath +映射文件的路径 --><value> classpath:com.dao.UserDao-mapper.xml</value ></list ></property ></bean ><!-- mybatis的数据源 --><bean id ="ds" class= "org.springframework.jdbc.datasource.DriverManagerDataSource" ><property name ="driverClassName" value= "com.mysql.jdbc.Driver"></property ><property name ="url" value= "jdbc:mysql://localhost:3306/demo" ></property ><property name ="username" value="root"></property><property name ="password" value="root"></property></bean ><!-- 配置mapper.xml所映射的接口,--><!-- 方法一 每增加一个接口类就得新增一个对应的bean进行注册 --><!-- <bean id ="userDao" class= "org.mybatis.spring.mapper.MapperFactoryBean" >指向sessionFactory<property name ="sqlSessionFactory" ref= "ssf"></property ><property name ="mapperInterface" value= "com.dao.UserDaoIf"></property >       </bean > --><!-- 方法二 直接扫描dao包 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <property name="basePackage" value="com.dao" />  <property name="sqlSessionFactoryBeanName" value="ssf"></property>  </bean>  
</beans>

6.mybatis的mapper文件的模板

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 声明命名空间即其所映射的接口 -->
<mapper namespace= "com.dao.UserDaoIf"><!-- parameterType指定参数类型,多个参数使用map resultMap指定结果集 --><select id ="selectById" parameterType="java.util.map"resultMap= "user">select * from user where name=#{name} and pw=#{pw };  </select ><!-- resultType表示返回int 型 --><select id ="selectUserCount" resultType= "java.lang.Integer">select count(*) from user;</select ><!-- 修改 --><update id ="uppw" parameterType="java.util.Map"  >update user set pw=#{pw} where id=#{id};</update ><delete id ="removeById" parameterType="java.lang.Integer">delete from user where id=#{id};</delete ><!-- 定义返回的结果集 使用select查询时可以使用resultType[返回类型如java.lang.String],也可以使用resultMap,但两者不可以同时使用,可定义多个,通过id区分--><resultMap type ="com.bean.User" id="user"><result property ="id" column="id"/><result property ="name" column="name"/><result property ="pw" column="pw"/></resultMap >
</mapper>

这篇关于spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1088786

相关文章

Java StringBuilder 实现原理全攻略

《JavaStringBuilder实现原理全攻略》StringBuilder是Java提供的可变字符序列类,位于java.lang包中,专门用于高效处理字符串的拼接和修改操作,本文给大家介绍Ja... 目录一、StringBuilder 基本概述核心特性二、StringBuilder 核心实现2.1 内部

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

Java中字符编码问题的解决方法详解

《Java中字符编码问题的解决方法详解》在日常Java开发中,字符编码问题是一个非常常见却又特别容易踩坑的地方,这篇文章就带你一步一步看清楚字符编码的来龙去脉,并结合可运行的代码,看看如何在Java项... 目录前言背景:为什么会出现编码问题常见场景分析控制台输出乱码文件读写乱码数据库存取乱码解决方案统一使

Java Stream流与使用操作指南

《JavaStream流与使用操作指南》Stream不是数据结构,而是一种高级的数据处理工具,允许你以声明式的方式处理数据集合,类似于SQL语句操作数据库,本文给大家介绍JavaStream流与使用... 目录一、什么是stream流二、创建stream流1.单列集合创建stream流2.双列集合创建str

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建