3.1.2 数据库表导入
项目的数据库创建好了之后, 可以直接将 资料/数据模型/db_reggie.sql 直接导入到数据库中, 也可以通过两种方式实现:
1).图形界面
2).命令行
注意: 通过命令导入表结构时,注意sql文件不要放在中文目录中
3.1.3 数据库表介绍
数据库表导入之后, 接下来介绍一下本项目中所涉及到的表结构:
上述的表结构, 我们目前先简单的结合页面原型了解一下, 大概有那些表, 每张表结构中存储什么样的数据, 有一个印象。对于具体的表结构, 以及表结构中的字段, 在讲解具体的功能开发时, 我们再详细介绍。
3.2 Maven项目搭建
3.2.1 创建maven项目
1). 在idea中创建maven project, 项目名称 reggie_take_out
2). 检查项目编码
3). 检查maven配置
4). 检查JDK版本
JDK的版本选择1.8;
3.2.2 搭建基础环境
1).在pom.xml中导入依赖
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#117700"><</span><span style="color:#117700">properties</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">java.version</span><span style="color:#117700">></span>1.8<span style="color:#117700"></</span><span style="color:#117700">java.version</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">properties</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">parent</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework.boot<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-boot-starter-parent<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>2.4.5<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">relativePath</span><span style="color:#117700">/></span> <span style="color:#aa5500"><!-- lookup parent from repository --></span> <span style="color:#117700"></</span><span style="color:#117700">parent</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependencies</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework.boot<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-boot-starter<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework.boot<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-boot-starter-test<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">scope</span><span style="color:#117700">></span>test<span style="color:#117700"></</span><span style="color:#117700">scope</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework.boot<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-boot-starter-web<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">scope</span><span style="color:#117700">></span>compile<span style="color:#117700"></</span><span style="color:#117700">scope</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>com.baomidou<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>mybatis-plus-boot-starter<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>3.4.2<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.projectlombok<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>lombok<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>1.18.20<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>com.alibaba<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>fastjson<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>1.2.76<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>commons-lang<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>commons-lang<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>2.6<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>mysql<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>mysql-connector-java<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">scope</span><span style="color:#117700">></span>runtime<span style="color:#117700"></</span><span style="color:#117700">scope</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>com.alibaba<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>druid-spring-boot-starter<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>1.1.23<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependency</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">dependencies</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">build</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">plugins</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">plugin</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">groupId</span><span style="color:#117700">></span>org.springframework.boot<span style="color:#117700"></</span><span style="color:#117700">groupId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span>spring-boot-maven-plugin<span style="color:#117700"></</span><span style="color:#117700">artifactId</span><span style="color:#117700">></span> <span style="color:#117700"><</span><span style="color:#117700">version</span><span style="color:#117700">></span>2.4.5<span style="color:#117700"></</span><span style="color:#117700">version</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">plugin</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">plugins</span><span style="color:#117700">></span> <span style="color:#117700"></</span><span style="color:#117700">build</span><span style="color:#117700">></span></span></span>
2).在工程的resources目录下创建application.yml文件,并引入配置
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#221199">server</span><span style="color:#555555">:</span> <span style="color:#221199"> port</span><span style="color:#555555">: </span><span style="color:#116644">8080</span> <span style="color:#221199">spring</span><span style="color:#555555">:</span> <span style="color:#221199"> application</span><span style="color:#555555">:</span> <span style="color:#aa5500">#应用名称 , 可选</span> <span style="color:#221199"> name</span><span style="color:#555555">: </span>reggie_take_out <span style="color:#221199"> datasource</span><span style="color:#555555">:</span> <span style="color:#221199"> druid</span><span style="color:#555555">:</span> <span style="color:#221199"> driver-class-name</span><span style="color:#555555">: </span>com.mysql.cj.jdbc.Driver <span style="color:#221199"> url</span><span style="color:#555555">: </span>jdbc<span style="color:#555555">:</span>mysql<span style="color:#555555">:</span>//localhost<span style="color:#555555">:</span>3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true <span style="color:#221199"> username</span><span style="color:#555555">: </span>root <span style="color:#221199"> password</span><span style="color:#555555">: </span>root <span style="color:#221199">mybatis-plus</span><span style="color:#555555">:</span> <span style="color:#221199"> configuration</span><span style="color:#555555">:</span> <span style="color:#aa5500">#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射 address_book ---> AddressBook</span> <span style="color:#221199"> map-underscore-to-camel-case</span><span style="color:#555555">: </span><span style="color:#770088">true</span> <span style="color:#aa5500">#日志输出</span> <span style="color:#221199"> log-impl</span><span style="color:#555555">: </span>org.apache.ibatis.logging.stdout.StdOutImpl <span style="color:#221199"> global-config</span><span style="color:#555555">:</span> <span style="color:#221199"> db-config</span><span style="color:#555555">:</span> <span style="color:#221199"> id-type</span><span style="color:#555555">: </span>ASSIGN_ID</span></span>
3).创建包 com.itheima.reggie , 并编写启动类
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">import</span> <span style="color:#000000">lombok</span>.<span style="color:#000000">extern</span>.<span style="color:#000000">slf4j</span>.<span style="color:#000000">Slf4j</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">boot</span>.<span style="color:#000000">SpringApplication</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">boot</span>.<span style="color:#000000">autoconfigure</span>.<span style="color:#000000">SpringBootApplication</span>; <span style="color:#555555">@Slf4j</span> <span style="color:#555555">@SpringBootApplication</span> <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ReggieApplication</span> { <span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) { <span style="color:#000000">SpringApplication</span>.<span style="color:#000000">run</span>(<span style="color:#000000">ReggieApplication</span>.<span style="color:#770088">class</span>,<span style="color:#000000">args</span>); <span style="color:#000000">log</span>.<span style="color:#000000">info</span>(<span style="color:#aa1111">"项目启动成功..."</span>); } }</span></span>
@Slf4j :
是lombok中提供的注解, 用来通过slf4j记录日志。
当搭建完上述的基础环境之后, 就可以通过引导类, 启动该项目。
3.2.3 前端静态资源导入
我们作为服务端开发工程师, 我们课程学习的重心应该放在后端的业务代码上, 前端的页面我们只需要导入课程资料中的前端资源, 前端页面的代码我们只需要能看懂即可。
1). 导入静态资源
前端资源存放位置为 资料/前端资源 :
将上述两个目录中的静态资源文件, 导入到项目的resources目录下:
2). 创建配置类WebMvcConfig,设置静态资源映射
用于在Springboot项目中, 默认静态资源的存放目录为 : "classpath:/resources/", "classpath:/static/", "classpath:/public/" ; 而在我们的项目中静态资源存放在 backend, front 目录中, 那么这个时候要想访问到静态资源, 就需要设置静态资源映射。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">import</span> <span style="color:#000000">lombok</span>.<span style="color:#000000">extern</span>.<span style="color:#000000">slf4j</span>.<span style="color:#000000">Slf4j</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">context</span>.<span style="color:#000000">annotation</span>.<span style="color:#000000">Configuration</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">web</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">config</span>.<span style="color:#000000">annotation</span>.<span style="color:#000000">ResourceHandlerRegistry</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">web</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">config</span>.<span style="color:#000000">annotation</span>.<span style="color:#000000">WebMvcConfigurationSupport</span>; <span style="color:#555555">@Slf4j</span> <span style="color:#555555">@Configuration</span> <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">WebMvcConfig</span> <span style="color:#770088">extends</span> <span style="color:#000000">WebMvcConfigurationSupport</span> { <span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 设置静态资源映射</span> <span style="color:#aa5500">* @param registry</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@Override</span> <span style="color:#770088">protected</span> <span style="color:#008855">void</span> <span style="color:#000000">addResourceHandlers</span>(<span style="color:#000000">ResourceHandlerRegistry</span> <span style="color:#000000">registry</span>) { <span style="color:#000000">log</span>.<span style="color:#000000">info</span>(<span style="color:#aa1111">"开始进行静态资源映射..."</span>); <span style="color:#000000">registry</span>.<span style="color:#000000">addResourceHandler</span>(<span style="color:#aa1111">"/backend/**"</span>).<span style="color:#000000">addResourceLocations</span>(<span style="color:#aa1111">"classpath:/backend/"</span>); <span style="color:#000000">registry</span>.<span style="color:#000000">addResourceHandler</span>(<span style="color:#aa1111">"/front/**"</span>).<span style="color:#000000">addResourceLocations</span>(<span style="color:#aa1111">"classpath:/front/"</span>); } }</span></span>
3). 访问测试
http://localhost:8080/backend/index.html