1.整合mybatismysql
(1) 配置yml文件: spring boot 配置自動配置注入數據源redis
server: port: 8090 # #spring: # datasource: # druid: # url: jdbc:mysql://192.168.1.9:3306/ftt?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false # username: root # password: root # driver-class-name: com.mysql.jdbc.Driver spring: datasource: url: jdbc:mysql://192.168.1.9:3306/ftt?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver redis: -- 針對redis配置 port: 6379 database: 0 host: 192.168.1.5 jedis: pool: max-active: 8 mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.springboot.mapper debug: true
(2)經過代碼注入數據源,sqlSessionFactoryspring
//@Configuration public class MybatisConfig { // @Bean(name = "sqlSessionFactory") // public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { // SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); // bean.setDataSource(dataSource); // bean.setTypeAliasesPackage(" com.springboot.mappper"); // ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // bean.setMapperLocations(resolver.getResources("classpath:mapping/*.xml")); // return bean.getObject(); // } }
//@Configuration public class DataConfig { // @Primary // @Bean(name="dataSource") // public DruidDataSource dataSource(){ // DruidDataSource dataSource = new DruidDataSource(); // dataSource.setUrl("jdbc:mysql://192.168.1.9:3306/ftt?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false"); // dataSource.setDriverClassName("com.mysql.jdbc.Driver"); // dataSource.setPassword("root"); // dataSource.setUsername("root"); // return dataSource; // } }
後續代碼整合:sql
@RestController @RequestMapping("/person") public class PersonController { @Autowired private PersonMapper personMapper; @Autowired private StringRedisTemplate redisTemplate; @ResponseBody @RequestMapping("/test") public String test(){ return "測試成功99990000"; } @ResponseBody @RequestMapping("/getall") public List<PersonEntity> getall(){ String s = redisTemplate.opsForValue().get("ftt"); System.out.println(s); return personMapper.getAll(); } }
@SpringBootApplication @MapperScan(basePackages = "com.springboot.mapper") //掃描mapper文件 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
pom.xml 文件:tomcat
@1 若是不引入數據源包:使用springboot 默認整合的數據源springboot
@2 若是引入 druid-spring-boot-starter 1.1.0 版本存在缺陷,不要使用mybatis
會報org.springframework.beans.factory.UnsatisfiedDependencyException異常app
使用springboot 整合的數據源,使用的是阿里巴巴的數據源ide
@3 使用阿里巴巴的數據源,spring boot 裏面採用的仍是默認的type=HikariDataSourcespring-boot
2.熱部署 改動代碼無需啓動。只須要從新編譯就能夠訪問改動後的代碼
idea 設置:
1) 「File」 -> 「Settings」 -> 「Build,Execution,Deplyment」 -> 「Compiler」,選中打勾 「Build project automatically」 。
2) 組合鍵:「Shift+Ctrl+Alt+/」 ,選擇 「Registry」 ,選中打勾 「compiler.automake.allow.when.app.running」 。
mvn spring-boot:run
<!-- 熱部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> optional=true,依賴不會傳遞,該項目依賴devtools;以後依賴myboot項目的項目若是想要使用devtools,須要從新引入 –> </dependency>
3.整合redis
yml 配置:如上
pom引入 :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
直接引入使用:
@Autowired
private StringRedisTemplate redisTemplate;
4.spring boot 默認使用的容器是tomcat
pom.xml 能夠不引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency>
最初輸出: