spring boot和mybatis入门
[size=x-large]昨天讲了一下spring boot的入门操作相信老手已经明白入门的操作,今天我来讲下我自己的心得,可能与官方有一定差异;希望对大家能有用
一:开门见山首先看工程结构

这里的包有点多大家就先把mybatis有用的先建好
二:接下里就是pom的文件代码
spring的依赖在我的前一个博文里面有这里就只给出mybatis的依赖

三:开始开发先还是看我的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和分页插件在下一个博文讲
备注:本人也是新人很多东西写的可能不是很清楚希望大家谅解,下部有我的源码 希望大家参考
[/size]
spring boot和mybatis入门的更多相关文章
- spring boot(一):入门篇
构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- 使用intelliJ创建 spring boot + gradle + mybatis站点
Spring boot作为快速入门是不错的选择,现在似乎没有看到大家写过spring boot + gradle + mybatis在intellij下的入门文章,碰巧.Net同事问到,我想我也可以写 ...
- (Spring Boot框架)快速入门
Spring Boot 系列文章推荐 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Boot 静态资源处理 今天介绍一 ...
- spring boot + gradle + mybatis
使用intelliJ创建 spring boot + gradle + mybatis站点 Spring boot作为快速入门是不错的选择,现在似乎没有看到大家写过spring boot + gr ...
- Spring Boot:快速入门教程
什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
- Spring Boot【快速入门】简单案例
Spring Boot[快速入门] Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point ...
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
随机推荐
- redis.conf配置详解(转)
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位,# 通常的格式就是 1k 5gb 4m 等酱紫:## 1k => 1000 bytes# 1kb =&g ...
- 15-算法训练 P1103
http://lx.lanqiao.cn/problem.page?gpid=T372 算法训练 P1103 时间限制:1.0s 内存限制:256.0MB 编程实现两个复数的运算 ...
- python学习-(__new__方法和单例模式)
class Dog(object): __instance = None __init_flag = False def __new__(cls, name): if cls.__instance = ...
- No appenders could be found for logger
在运行代码时,出现下面的错误, log4j:WARN No appenders could be found for logger (genericTest.GenericTest). log4j:W ...
- Struts2把数据封装到集合中之封装到map中
struts框架封装数据可以封装到集合中也可以封装到map中,该篇博客主要讲解将数据封装到map中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) 2. 需求:页面中有可 ...
- [z]spring boot gradle build
I had the same problem. I believe it is caused by the JRE that gradle is configured to use rather th ...
- python 网络基础 之 网络协议
osi 七层协议 互联网协议按照功能不同分为osi七层或者tcp/ip 五层或tcp/ip 四层 tcp/ip 四层 1.应用层 2.传输层 3.网络层 4.网络接口层 tcp/ip 五层 1.应用层 ...
- u-boot之怎么实现分区
启动参数bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0中kernel在哪定义,为什么可以直接引用?针对这个问题展开思考最终定位到 ...
- POJ3694 Network - Tarjan + 并查集
Description 给定$N$个点和 $M$条边的无向联通图, 有$Q$ 次操作, 连接两个点的边, 问每次操作后的图中有几个桥 Solution 首先Tarjan找出边双联通分量, 每个双联通分 ...
- JeeSite 4.0
http://jeesite.com/ JeeSite 是一个 Java EE 企业级快速开发平台,基于经典技术组合(Spring Boot.Spring MVC.Apache Shiro.MyBat ...