xml文件配置问题

xml文件配置问题
1,web.xml:
javaweb项目会有此配置文件。表示当前web应用的核心配置文件。
注意web项目版本3.0不会产生web.xml. 2.5版本可以

Servlet在web.xml中的配置
在通过Eclipse创建Servlet时,会自动在web.xml文件中进行Servlet相关信息的配置
(注意:如果是复制类文件,配置信息不会配置,需要自己手动配置!)

 <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.tedu.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>

关于上面的配置信息:
a) Eclipse每创建一个Servlet, 就会在web.xml文件中添加两个标签:和标签(可以将这两个标签看成一个组的标签)
b) 和标签内都会有一个标签,标签的内容可以更改,但要求更改后的这两个标签的内容也必须一致。
c) 标签用于配置Servlet类的全路径名(即包名+类名)
需要注意:如果在创建Servlet后修改了Servlet类的名称,这个地方也要一起更改,否则将会出现"ClassNotFoundException" 即类找不到异常
d) 标签用于配置浏览器以什么路径访问当前Servlet(即Servlet对外访问的路径),默认的路径是:/类名
例如:上面为HelloServlet配置的为 /HelloServlet,因此我们在浏览器中的访问路径则为:
http://主机名/web项目访问路径/HelloServlet

在web.xml中配置springmvc

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
	<!-- 配置springmvc前端控制器, 将所有请求交给springmvc来处理 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		
		<!-- 配置springmvc核心配置文件的位置,默认Springmvc的配置文件是在WEB-INF目录下,默认的名字为springmvc-servlet.xml,如果要放在其他目录,则需要指定如下配置:
		-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-config.xml</param-value>
		</init-param>		
	</servlet>
<!-- 其中的斜杠(/)表示拦截所有请求(除JSP以外), 所有请求都要经过springmvc前端控制器 -->
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

2,server.xml:
Tomcat中的server.xml在软件安装的conf/目录下,其中可以修改端口号port=”8080”----->port=”80”,修改完后重启服务器生效。这样访问时就不需要加端口号8080了。

3,setting.xml:
Maven中的setting.xml在软件安装的conf/目录下,其中可修改maven仓库的路径,修改maven仓库的路径。

配置该目录后,以后通过maven下载的jar包将会保存在配置的目录下。

注意:在项目开发时,可能没有D盘等盘,所以需要设置如下:
${user.home}/.m3/repo
即:用户目录下m3文件夹下repo

配置镜像仓库(阿里):

<mirror>
		<id>aliyun</id>
		<name>aliyun Maven</name>
		<mirrorOf>*</mirrorOf>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

配置JDK版本:

<profile>
		<id>jdk-1.8</id>
		<activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
		</activation>
		<properties>
		 <maven.compiler.source>1.8</maven.compiler.source>
		 <maven.compiler.target>1.8</maven.compiler.target>
		 <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
		</properties>
    </profile>

4,pom.xml:
maven项目的核心配置文件,文件中通过坐标来管理项目中的所有jar包和插件。
在pom.xml文件中配置了所依赖的jar包或者插件后,Maven会根据文件中的配置信息到本地仓库中寻找指定的jar包或者插件,如果本地仓库中没有就会到私服或者中央仓库中下载相应的jar包或者插件。再引入到项目中使用!

5,mybatis-config.xml
(1)mybatis-config.xml是Mybatis的核心配置文件,通过其中的配置可以生成SqlSessionFactory,也就是SqlSession工厂
(2)基于SqlSessionFactory可以生成SqlSession对象
(3)SqlSession是一个既可以发送SQL去执行,并返回结果,类似于JDBC中的Connection对象,也是Mybatis中至关重要的一个对象。
(4)Executor是SqlSession底层的对象,用于执行SQL语句
(5)MapperStatement对象也是SqlSession底层的对象,用于接收输入映射(SQL语句中的参数),以及做输出映射(即将SQL查询的结果映射成相应的结果)

例:mybatis-config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
<!-- MyBatis的全局配置文件 -->
<configuration >
	<!-- 配置环境,可配置多个环境(比如:develop开发、test测试) -->
	<environments default="develop">
		<environment id="develop">
			
			<!-- 配置事务管理方式:JDBC/MANAGED
			JDBC:将事务交给JDBC管理(推荐)
			MANAGED:自己管理事务
			  -->
			<transactionManager type="JDBC"></transactionManager>
			
			<!-- 配置数据源,即连接池 JNDI/POOLED/UNPOOLED
				JNDI:已过时
				POOLED:使用连接池(推荐)
				UNPOOLED:不使用连接池
			 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/yonghedb?characterEncoding=utf-8"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- 引入Mapper配置文件,可以配置多个 -->
	<mappers>
		<mapper resource="EmpMapper.xml"/>
	</mappers>

</configuration>

configuration是根标签,当前文件中所有的配置都在该标签内,注意其中配置的关键点:
默认的环境 ID(比如:default=“develop”)。
每个 environment 元素定义的环境 ID(比如:id=“develop”)。
事务管理器的配置(比如:type=“JDBC”)。
数据源的配置(比如:type=“POOLED”)。
(1)environments标签:该标签内部可以配置多个environment,即多种环境,每种环境可以做不同配置或连接不同数据库。例如,开发、测试、生产环境可能需要不同的配置,连接的数据库可能也不相同,因此我们可以配置三个environment,分别对应上面三种不同的环境。
但是要记住,environment可以配置多个,但是最终要使用的只能是其中一个!

(2)environment标签:内部可以配置多种配置信息,下面介绍事务管理配置和数据源配置。
(3)transactionManage标签:事务管理配置,mybatis中有两种事务管理方式,也就是 type="[JDBC|MANAGED]。
JDBC:这个配置就是直接使用了 JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务范围。推荐使用。
MANAGED:这个配置几乎没做什么。它从来不提交或回滚一个连接。需要自己手动添加并管理。不推荐使用。
(4)dataSource标签:数据源,也就是连接池配置。这里type指定数据源类型,有三种内建的类型:JNDI、POOLED、UNPOOLED
JNDI:已过时,不推荐使用!
POOLED:使用连接池,mybatis会创建连接池,并从连接池中获取连接访问数据库,在操作完成后,将会把连接返回连接池。
UNPOOLED:不使用连接池,该方式适用于只有小规模数量并发用户的简单应用程序上。
(5)mappers标签:用于导入mapper文件的位置,其中可以配置多个mapper,即可以导入多个mapper文件。

6.XxxMapper.xml:
实体类的映射文件.
Mybatis是将SQL配置在mapper文件中,修改SQL只是修改配置文件,类不需要重新编译。

例:EmpMapper.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">

<!-- 
	namespace一般指定为当前文件的所在包路径+文件名
	将来在程序中通过[ namespace + id ]定位到执行哪一条SQL语句
 -->
<mapper namespace="EmpMapper">
	<!-- 通过select、insert、update、delete标签声明要执行的SQL -->
	<select id="findAll" resultType="com.tedu.pojo.Emp">
		select * from emp
	</select>
	<!-- 
	resultType:返回值类型,简单类型(例如:Integer,String,Emp等)
		如果返回集合(List<Emp>),只需配置集合中的元素类型即可!
	resultMap:复杂对象结构(例如多表关联查询等),后面用到再讲解
	 -->
</mapper>

(1)第1行是xml的文档声明,用于声明xml的版本和编码
(2)第2、3、4行,引入了xml约束文档,当前xml文档将会按照mybatis-3-mapper.dtd文件所要求的规则进行书写。
(3)Mapper标签:根标签,其中namespace(名称空间,也叫命名空间),要求不能重复。其实就是一个名称,一般我们指定为"包名+文件名"。
(4)select标签:用于指定将来要执行的各种SQL语句。标签上可以声明属性,下面介绍常用的属性:id、resultType、resultMap
id属性:要求值不能重复。将来在执行SQL时,可以通过namespace + id找到指定SQL并执行。
resultType属性:从这条SQL语句中返回所期望类型的类的完全限定名称(包名+类名)。注意如果是集合情形,那应该是集合可以包含的类型,而不能是集合本身。
简而言之,resultType控制查询SQL执行后返回值的类型或集合中的泛型,例如查询emp表中的单条记录,返回值是一个Emp对象,因此,resultType=“com.tedu.pojo.Emp”;
如果查询emp表中的多条记录,返回值是一个List,此时resultType的值应该集合中的泛型,因此resultType=“com.tedu.pojo.Emp”;
resultMap属性:复杂对象结构(例如多表关联查询等)。 使用 resultType 或 resultMap,但不能同时使用。

7.jdbc.properties:
在开发中,通常我们会将连接数据库的配置信息单独放在一个properties文件中(方便管理和维护), 然后在MyBatis的mapper文件中引入properties文件的配置信息即可!

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yonghedb?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

在mybatis-config.xml文件中引入jdbc.properties文件

1、其中 标签用于引入jdbc.properties文件,默认到classpath即类目录下寻找指定的文件;
2、properties标签上value属性中配置的 ${jdbc.xxx}:
${jdbc.driver}:其实就是jdbc.properties文件中的 jdbc.driver的值,即:
com.mysql.jdbc.Driver
${jdbc.url}:其实就是jdbc.properties文件中的 jdbc.url的值,即:
jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=utf-8
${jdbc.username}:其实就是jdbc.properties文件中的 jdbc.username的值,即:
root
${jdbc.password}:其实就是jdbc.properties文件中的 jdbc.password的值,即:
root
8,applicationContext.xml:
spring核心配置文件

在applicationContext.xml中添加如下内容(下面的配置不需要掌握,只是一个文件格式):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

</beans>

例:将Hello对象的创建交给Spring容器来管理,在核心配置文件中添加如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 声明bean对象,id是唯一标志,class:bean对象的全路径 -->
	<bean id="hello" class="com.tedu.spring.Hello"></bean>

</beans>

为什么用单实例或多实例?
之所以用单实例,在没有线程安全问题的前提下,没必要每个请求都创建一个对象,这样子既浪费CPU又浪费内存;
之所以用多例,是为了防止并发问题;即一个请求改变了对象的状态(例如,可改变的成员变量),此时对象又处理另一个请求,而之前请求对对象状态的改变导致了对象对另一个请求做了错误的处理;
用单例和多例的标准只有一个:当对象含有可改变的状态时(更精确的说就是在实际应用中该状态会改变),使用多实例,否则单实例;

在Spring中配置Bean实例是单例还是多例方法是:
单例:

<bean id="hello" scope="singleton" class="com.tedu.spring.Hello"></bean>

多例:

<bean id="hello" scope="prototype" class="com.tedu.spring.Hello"></bean>

9,Spring Context:
Spring上下文,是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。

10,springmvc-config.xml
springmvc核心配置文件

创建并配置springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
			http://www.springframework.org/schema/mvc/spring-mvc-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">
	
	<!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) -->
	<mvc:default-servlet-handler/>
	
	<!-- 2.配置注解驱动,用于识别注解(比如@Controller) -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类,
		如果扫描到的类上有 @Controller、@Service、@Component等注解,
		将会自动将类注册为bean 
	 -->
	<context:component-scan base-package="com.tedu.controller">
	</context:component-scan>
	
	<!-- 4.配置内部资源视图解析器
		prefix:配置路径前缀
		suffix:配置文件后缀
	 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	
</beans>

11, application.yml:
application.properties:
二者皆为Spring Boot的配置文件。
Spring Boot使用了一个全局的配置文件application.properties,放在src/main/resources目录下或者类路径的/config下。Sping Boot的全局配置文件的作用是对一些默认配置的配置值进行修改。
springboot官方推荐使用application.yml配置文件,yml文件的好处,天然的树状结构,一目了然。在yml文件中冒号后面要空一格再写值,不过idea会自动空格。
.yml文件时树状结构,层级浅时比较方便,层级深的时候就比较麻烦
application.properties文件时属性访问结构,层级深浅对它来说是一样的,而且相较于.yml类型的文件比较好配置,但缺点也很明显–要重复写很多遍父级属性;
二者对比:
.properties

#spring datasource:HiKariCP
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql:///goods?useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#spring myatis
mybatis.configuration.default-statement-timeout=30
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:/mapper/sys/*.xml
management.endpoints.web.exposure.include=*
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
#spring log
logging.level.com.cy=DEBUG

application.yml:

#server
server:
  port: 80
  servlet:
    context-path: /
  tomcat:
    max-threads: 1000

#spring
spring:
  datasource:
    url: jdbc:mysql:///jtsys?serverTimezone=GMT
    username: root
    password: root
  thymeleaf:
    prefix: classpath:/templates/pages/
    suffix: .html

#mybatis
mybatis:
  configuration:
    default-statement-timeout: 30
    map-underscore-to-camel-case: true
  mapper-locations:
  - classpath:/mapper/sys/*.xml

#lOG
logging:
  level:
    com.cy: DEBUG
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值