- 配置sqlMapConfig核心配置文件和Mapper映射文件
sqlMapConfig.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>
<!--加载jdbc.properties文件-->
<properties resource="jdbc.properties"></properties>
<!--自定义别名-->
<typeAliases>
<!--<typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
<package name="com.itheima.domain"/>
</typeAliases>
<!--配置数据源的环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<!--<mapper resource="com.itheima.mapper/AccountMapper.xml"></mapper>-->
<package name="com.itheima.mapper"/>
</mappers>
</configuration>
AccountMapper.xml: 定义sql语句:参数和返回值的类型需要使用到核心配置文件中定义的别名
<?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">
<!--此映射文件主要配置sql语句-->
<mapper namespace="com.itheima.mapper.AccountMapper">
<select id="findAll" resultType="account">
select * from account
</select>
<select id="save" parameterType="account">
insert into account values(${id},${name},${money})
</select>
</mapper>
- 配置spring和spring-mvc配置文件
- 在applicationContext.xml中配置spring的组件扫描,扫描service和mapper,controller在spring-mvc中进行扫描,因此排除掉
<?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
">
<!--组件扫描,扫描service和mapper-->
<context:component-scan base-package="com.itheima">
<!--排除controller,spring-mvc去扫描controller-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
- 配置spring-mvc.xml
扫描controller 配置注解驱动 配置视图解析器 开放静态资源访问权限
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--引入一下mvc的命名空间xmlns:mvc="http://www.springframework.org/schema/mvc"
和http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd-->
<!--组件扫描,扫描Controller-->
<context:component-scan base-package="com.itheima.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器,确定一下前后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--静态资源权限开放-->
<mvc:default-servlet-handler/>
</beans>
- 配置web.xml,主要配置内容如下:
监听器 springmvc的前端控制器 乱码过滤器
<!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>
<display-name>Archetype Created Web Application</display-name>
<!--监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!--ContextLoaderListener内部需要加载配置文件applicationContext.xml,因此上方得配置一个全局参数contextConfigLocation-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--需要加载springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--服务器启动时使得springmvc的前端控制器一起启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
<!--/是缺省形式,代表任何访问资源都进入springmvc的框架-->
</servlet-mapping>
<!--乱码过滤器-->
<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>
mybatis整合spring实现:将sqlSessionFactory的实现交给spring 将sqlMapConfig.xml修改为sqlMapConfig-spring.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>
<!--自定义别名-->
<typeAliases>
<!--<typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
<package name="com.itheima.domain"/>
</typeAliases>
</configuration>
将数据库的配置信息和sqlSession工厂的创建权交给applicationContext.xml,并扫描一下mapper的信息,方便service层直接注入mapper
<?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
">
<!--组件扫描,扫描service和mapper-->
<context:component-scan base-package="com.itheima">
<!--排除controller,spring-mvc去扫描controller-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--记载properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--加载核心配置文件(整合后的sqlMapConfig-spring.xml)-->
<property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
</bean>
<!--扫描mapper所在的包 为mapper创建实现类,可在Service层直接注入mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"></property>
</bean>
</beans>
Service层就可以直接注入accountMapper调用方法,而不再创建sqlSessionFactory
|