配置Mybatis--SpringMVC-Mybatis-Maven项目整合(三)

来源:互联网 发布:mac office字体不能用 编辑:程序博客网 时间:2024/06/01 13:56

呵呵。有几天没写博客了。

今天先接着上次项目整合来写。

上次说到了怎么配置控制层。

当我们配好了控制层,接着当然是配置我们的数据层了。

而我们的数据层,就是由我们的Mybatis来构成的。

上一次刚说完怎么来配置controller层,今天来看看怎么配置Mybatis。

Mybatis的配置,写在src下面的applicationContext.xml文件里面。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- 数据库连接池 -->
    <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
        <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
        <propertyname="url"value="jdbc:mysql://localhost:3306/test"/>
        <propertyname="username"value="root"/>
        <propertyname="password"value=""/>
    </bean>
 
    <!-- transaction manager, use JtaTransactionManager for global tx -->
    <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <propertyname="dataSource"ref="dataSource"/>
    </bean>
 
    <!-- enable transaction demarcation with annotations -->
    <tx:annotation-driven/>
 
    <!-- define the SqlSessionFactory -->
    <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
        <propertyname="dataSource"ref="dataSource"/>
        <propertyname="typeAliasesPackage"value="me.idashu.attg.domain"/>
    </bean>
 
    <!-- scan for mappers and let them be autowired -->
    <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <propertyname="basePackage"value="me.idashu.attg.persistence"/>
    </bean>

首先配的,当然是我们的数据可连接池。也就是dataSource,这里用的mysql数据库。

接下来下面几行,配置了transaction manager,也就是事务管理器。这里了事务是用注解的方式配置的。

然后sqlSessionFactory,就是我们mybatis的配置,忘记叫了叫什么了。。。me.idashu.attg.domain就是说,我们的domain在这个包的下面。

最后这里:

?
1
2
3
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <propertyname="basePackage"value="me.idashu.attg.persistence"/>
</bean>

就是说,我们的mybatis的mapper,是写在me.idashu.attg.persistence这个包的下面,而且,这些mapper会自动注入。

就这样,mybatis的配置就完成了。

0 0