问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

spring-boot怎么配置web.xml中的内容

发布网友 发布时间:2022-04-25 15:13

我来回答

2个回答

懂视网 时间:2022-04-10 05:26

<?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.hxyz</groupId> 7 <artifactId>media</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>media</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.4.0.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <mybatis.version>3.2.2</mybatis.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>mysql</groupId> 35 <artifactId>mysql-connector-java</artifactId> 36 </dependency> 37 <!-- mybatis --> 38 <dependency> 39 <groupId>org.mybatis</groupId> 40 <artifactId>mybatis</artifactId> 41 <version>${mybatis.version}</version> 42 </dependency> 43 <dependency> 44 <groupId>org.mybatis</groupId> 45 <artifactId>mybatis-spring</artifactId> 46 <version>1.2.0</version> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-starter</artifactId> 52 </dependency> 53 <dependency> 54 <groupId>org.springframework.data</groupId> 55 <artifactId>spring-data-commons</artifactId> 56 </dependency> 57 <!-- connection pool --> 58 <dependency> 59 <groupId>org.springframework</groupId> 60 <artifactId>spring-jdbc</artifactId> 61 </dependency> 62 <dependency> 63 <groupId>org.apache.tomcat</groupId> 64 <artifactId>tomcat-jdbc</artifactId> 65 </dependency> 66 <dependency> 67 <groupId>mysql</groupId> 68 <artifactId>mysql-connector-java</artifactId> 69 </dependency> 70 <dependency> 71 <groupId>com.fasterxml.jackson.core</groupId> 72 <artifactId>jackson-databind</artifactId> 73 <version>2.7.0</version> 74 </dependency> 75 <dependency> 76 <groupId>net.sf.json-lib</groupId> 77 <artifactId>json-lib</artifactId> 78 <version>2.4</version> 79 <classifier>jdk15</classifier> 80 </dependency> 81 <dependency> 82 <groupId>com.fasterxml.jackson.module</groupId> 83 <artifactId>jackson-module-jaxb-annotations</artifactId> 84 <version>2.7.0</version> 85 </dependency> 86 <dependency> 87 <groupId>net.sf.json-lib</groupId> 88 <artifactId>json-lib</artifactId> 89 <version>2.4</version> 90 <classifier>jdk15</classifier> 91 </dependency> 92 <dependency> 93 <groupId>dom4j</groupId> 94 <artifactId>dom4j</artifactId> 95 <version>1.1</version> 96 </dependency> 97 <dependency> 98 <groupId>org.apache.commons</groupId> 99 <artifactId>commons-lang3</artifactId> 100 <version>3.1</version> 101 </dependency> 102 <dependency> 103 <groupId>org.codehaus.jackson</groupId> 104 <artifactId>jackson-mapper-asl</artifactId> 105 <version>1.9.13</version> 106 </dependency> 107 <dependency> 108 <groupId>org.codehaus.jackson</groupId> 109 <artifactId>jackson-core-asl</artifactId> 110 <version>1.9.13</version> 111 </dependency> 112 </dependencies> 113 114 <build> 115 <plugins> 116 <plugin> 117 <groupId>org.springframework.boot</groupId> 118 <artifactId>spring-boot-maven-plugin</artifactId> 119 </plugin> 120 </plugins> 121 </build> 122 123 124 </project>

2.通过MyBatis-generate自动生成实体类和mapper

技术分享

3.配置XML文件

在resource下面新建applicationContext.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:aop="http://www.springframework.org/schema/aop" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:jee="http://www.springframework.org/schema/jee" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xsi:schemaLocation=" 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
 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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 
 <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
 <!-- Connection Info -->
 <property name="driverClassName" value="${jdbc.driver}" />
 <property name="url" value="${jdbc.url}" />
 <property name="username" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />

 <!-- Connection Pooling Info -->
 <property name="maxActive" value="10" />
 <property name="maxIdle" value="50" />
 <property name="minIdle" value="0" />
 <property name="defaultAutoCommit" value="false" />
 </bean>
 <!-- MyBatis配置 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
 <property name="typeAliasesPackage" value="com.hxyz.media.entity" />
 <!-- 显式指定Mapper文件位置 -->
 <property name="mapperLocations" value="classpath:/mapper/*Mapper.xml" />
 </bean>
 <!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.hxyz.media.mapper" />
 </bean>
</beans> 

在resource路径下新建application.properties

1 server.port=8089
2 jdbc.driver=com.mysql.jdbc.Driver
3 jdbc.url=jdbc:mysql://localhost:3306/
4 jdbc.username=jack
5 jdbc.password=123456

至此,我们的xml文件已经配置好了,我们可以写一个简单的控制器来测试下数据库是否已经连通

4.测试是否连通

1 public interface FactoryService {
2 
3 public List<FactoryEntity> findFactory();
4 
5 }
@Service
public class FactoryServiceImpl implements FactoryService{
 @Resource
 private FactoryEntityMapper factoryMaaper;

 @Override
 public List<FactoryEntity> findFactory() {
 FactoryEntityExample example=new FactoryEntityExample();
 Criteria createCriteria = example.createCriteria();
 List<FactoryEntity> selectByExample = factoryMaaper.selectByExample(example);
 return selectByExample;
 }

}
 1 @RestController
 2 @RequestMapping(value="front/factory")
 3 public class FactoryController {
 4  @Resource
 5 private FactoryService factoryService;
 6 
 7 @RequestMapping(value="list")
 8  @ResponseBody
 9 public ResponseVo queryAll(){
10  ResponseVo responseVo=new ResponseVo();
11  List<FactoryEntity> factoryEntityList = factoryService.findFactory();
12  responseVo.setData(factoryEntityList);
13  return responseVo;
14  
15  }
16 
17 
18 }

所以,我们猜一下,现在我请求下,数据会正常的返回么

我们现在请求下

http://localhost:8089/front/factory/list

返回的是

 1 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hxyz.media.mapper.FactoryEntityMapper] found for dependency [com.hxyz.media.mapper.FactoryEntityMapper]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
 2 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 3 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 4 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 5 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:518) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 6 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 7 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 8 at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 9 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
10 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
11 ... 34 common frames omitted

没有找到FactoryEntityMapper,但是我们明明是在xml中配置了mapperScannerConfigue

这是因为springboot在启动后并不会主动的去读取xml文件,只会根据类文件间的依赖去加载spring的bean类,所以springboot并没有发现xml中的配置

我们可以在启动类中添加

@ImportResource(locations = "classpath*:/applicationContext.xml")

完整的启动类如下

1 @ImportResource(locations = "classpath*:/applicationContext.xml")
2 @SpringBootApplication
3 //@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class})
4 public class MediaApplication {
5 
6 public static void main(String[] args) {
7  SpringApplication.run(MediaApplication.class, args);
8  }
9 }

这时我们再启动就可以返回数据了

 

 

SpringBoot填坑系列---XML方式配置数据库

标签:tco   default   mysq   rom   sql   jdb   osi   连通   driver   

热心网友 时间:2022-04-10 02:34

Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xml。
当需要载入多个spring相关的配置文件时,首先加载ContextLoaderListener类,再指定context-param中指定多个spring配置文件,使用逗号分别隔开各个文件。为了使用方便可以将配置文件进行MVC式的分解,配置控制器Bean的配置文件放置在一个xml文件中,server的Bean放在service.xml文件中。
springboot前端模板?

选择main\webapp\WEB-INF目录,在后面添加上web.xml,点击ok web.xml内容如下: 添加?org.apache.tomcat.embed的tomcat-embed-jasper依赖包和javax.servlet的jstl依赖包 配置tomcat端口(默认值为8080),可以选择不配置 spring.mvc.view.prefix和spring.mvc.view.suffix是SpringBoot约定的视图前缀和后缀的配置,意思是找到/...

springboot项目模板(springboot项目结构讲解)

填写你的maven项目的groupId和artifact等项目基本信息之后,在依赖中选择Web--Web之后finish。 目录结构如下: 直接运行DemoApplication.java就可以运行项目了。 你也可以使用官方模板快速开始SpringBoot项目 接下来我们分析一下pom.xml中的依赖。 SpringBoot中还有一个很重要的文件:application.properties/yml/yaml文件。 项...

用springboot做一个网站(如何用springboot搭建一个web工程)_百度...

Helloworld使用传统的springmvc,需要配置web.xml,applicationContext.xml,然后打包为war在tomcat中运行,而如果使用springboot,一切都变得简单起来了。下面使用Maven来创建springboot的webapp工程pom.xml4.0.0org.springframeworkgs-spring-boot0.1.0org.springframework.bootspring-boot-starter-parent1.3.3.RELEASEorg.springfr...

springboot怎么用(springboot怎么用在web上又用在微信小程序上)_百 ...

首先,我们设置由springboot应用程序引导的RSocketserver服务器。因为有spring-boot-starter-rsocketdependency依赖,所以springboot会自动配置RSocketserver。跟平常一样,可以用属性驱动的方式修改RSocketserver默认配置值。例如:通过增加如下配置在application.properties中,来修改RSocket端口 也可以根据需要进一步修改服务器的其他属...

使用SpringBoot之后web.xml去哪儿了

Spring Boot的出现进一步消除了web.xml的需要。它提倡内嵌Servlet容器,或者通过继承SpringBootServletInitializer简化部署过程。Spring Boot通过利用Servlet 3.0的ServletContainerInitializer接口,实现了与web.xml类似的注册功能,无需显式地在web.xml中编写相关配置。它在启动时自动扫描Spring-web包下的Spring...

SpringBoot创建多模块方式以及打包方式

使用maven命令启动项目时,需要确保在Web模块的pom.xml文件中正确配置主类信息。执行mvn spring-boot:run命令启动项目,可能遇到找不到主类或依赖缺失的问题。解决方法是退回到根目录,执行mvn -Dmaven.test.skip -U clean install命令安装依赖,然后再次进入Web模块目录下执行mvn spring-boot:run命令。遇...

springboot配置Period(springboot配置跨域请求)

SpringBoot的默认配置文件是什么?1、SpringBoot不需要添加复杂的web.xml或者spring.xml等配置文件,spring只有一个配置文件,也非必须的。默认的配置文件是application.properties。2、创建SpringBoot项目默认会在src/main/resources目录下生成一个全局配置文件application.properties。全局配置文件的作用是对一些默认...

springboot配置信息(springboot配置jndi)

在SpringBoot框架中,推荐使用配置类的方式向容器中添加和配置组件 在SpringBoot框架中,通常使用@Configuration注解定义一个配置类,SpringBoot会自动扫描和识别配置类,从而替换传统Spring框架中的XML配置文件。 当定义一个配置类后,还需要在类中的方法上使用@Bean注解进行组件配置,将方法的返回对象注入到Spring容器中,并且...

mavenspringboot(mavenspringboot插件)

mavenspringboot转成dynamic步骤如下:第一步,选择projectfaces,设置dynamicweb版本、jdk版本、runtime环境 如果projectfacets无法转换为dynamicweb3.0,则根据以下两个步骤进行操作 1、修改项目中的web.xml文件内容2、修改项目的.setting目录中的org.eclipse.wst.common.project.facet.core.xml文件,将jst....

(二)springboot之spring-boot-starter-web

springboot版本:3.0.2 通过查看spring-boot-starter-web依赖文件的源码,我们可以发现其依赖的jar包包括以下内容:spring-boot-starter-web依赖启动器的主要功能是为Web开发提供所有必要的底层依赖。因此,在pom.xml文件中引入spring-boot-starter-web依赖启动器之后,我们就可以直接进行Web场景的开发,无需...

springboot redis spring boot配置文件 springboot引入配置文件 springboot指定配置文件 springboot配置文件加载 springboot刷新配置文件 springboot json spring boot web spring boot
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
橱柜pet和双饰面板区别 pet橱柜是什么意思? 我本科毕业在铁路工程局工作两年,可以报考注册电气工程师初级考试吗... 我是学化学合成的你工作,没节能方面的知识,适不适合做节能评估工作 求一圣骑士竞技场和战场的加奶天赋(61点) 现在WOW3.13圣骑士的PVP天赋改如何+才厉害? 魔兽世界中骑士在竞技场中怎么配天赋好啊? 魔兽世界竞技场圣骑士哪个天赋强力 全屋软水机选哪些品牌好? 抖音们是如何毁掉我们的 spring boot框架的web.xml文件怎么配置 如何在docker中部署springboot的web应用 怎么创建spring boot web 工程 mysql 用什么数据类型表示价格 MySQL里存储图片的是什么数据类型? mysql 数据类型 给我介绍下智能手机的概念和功能吧~ 智能手机的概念是怎么样的?怎么样的手机才能称得上是智能机? 怎样拟作文标题及如何写好开头 怎样给你的作文拟个漂亮的题目教案 作文题目怎么拟 作文题目怎么写好 如何拟好高考作文的标题 手机的由来? 概念手机的概念是指什么? 高考语文:怎么起好作文标题 作文小标题的拟写方法 怎么给作文取一个好题目 如何拟好作文标题 如何拟一个出彩的作文标题 初中生如何写好作文题目 “spring boot”框架的“web.xml”文件怎么配置? 如何部署同一个Spring boot web 应用到不同的环境 spring boot怎么获取web路径 springboot项目调用webservice客户端 问题 springboot 跟spring web项目兼容 springboot开发webservice怎么发布 spring boot web应用为什么刷新页面没用 spring boot 为什么要用webmvcconfigurationsupport,是干什么用的? 如何修改基于Spring Boot开发的Web应用的端口 晚上做梦梦见和男朋友的爸妈见面是好现象吗?男朋友爸妈现在反对我们 spring boot 发布外部tomcat 没有web.xml怎么部署 妞:可爱界扛把子的!繁体字翻译 成真在中国泡妞界厉害吗 梦见我和男朋友的父母无意中见面,男友却很冷淡 梦见去男朋友家,他爸妈对我很不好,这是怎么了? 建行信用贷款怎么办理 工伤认定书有效期多久 工伤认定有效期是多久 工伤认定有效期和期限 工伤认定的有效期是多久