首页 > 代码库 > spring + mybatis 整合

spring + mybatis 整合

1.先说说依赖类库

技术分享
 <dependencies>       <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.1.1</version>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.1.1</version>        </dependency>        <dependency>            <groupId>commons-dbcp</groupId>            <artifactId>commons-dbcp</artifactId>            <version>1.4</version>        </dependency>        <dependency>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>            <version>1.1.1</version>        </dependency>           <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-core</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-web</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-context</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-aop</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-jdbc</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-beans</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-expression</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-orm</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-tx</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-test</artifactId>                <version>${spring-version}</version>            </dependency>            <dependency>                <groupId>log4j</groupId>                <artifactId>log4j</artifactId>                <version>1.2.17</version>            </dependency>            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>3.8.1</version>            </dependency>  </dependencies>
View Code
maven 3.x profiles配置
<profiles>
<profile>
<id>dev</id>
<properties>
<spring-version>3.1.1.RELEASE</spring-version>
</properties>
</profile>

<profile>
<id>pro</id>
<properties>
<spring-version>3.1.1.RELEASE</spring-version>
</properties>
</profile>
</profiles>

这里特别注意spring的版本,保持项目中spring版本的一致,否则会出现spring容器初始化失败等问题。

2.重要的一些配置

2.1这里主要配置sqlSession和事物管理器,通过sqlSession可以直接操作数据库。

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="mapperLocations" value="classpath*:/sqlMaps/*.xml"/>        <property name="dataSource" ref="dataSource"/>    </bean>    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">        <constructor-arg index="0" ref="sqlSessionFactory" />    </bean>    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>

2.2 另一种方式,通过mybatis中提供的

SqlSessionDaoSupport,这里需要注入sqlSessionFactory,其他DAO对象只要继承或包含baseDao即可。
    <bean id="baseDao" class="net.idalang.dao.impl.BaseDao">        <property name="sqlSessionFactory" ref="sqlSessionFactory" />    </bean>

2.3 另外,对于一些数据操作逻辑不是很复杂的数据操作,直接使用mybatis-generator即可生成基本增删改查方法,提高效率。

2.3.1 引入插件

           <plugin>                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.3.2</version>                <configuration>                    <verbose>true</verbose>                    <overwrite>true</overwrite>                </configuration>            </plugin>

2.3.2 简单配置

在classpath下创建generatorConfig.xml,关键配置:数据库驱动的位置,数据库连接的相关配置,对那些表进行table--》object,其他一些转换约定,如表前缀,对象命名等。

以下为sqlite数据库的一个简单配置:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration>    <classPathEntry location="your_maven_repo_path\org\xerial\sqlite-jdbc\3.7.15-M1\sqlite-jdbc-3.7.15-M1.jar"/>    <context id="MysqlTables" targetRuntime="MyBatis3">        <commentGenerator>            <property name="suppressAllComments" value="true" />        </commentGenerator>        <jdbcConnection driverClass="org.sqlite.JDBC" connectionURL="jdbc:sqlite:your_DB_PATH" userId="id" password="pwd">        </jdbcConnection>        <javaTypeResolver>            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <javaModelGenerator targetPackage="your_model_target_packages" targetProject="src\main\java">            <property name="enableSubPackages" value="true"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <sqlMapGenerator targetPackage="sqlMaps" targetProject="src\main\resources">            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <javaClientGenerator type="XMLMAPPER" targetPackage="your_mapper_target_package" targetProject="src\main\java">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <table schema="idalang" tableName="news" domainObjectName="News" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">            <property name="useActualColumnNames" value="true"/>        </table>    </context></generatorConfiguration>

 

参考资源:

1.这一个连接可以解决相关的所有问题 

http://mybatis.github.io/spring/zh/

 

          

  

spring + mybatis 整合