spring boot和mybatis入门

[size=x-large]昨天讲了一下spring boot的入门操作相信老手已经明白入门的操作,今天我来讲下我自己的心得,可能与官方有一定差异;希望对大家能有用
一:开门见山首先看工程结构
[img]http://dl2.iteye.com/upload/attachment/0119/4361/e13277df-9ce6-31be-9851-0bcc8aea3603.png[/img]
这里的包有点多大家就先把mybatis有用的先建好

二:接下里就是pom的文件代码
spring的依赖在我的前一个博文里面有这里就只给出mybatis的依赖

[img]http://dl2.iteye.com/upload/attachment/0119/4368/53d5b608-4a49-3fca-bbeb-9f7dcbba1237.png[/img]

三:开始开发先还是看我的App吧
@EnableConfigurationProperties
@SpringBootApplication
//加载一些资源文件
@PropertySources({@PropertySource("classpath:/config/app/app-common.properties"),
@PropertySource("classpath:/config/app/app-${spring.profiles.active}.properties")})
public class App {
private static final Logger logger = LoggerFactory.getLogger(App.class);

public static void main(String[] args) {
if (System.getProperty("spring.profiles.active") == null) {
//把测试配置文件进入进来
System.setProperty("spring.profiles.active", "dev");
}
logger
.info("已启动############################################################################################");
SpringApplication.run(App.class, args);
}
}


四:myabatis和mapper的java配置
mapper配置:
@Configuration
public class MapperConfig {

@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer configure = new MapperScannerConfigurer();
// 扫描mapper的包
configure.setBasePackage("com.lx.mapper");
configure.setMarkerInterface(BaseMapper.class);
return configure;
}

}
mybatis的配置:
@Configuration
public class MybatisConfig {

// 注入配置
@Autowired
private DataSourceConfig dataSourceConfig;

@Bean
@InitBinder
public DataSource dataSource() {
// 引用阿里连接池
DruidDataSource ds = new DruidDataSource();
//dataSourceConfig.getUrl()引入一些配置文件信息注入到DataSource
ds.setUrl(dataSourceConfig.getUrl());
ds.setUsername(dataSourceConfig.getUsername());
ds.setPassword(dataSourceConfig.getPassword());
ds.setInitialSize(dataSourceConfig.getInitConnSize());
ds.setMinIdle(dataSourceConfig.getMinIdleConnSize());
ds.setMaxActive(dataSourceConfig.getMaxActiveConnSize());
ds.setTimeBetweenEvictionRunsMillis(60000);
ds.setDriverClassName(dataSourceConfig.getDriver());
ds.setMaxWait(60000);
ds.setMinEvictableIdleTimeMillis(300000);
ds.setTestOnBorrow(false);
ds.setTestOnReturn(false);
ds.setTestWhileIdle(true);
ds.setValidationQuery("SELECT 'x'");
ds.setPoolPreparedStatements(false);
return ds;
}

// spring 事务
@Bean(name = "txManager")
public DataSourceTransactionManager dataSourceTransactionManager() {
DataSourceTransactionManager txManager = new DataSourceTransactionManager();
txManager.setDataSource(dataSource());
return txManager;
}

@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(
// 扫描mybatis的配置文件
"/config/mybatis/mybatis-config.xml"));
Resource[] mapperResources = new PathMatchingResourcePatternResolver()
// 扫描mapper
.getResources("classpath*:/config/mybatis/mapper/*.xml");
sqlSessionFactoryBean.setMapperLocations(mapperResources);
return sqlSessionFactoryBean;
}


对了忘给连接池和数据库依赖了:
<!-- database -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.15</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>


现在配置好了 基本就能运行起来了 不报错就成功了接下来就是写crud运用到tk mapper和分页插件在下一个博文讲
[color=red]备注:本人也是新人很多东西写的可能不是很清楚希望大家谅解,下部有我的源码 希望大家参考[/color]

[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值