一.MyBatis配置文件
首先,配置文件中有严格的顺序,如果顺序不一致则会报错。正确的配置顺序如下图
1.属性(properties):主要作用为读取外部配置文件,即数据库中的配置信息
<properties resource="db.properties"></properties>
2.常用的为autoMappingBehavior,自动完成字段的映射,更多详细配置信息见MyBatis的XML配置信息
3.类型别名(typeAliases):给类起别名,减少冗余性增加可读性,常见的有给实体类起别名和给包起别名两种方式
<typeAliases>
<typeAlias type="com.bjpowernode.pojo.Admin" alias="自定义别名"></typeAlias>
<package name="com.bjpowernode.pojo/>
</typeAliases>
给包起别名时默认为这个类的类名,首字母自动小写。
4.类型处理器(typeHandlers)、对象工厂(objectFactory):不太了解
5.插件(plugins):顾名思义,就是实现某种功能的插件,如下文中的分页查询
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
6.配置环境(environments):通过一段代码来详解配置中的各个关键点
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
默认的环境 ID(比如:default=“development”)。
每个 environment 元素定义的环境 ID(比如:id=“development”)。
事务管理器的配置(比如:type=“JDBC”)。
数据源的配置(比如:type=“POOLED”)
1.必须要保证默认环境要匹配其中一个环境ID。
2.数据源(dataSource):POOLED– 这种数据源的实现利用"池"的概念将 JDBC 连接对象组织起来,避免了创建新的连接实例时所必需的初始化和认证时间。 这是一种使得并发 Web 应用快速响应请求的流行处理方式。
映射器(mappers):定义SQL映射语句,最佳的方式是告诉 MyBatis 到哪里去找映射文件。可以使用相对于类路径的资源引用, 或完全限定资源定位符(包括 file:/// 的 URL),或类名和包名等。例如:
<mappers>
<!--注意要用/而不是.-->
<mapper resource="com/xxx/xxx/AdminMapper.xml"></mapper>
<!--使用这种映射方法时必须保证接口与Mapper文件同名-->
<mapper class="com.xxx.xxx.AdminMapper.xml"></mapper>
<!--通过url指定文件位置-->
<mapper url="file://....AdminMapper.xml"/>
<!--直接指定包自动扫描与第二种方法同理-->
<package name="com.xxx.xxx"/>
</mappers>
二.Spring配置文件
applicationContext-dao.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置mybatis工厂-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置mybatis的核心配置文件,指定全局配置文件的位置-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!--配置pojo,指定实体类的位置-->
<property name="typeAliasesPackage" value="com.bjpowernode.pojo"></property>
</bean>
<!--配置mapper扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bjpowernode.mapper"></property>
</bean>
</beans>
applicationContext-service.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置业务逻辑层,给spring扫描识别该包下的所有类,使用注解方式-->
<context:component-scan base-package="com.bjpowernode.service"></context:component-scan>
</beans>
springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描根包,实现自动注释驱动bean与自动注入 -->
<context:component-scan base-package="com.bjpowernode.controller"></context:component-scan>
<!--视图解析器-->
<bean id="ResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/admin/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--文件上传插件-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
web.xml配置文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app 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/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!--字符编码过滤器-->
<filter>
<filter-name>encode</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>encode</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--注册springmvc框架-->
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--spring框架注册-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
</web-app>