spring和mybatis整合

第一步 添加依赖

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
 </dependency>

第二步 在Spring中管理SqlSessionFactory

<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 设置mybatis核心配置文件路径(可选) -->    
<property name="configLocation" value="classpath:/mybatis-config.xml" />
 <!-- 配置mybatis xml映射文件位置(如果Mapper是用注解配置的,这里就不用设置此属性了) -->   
<property name="mapperLocations" value="classpath:/mappers/*" />
</bean>

如果是注解的方式配置的Mapper,我们需要在Spring配置文件中添加mybatis的schema以支持mybatis注解扫描

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

<!-- 配置基于注解的Mapper所在包的路径 -->
  <mybatis:scan base-package="org.mybatis.spring.sample.mapper" />


</beans>

或者这样配置

@Configuration
@MapperScan(“org.mybatis.spring.sample.mapper”)
public class AppConfig {
// …
}
第三步 用Spring管理事务

和单独使用Spring时一样, 配置Spring的声明式事务就可以了,mybatis会自动参与到spring的事务中

示例
数据源

jdbc.url=jdbc:mysql://localhost:3306/tushu?characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
        http://mybatis.org/schema/mybatis-spring
        http://mybatis.org/schema/mybatis-spring.xsd">

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <context:component-scan base-package="com.lanou3g.spring"/>

    <mybatis:scan base-package="com.lanou3g.spring.mapper" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:/mapper/*"/>
    </bean>

</beans>

service层

@Service
public class MessageServiceImpl {

    @Autowired
    private MessageMapper messageMapper;

    public List<Message> queryAll() {
        return messageMapper.selectByExample(null);
    }

}

使用入口

public class Application {
    public static void main(String[] args) throws SQLException {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        MessageServiceImpl messageService = ctx.getBean(MessageServiceImpl.class);
        List<Message> messageList = messageService.queryAll();
        log.info("" + messageList);
    }

}

数据库对应的类用(genarator)自动生成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值