zoukankan      html  css  js  c++  java
  • 《Mybatis从入门到精通》读书笔记(四)

    第九章. Spring集成Mybaits

    MyBatis-Spring可以帮助我们将Mybaits代码无缝整合到Spring中。使用这个类库中的类,Spring将会加载必要的Mybaits工厂类和Session类。这个类库也提供了一个简单的方式将Mybaits数据映射器和SqlSession注入到业务层的bean中,而且也可以处理事务,翻译Mybaits的异常到Spring的DataAccessException数据访问异常中。

    applicationContext.xml
    
    <bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <array>
                <value>classpath:tk/mybatis/**/mapper/*.xml</value>
            </array>
        </property>
        <property name="typeAliasesPackage" value="tk.mybatis.web.model"/>
    </bean>
    <!-- 这个配置不支持Ant风格的路径,当需要配置多个包路径时可以使用分号或逗号进行分隔。-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="addToConfig" value="true"/>
        <!-- 可以使用分号和逗号作为分隔符设置多余一个的包路,每个映射器将会在指定的包路径中递归的被搜索到。-->
        <property name="basePackage" value="tk.mybatis.**.mapper"/>
        <!-- 用于过滤被扫描的接口,如果设置了该属性,那么Mybatis的接口中只有包含该注解才会被扫描进去。-->
        <!--<property name="annotationClass" value=""/>-->
    </bean>
    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">
    <configuration>
    	<settings>
            <setting name="logImpl" value="LOG4J"/>
            <setting name="cacheEnabled" value="true"/>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <setting name="aggressiveLazyLoading" value="false"/>
    	</settings>
    </configuration>
    
    
    
    spring-framework-bom是Spring的一个项目清单文件,由于集成Spring时需要添加很多Spring组件的依赖,为了避免使用不同版本的组件导致意外情况发生,可以使用spring-framework-bom。添加spring-framework-bom后,在使用Spring依赖时就不需要再配置每个依赖的版本号了,Spring组件的版本由spring-framework-bom统一管理。这不仅可以避免版本混乱引起的问题。还可以很方便的升级Spring的版本。
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.3.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    具体参加mybatis-spring项目!

    第十章. Spring Boot集成Mybaits

    1. pom.xml:

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.mybatis.spring.boot</groupId>
    	<artifactId>mybatis-spring-boot-starter</artifactId>
    	<version>1.2.0</version>
    </dependency>
    <dependency>
    	<groupId>tk.mybatis</groupId>
    	<artifactId>simple</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    </dependency>

    2. 配置Spring Boot主启动类扫描Mybatis的Mapper接口(通过@MapperScan注解)

    @SpringBootApplication默认自动扫描其所在类的同级包以及子包(所以建议主启动类放在最外层的包名下)。为了让@SpringBootApplication扫描到simple-0.0.1-SNAPSHOT.jar中的mapper接口,需要借助@MapperScan注解来显示指定需要扫描的mapper包路径。

    @SpringBootApplication
    @MapperScan({"tk.mybatis.springboot.mapper", "tk.mybatis.simple.mapper"})
    public class Application implements CommandLineRunner {
        // 省略
    }

    3. 配置Spring Boot扫描Mybatis的xml映射文件

    mybatis.mapper-locations=classpath:mapper/*.xml,classpath*:tk/**/mapper/*.xml

    注:为了扫描到jar包中的xml映射文件,需要采用classpath*才能检索到!

    4. mybatis其他配置选项

    Mybatis Starter提供的所有可配置的属性都在org.mybatis.spring.boot.autoconfigure.MybatisProperties类中,该类部分代码如下:
    @ConfigurationProperties(
        prefix = "mybatis"
    )
    public class MybatisProperties {
        public static final String MYBATIS_PREFIX = "mybatis";
        private String configLocation;
        private String[] mapperLocations;
        private String typeAliasesPackage;
        // 省略部分
        @NestedConfigurationProperty
        private Configuration configuration;
        // 省略部分

    Spring Boot可以通过@ConfigurationProperties注解自动将配置文件中的属性组装到对象上。这个注解一般都需要配置与属性匹配的前缀,此处前缀为"mybatis",因此对Mybatis的配置都是以"mybatis."作为前缀的。属性类中的字段如果是驼峰形式的,在配置文件中进行配置时建议改为横杠(-)和小写字母连接的形式,虽然Spring Boot仍然能正确匹配驼峰形式的属性。但是支持Spring Boot的IDE在自动提示时会使用标准的形式。例如configLocation在配置时应改写成mybatis.config-location的形式。

    MybatisProperties并没有把所有的属性都列举出来,但是这个类提供了一个嵌套的Configuration属性,通过这种方式可以直接对Configuration对象进行属性配置。例如settings中的属性可以按照下面的方式进行配置。

    mybatis.configuration.lazy-loading-enabled=true
    mybatis.configuration.aggressive-lazy-loading=true

    基本上大部分的配置都可以通过这种形式去实现,如果遇到不会配置的内容,仍然可以通过mybatis-config.xml方式去配置Mybatis,然后在Spring Boot配置中指定该文件的路径即可,示例如下:

    mybatis.config-location=classpath:mybaits-config.xml
  • 相关阅读:
    【python】一个文件内容写入另一个
    【Linux】批量修改权限
    【Git】git add git commit
    赌博游戏
    输出斐波那契数列前20项,每输出5个数换行
    Java线程的几种可用状态
    Java创建线程的方式
    Java虚拟机
    判断对象oStringObject是否为String
    throw跟throws关键字
  • 原文地址:https://www.cnblogs.com/codestarer/p/13635557.html
Copyright © 2011-2022 走看看