一.一些常用的配置
1.1 添加scope=import的依赖
1.2 修改jdk的编译版本
修改pom.xml文件
1.3 禁用特定的自动配置
使用@EnableAutoConfiguation的exclude属性即可完成:
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})publicclass MyConfiguration {
publicstaticvoid main(String[] args) {
SpringApplication.run(MyConfiguration.class,args);
}
}
1.4@SpringBootApplication注解
@springbootapplication注解:它相当于以默认属性使用@Configuration, @EnableAutoConfiguration 和@ComponentScan
可以用于简化开发.
二. SpringBoot的热部署插件spring-boot-devtools
2.1 添加maven依赖
2.2 修改maven插件.添加配置.
2.3 重新启动
三.Spring整合mybatis
3.1 引入mybatis的依赖
3.2 在Application类上面声明Mapper包的扫描
3.3 application.properties中书写数据源
##c3p0
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
c3p0.user=root
c3p0.password=root
c3p0.driverClass=com.mysql.jdbc.Driver
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
3.4 建立mybatis的配置类,配置c3p0数据源和sessionfactory
@Configurationpublicclass DataSourceConfig {
@Bean(name = "dataSource")
@Primary
@ConfigurationProperties(prefix = "c3p0")
public DataSource dataSource(){
returnDataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory() {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource());
return bean;
}
}
3.5 编写Mapper借口和Mapper配置文件
3.6 编写service
注意涉及到增删改的操作需要用@Transactional开启事务
@Servicepublicclass DogService {
@Autowired
private DogMapper dogMapper;
public Dog findById(Integer id) {
return dogMapper.findById(id);
}
publicList findAll() {
return dogMapper.findAllDogs();
}
@Transactional
publicvoid saveDog(Dog dog) {
dogMapper.saveDog(dog);
}
}
3.7 编写Controller
@Controllerpublicclass DogController {
@Autowired
private DogService dogService;
@RequestMapping("/findById/{id}")
@ResponseBody
publicDog findDogById(@PathVariable("id") Integer id) {
return dogService.findById(id);
}
@RequestMapping("/findAll")
@ResponseBody
publicList findAll() {
return dogService.findAll();
}
@RequestMapping("/saveDog/{name}/{age}")
publicString saveDog(@PathVariable("name")String name,@PathVariable("age")String age) {
Dog dog=new Dog();
dog.setName(name);
dog.setAge(age);
dogService.saveDog(dog);
return"addSuccess";
}
}
运行Application进行测试