欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring集成MyBatis进行项目开发(一)

程序员文章站 2022-07-12 13:27:39
...

 

    下面将介绍使用spring+mybatis的开发样例:

    首先,笔者创建的是一个maven工程,在开发先先导入相关的依赖jar:

    pom.xml:

 

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<!-- spring3 相关依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<!-- spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<!-- mysql jdbc driver -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.13</version>
		</dependency>
		<!-- log 日志 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>
		<!-- mybatis-spring 插件 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.1</version>
		</dependency>
		<!-- mybatis依赖 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.1</version>
		</dependency>
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- 数据库连接池 -->
		<dependency>
			<groupId>proxool</groupId>
			<artifactId>proxool</artifactId>
			<version>0.9.1</version>
		</dependency>
		<dependency>
			<groupId>proxool</groupId>
			<artifactId>proxool-cglib</artifactId>
			<version>0.9.1</version>
		</dependency>
		<!-- servlet-jsp 依赖,为编译jsp时使用 -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	

 

   web.xml中对spring和数据库连接池的配置:

 

	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.xml</param-value>
	</context-param>
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- The front controller of this Spring Web application, responsible for 
		handling all application requests -->
	<servlet>
		<servlet-name>spring3</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>spring3</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- database proxool config -->
	<servlet>
		<servlet-name>ServletConfigurator</servlet-name>
		<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
		<init-param>
			<param-name>xmlFile</param-name>
			<param-value>WEB-INF/proxool.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

 

    在这里要注意,因为我使用了数据库连接池proxool,虽然已经设置servlet的启动级别是1,但是由于在servlet启动之前,spring(ContextLoaderListener监听器已启动了,所以在spring监听启动时它会报一个找不到对应的数据源的SQLException对于这个问题,本可以通过更改spring为servlet启动,并将启动级别设置为proxool配置加载之后解决:

 

 <servlet>
    <servlet-name>contextConfigLocation</servlet-name>
    <servlet-class>
             org.springframework.web.context.ContextLoaderServlet
    </servlet-class>
    <load-on-startup>2</load-on-startup>
 </servlet>

 

    但是因为spring3以后就不支持使用servlet启动了官方推荐使用listener启动applicationContext。所以这个方法在新版本不适合了,不过这个异常可以忽略,因为proxool在整个项目加载完成的时候的确以及完成了加载,所以在项目运行起来以后是不会报错的,开始报错是spring做的一个检查。

 

    为了解决乱码问题最好在web.xml中加上这个配置:

 

	<!-- character filter. use UTF8 -->
	<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>/*</url-pattern>
	</filter-mapping>

 

    下面是对spring3-servlet.xml的配置,它里面申明的bean都存放在webApplicationContext中:

<?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"
	default-lazy-init="false" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.pinche.statistic.web"></context:component-scan>
	<!-- 启用spring mvc注解 -->
	<mvc:annotation-driven />
	<!-- 静态资源访问问题 -->
	<mvc:default-servlet-handler />
	<!-- Default ViewResolver -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/page/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 对抛给spring的异常的处理 -->
	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">error</prop>
			</props>
		</property>
	</bean>
</beans>

 

    下面对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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!--
		数据源配置:这里配置了两个数据源dataSource_statistic和dataSource_pinche,
	分别注入到_sqlSessionFactory_statistic和_sqlSessionFactory_pinche
	-->
	<bean id="dataSource_statistic"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 数据库连接 -->
		<property name="url" value="proxool.statistic" /> 
		<!--
		这是不使用数据库连接池的配置方式,由于proxool只有在启动web应用的时候才能加载,
		所以如果在初期开发阶段可以用这种简单的数据库连接方式,在通过
		new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml")
		获取到context,并进行测试。
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.1.168:3306/statistic"/>
		<property name="username" value="statistic"></property>
		<property name="password" value="statistic"></property> 
		 -->
	</bean>
	<bean id="dataSource_pinche"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 数据库连接 -->
		<property name="url" value="proxool.pinche" /> 
	</bean>
	
	<!-- 为mybatis-spring注入数据源 -->
	<bean name="_sqlSessionFactory_statistic" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource_statistic"></property>
		<!-- 注入别名的包名前缀,这样就可以在mybatis的文件中直接写类名不用写全名了 -->
		<property name="typeAliasesPackage" value="com.pinche.statistic.domain" />
	</bean>
	<bean name="_sqlSessionFactory_pinche" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource_pinche"></property>
		<!-- 注入别名的包名前缀,这样就可以在mybatis的文件中直接写类名不用写全名了 -->
		<property name="typeAliasesPackage" value="com.pinche.statistic.domain" />
		<!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名,且在同一路径下,那么可以不配置该选项-->
		<!--<property name="mapperLocations" value="src/UserMapper.xml"/>-->
		<!--
			该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,
			编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的
			元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。
			因为这些都是使用Spring中的配置。当然如果你不打算添加typeAlias 之
			类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了
		-->
		<!-- <property name="configLocation" value=""/> -->
	</bean>
	<!--
		注册Mapper方式一
		<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
			<property name="mapperInterface" value="com.pinche.statistic.mapper.ApplicationsMapper"/> 
			<property name="sqlSessionFactory" ref="_sqlSessionFactory_statistic"/> 
		</bean>
	-->
	<!-- 注册Mapper方式二:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper ,配置如下:-->
	<!-- 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.pinche.statistic.mapper"></property>
	</bean>  
	-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.pinche.statistic.dialstatistic.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="_sqlSessionFactory_pinche"></property>
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.pinche.statistic.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="_sqlSessionFactory_statistic"></property>
	</bean> 
	
	<!-- 事务 -->
	<!-- 虽然mybatis也有自己的事务配置,但是配置了也没用,事务最终会交由spring控制,
	由于项目没有用到可以注掉。 
	-->
	<!-- <bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource_statistic" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/> -->
	
	<!-- 启用注解 -->
	<context:annotation-config />
	
	<!-- 自动扫描注入 -->
	<context:component-scan
		base-package="com.pinche.statistic.dao,
				com.pinche.statistic.service" />
</beans>

     通过以上的工作环境的配置完成,下一章继续介绍spring-mybatis集成开发的剩余内容。

     下一章  Spring集成MyBatis进行项目开发(二)