java代码配置 mybatis_使用JAVA代码来整合spring和mybatis配置文件

非常讨厌使用xml配置spring和mybati的配置文件.故尝试使用JAVA代码来编写spring和mybati的整合配置文件

很开心搭建成功,JAVA配置代码如下

package com.spring.study;

import org.apache.ibatis.datasource.pooled.PooledDataSource;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.FileSystemResource;

import org.springframework.core.io.Resource;

@Configuration //标明为spring的配置类

@ComponentScan //开启spring的自动扫描bean和管理bean

@MapperScan("com.spring.study.dao")//开启mybatis的自动扫描mapper接口和管理

public class SpringConfig {

//数据源

@Bean

public PooledDataSource dataSource() {

PooledDataSource dataSource = new PooledDataSource();

dataSource.setDriver("com.mysql.jdbc.Driver");

dataSource.setUrl("jdbc:mysql://192.168.0.23:3306/lalala?useUnicode=true&characterEncoding=UTF-8");

dataSource.setUsername("shanhao");

dataSource.setPassword("23232322");

return dataSource;

}

@Bean

public SqlSessionFactoryBean sqlSessionFactory() {

SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();

sqlSessionFactory.setDataSource(dataSource());

FileSystemResource fileSystemResource = new FileSystemResource("C:\\mapping\\InstrumentMapper.xml");

sqlSessionFactory.setMapperLocations(new Resource[]{fileSystemResource});

return sqlSessionFactory;

}

}

Resource我用的是绝对路径的,一般可以用相对路径的Resource比较好一点,我懒得改,开发的时候注意一下

把FileSystemResource 换成解析相对路径的类就好了,有空我找下重新贴下

以防万一,顺便贴上目录,防止有些童鞋迷失

3f313d8cc0ff56cb91f47e7aeb3322cb.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值