SpringBoot+Mybatis+Mysql环境搭建
SpringBoot+Mybatis+Mysql环境搭建
1.首先导包
每个包代表的含义
- spring-boot-starter-web包提供访问网页的能力
- mysql-connector-java 选择访问的数据库
- mybatis-spring-boot-starter 持久层框架包
- druid 数据源连接池
- spring-boot-starter-thymeleaf 解析html模板专用
- spring-boot-starter-data-redis spring集成redis所需要的包
2.创建Spirngboot启动类
@SpringBootApplication
@ComponentScan(“com.")
@MapperScan("com.tests.”)
public class SpringbootApplicon {
public static void main(String [] args){
SpringApplication.run(SpringbootApplicon.class, args);
}
}
启动类注解解释
SpringBootApplication 启动类必要注解,代表该类为Springboot程序入口
ComponentScan 扫描注解,将@Controller,@Service,…扫进容器中
@MapperScan Mybatis扫描,用于将javaMapper类,扫描到容器中
3.编写Controller类
注意事项:在Controller类上必须打上@Controller注解,表明该类为Controller类,并扫描到Spring容器中,返回值为html名称,返回值类型必须为Stirng
@Controller
public class TestController {
@RequestMapping("/hello")
public String hello() throws UnsupportedEncodingException {
return “hello”;
}
}
Controller类注解解释
@RequestMapping请求映射地址,如果是本地测试localhost:端口/hello
4.配置类 application.yml文件,不需要redis可将redis配置删除,如本地没有redis也需要删除
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
username: root
password: 123456
redis:
open: true
host: 127.0.0.1
port: 6379
password:
database: 0
timeout: 5000
pool:
max-active: 60
max-wait: -1
max-idle: 8
min-idle: 0
mybatis:
#需要扫描的map文件
mapperLocations: classpath:mybatis/**/*Mapper.xml
5.最后为项目结构
注意:此文档只是记录专用,如有错误欢迎指点,