spring boot学习笔记(一)
来源:互联网 发布:linux date命令 编辑:程序博客网 时间:2024/06/05 04:26
spring boot的优点
配置简单!!!配置简单!!!配置简单!!!
1,可以创建独立的spring应用程序。
2,嵌入tomcat jetty 容器,不需要部署war包。
3,简化maven配置。
4,根据项目依赖自动配置spring框架
5,没有xml配置文件
spring boot的注解
- @RestController
注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者=@Controller+@Responsbody - @RequestMapping
注解提供路由信息,告诉Spring任何来自”/”路径的HTTP请求都应该被映射到 home 方法 - @Configuration
加上该注解,等价于在XML中配置beans 用@Bean注解等价于xml配置bean - @EnableAutoConfiguration
告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring,spring boot 建议只含有一个带有该注解的类 - @CompoentScan
常使用 @ComponentScan 注解搜索beans,并结合 @Autowired 构造器注入。 - @SpringBootApplication
=@Configuration + @EnableAutoConfiguration + @CompoentScan - @vlaue
在初始化时很方便的对Bean的入参变量进行赋值。还有一种写法:${some.proptery.key}”,即将property的key值写在花括号中。
@Value("${mybatis.configLocation}") private String configLocation;
spring boot 集成mybatis
- 使用mybatis官方提供的mybatis-spring-boot-starter
在pom.xml加依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.0.0</version></dependency>
在application.yml中添加配置:
mybatis: mapperLocations: classpath:mappers/*.xml typeAliasesPackage: com.pinganfu.dayu.dal.domain.auto basePackage: com.pinganfu.dayu.dal.mapper configLocation: mybatis-config.xml
或在.properties中增加配置:
mybatis.mapperLocations=classpath*:mappers/**/*.xmlmybatis.typeAliasesPackage=com.pinganfu.dayu.dal.domainmybatis.configuration.map-underscore-to-camel-case=truepagehelper.offset-as-page-num=true
- mybatis-spring方式
需要加mybatis mybatis-spring 依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency>
然后编写MybatisConfig配置类
@Configuration@EnableTransactionManagementpublic class MyBatisConfig implements TransactionManagementConfigurer { @Value("${mybatis.configLocation}") private String configLocation; @Value("${mybatis.typeAliasesPackage}") private String typeAliasesPackage; @Value("${mybatis.mapperLocations}") private String mapperLocations; @Autowired DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage(typeAliasesPackage); //bean.setPlugins(new Interceptor[] { pageHelper() }); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setConfigLocation(resolver.getResource(configLocation)); bean.setMapperLocations(resolver.getResources(mapperLocations)); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); }}
上面代码创建了一个SqlSessionFactory和一个SqlSessionTemplate,为了支持注解事务,增加了@EnableTransactionManagement注解,并且反回了一个PlatformTransactionManagerBean。
如果我们想要扫描MyBatis的Mapper接口,我们就需要配置这个类,这个配置我们需要单独放到一个类中:
@Configuration@AutoConfigureAfter(MyBatisConfig.class)public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("com.pinganfu.dayu.dal.mapper.auto"); return mapperScannerConfigurer; }}
阅读全文
0 0
- spring boot 学习笔记(一)
- spring boot学习笔记(一)
- spring boot学习笔记(一)
- Spring Boot学习笔记(一)
- Spring Boot入门学习笔记(一)
- spring boot学习笔记(一)
- Spring Boot 学习笔记(一)
- Spring Boot 学习笔记(一)
- Spring Boot学习笔记-从Hello World开始 (一)
- Spring Boot 学习笔记(一)——入门
- Spring Boot 学习笔记一(SpringBoot启动过程)
- Spring-Boot学习笔记(一) 项目搭建和HelloWold
- Spring Boot学习笔记----mybatis注解(一)
- Spring Boot学习笔记:(一)简介与HelloWorld搭建
- Spring-Boot学习笔记一:Hello World
- 学习笔记一:Spring Boot快速入门
- Spring Boot学习(一)
- Spring Boot学习(一)
- 二分练习
- linux 分析apache访问日志脚本
- 决策树DTC数据分析及鸢尾数据集分析
- CSU1973: 给自己出题的小X
- 修改电脑ip为静态
- spring boot学习笔记(一)
- c语言中变量的引用传递和指针
- react+leaflet入门Demo
- 最小费用流
- Vert.x学习笔记(二) Vert.x Web Client
- reverse integer
- opennebula : what's new in 5.4 中文版
- 机器学习之特征编码总结
- Java:InputStream读取文本文件内容出现乱码问题的解决方法