spring+mybatis+springboot
- 通过网站快速创建项目
点击GENERATE创建 - 在propertise里连接数据库
server.port=8081
#==============================数据库相关配置
spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxxxxx?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=xxxxx
#使⽤阿⾥巴巴druid数据源,默认使⽤⾃带的
#spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#开启控制台打印sql
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# mybatis 下划线转驼峰配置,两者都可以
#mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.map-underscore-to-camel-case=true
#配置扫描
mybatis.mapper-locations=classpath:mapper/*.xml
#配置xml的结果别名
mybatis.type-aliases-package=net.xdclass.online_xdclass.domain
- 导入需要的xml
springboot核心依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
mysql驱动 (注意需要去掉runtime,否则报错)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
通用工具包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
跨域身份验证解决⽅案 Json web token包
<!-- JWT相关 -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
高性能缓存组件
<!--guava依赖包-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
项目热部署
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
//其实就这三句
<configuration>
<fork>true</fork><!--必须添加这个配置-->
</configuration>
</plugin>
</plugins>
</build
创建xml文件
?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="net.xdclass.online_xdclass.mapper.VideoMapper">
</mapper>
扫描mapper路径
@SpringBootApplication
@MapperScan("net.xdclass.online_xdclass.mapper")
public class OnlineXdclassApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineXdclassApplication.class, args);
}
}
配置一下
使⽤快捷键打开,选择Registry
选择compiler.automake.allow.when.app.running ,重启idea
window快捷键 Shift+Ctrl+Alt+/
mac快捷键 Shift+Command+Alt+/
file->setting->
4. 加入需要的注解
app
controller
mapper
service没有
serviceImpl
创建utils工具类
创建jsondata用于和前端交互
创建异常类Exception
全局异常处理