Hi,大家好,我是姜友华。
今天,我们一起来部署Spring Boot与Mybaits.
本来想用Spring Tools 4 for Eclipse,原因是用这个不用花钱。但界面不好看,换了IntelliJ IDEA ,这个太贵了,还好有30天试用,安装好后我开始今天的旅程。
一、新建项目。
-
启动IntelliJ IDEA,选择新建项目。
-
在新项目页面,选择Spring Initializr,所有项目按需要来填写,这里Java Version使用的是8,这可是万年的Java8。
项目设置
-
添加支持顶。我选择了Web、Mysql、MyBatis。
添加支持顶 点击
Finish
,等着编辑器完成初始化。
注意,我的工程为:com.muutr.shop,下面该以此作阐述。
- 假定有本地数据库shop,建表catagory并保存好数据。这个,我在Mysql8.0递归函数中做,如需要,你也可照着做一个。表的数据如下。
+----+----------------------+-----------+
| id | title | parent_id |
+----+----------------------+-----------+
| 1 | Electronics | NULL |
| 2 | Laptops & PC | 1 |
| 3 | Laptops | 2 |
| 4 | PC | 2 |
| 5 | Cameras & photo | 1 |
| 6 | Camera | 5 |
| 7 | Phones & Accessories | 1 |
| 8 | Smartphones | 7 |
| 9 | Android | 8 |
| 10 | iOS | 8 |
| 11 | Other Smartphones | 8 |
| 12 | Batteries | 7 |
| 13 | Headsets | 7 |
| 14 | Screen Protectors | 7 |
+----+----------------------+-----------+
- 添加数据库连接信息。
在shop → src → main → resource → application.properties添加下面内容。一般的配置我们可以写在application.properties里。
# 连接数据库。
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=jiangyouhua
- 运行一下,看看可不可行?
在浏览器输入:http://localhost:8080/
回车。如显示如下图,恭喜你,第一步成功了。

二、添加Lombok。
- 为了少写一些getter setter之类,我们决定加载Lombok插件。
在pom.xml里添加下列代码。一般来说添加<dependencies>
部分就可以了。
......
<dependencies>
.......
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
......
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
但如果出现java: cannot find symbol symbol: class data site:stackoverflow.com
错误的话,你需要像我这样写全两部分。
-
设置为Lombok注解在编译阶段起到作用。
设置为Lombok注解在编译阶段起到作用
勾选“Enable annotation procesing”,即可。
-
常用注解
@Setter :可用类、字段,生成setter方法;
@Getter :同上,生成getter方法;
@ToString :用于类,生成toString方法。
@EqualsAndHashCode: 用于类,生成hashCode、equals方法。
@NoArgsConstructor: 用于类,生成无参构造方法。
@RequiredArgsConstructor: 用于类,生成含特殊属性的构造方法,如final、@NonNull注解的属性。
@AllArgsConstructor: 用于类,生成含所有属性的构造方法。
@Data: 用于类,生成setter/getter、equals、canEqual、hashCode、toString方法,不生成final修改的属性的setter方法。
@Slf4j: 用于类,生成log变量,严格意义来说是常量。
三、添加代码。
在添加代码之前,需要在shop → src → main → java → com.muutr.shop里添加下列包。完成后再在service包里添加impl包。
- entity,实体,对应数据库表;
- mapper,映射,实现设计数据库查询;
- service,服务,对接数据库查询行为。分接口类与实现类,实现类在其下的impl包里。当然你也可以不要接口类;
- controller,控制器,这个就不说了;
- 在entity包里添加Category类。
package com.muutr.shop.entity;
@data
public class category {
private int id;
private String title;
private int parentId;
}
- 在mapper包里添加CategoryMapper接口。
package com.muutr.shop.mapper;
import com.muutr.shop.entity.Category;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface CategoryMapper {
@Select("SELECT * FROM category")
List<Category> selectAll();
}
暂时把查询语句写在注释里。
- 在service包添加CategoryService接口。
package com.muutr.shop.service;
import com.muutr.shop.entity.Category;
import java.util.List;
public interface CategoryService {
List<Category> findAll();
}
- 在service.impl包添加CategoryServiceImpl实现类。CategoryServiceImpl类实现了CategoryService接口。
package com.muutr.shop.service.impl;
import com.muutr.shop.mapper.CategoryMapper;
import com.muutr.shop.service.CategoryService;
import com.muutr.shop.entity.Category;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {
private final CategoryMapper categoryMapper;
public CategoryServiceImpl(CategoryMapper categoryMapper) {
this.categoryMapper = categoryMapper;
}
@Override
public List<Category> findAll() {
return categoryMapper.selectAll();
}
}
- 在controller包里添加CategoryController,作为查找所有分类项的入口。
package com.muutr.shop.controller;
import com.muutr.shop.service.CategoryService;
import com.muutr.shop.entity.Category;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}
@GetMapping(value = "/findall")
public List<Category> findAll(){
return categoryService.findAll();
}
}
第二阶段的所有事情都做完了,运行一下,在浏览器地址栏输入:http://localhost:8080/findall
,如显示如下则为正确。那恭喜你,赢得了第二阶段的胜利。

四、使用*mapper.xml
现在的查询语句是在注释里,在简单的工程里这个非常好用。当工程一复杂,我们会希望有统一的管理入口。Mybaits支持这样的处理。
- 建立mapper文件夹,作为查询语句的入口。
- 在shop → src → main → resource里建立mapper文件夹。
-
建立一个mapper.xml模板,以方便同类型文档的创建。
添加模板
添加模板在Preferences里实现。最后需要添加下面的代码到5里。
<?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="">
</mapper>
- 现在,在mapper文件夹添加一个
CatagoryMapper.xml
文件。
- 新建一个Mapper文件,并将添加一个
select
标签。
-
首先,为了实现语法支持,我们需要按下图进行设置一下
Mysql语法支持
-
其次,需要绑定数据库。
添加Mysql数据库
建立数据连接
- 连接好之后,将category表,往CategoryMapper里拖,以建立连接。
- 添加
select
标签如下。 - 注意填写的命名空间要与Mapper接口匹配。
- 需要有返回格式,List的填写元素的类型。
<?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="com.muutr.shop.mapper.CategoryMapper">
<select id="selectAll" resultType="com.muutr.shop.entity.Category">
select * from category
</select>
</mapper>
- 设置对Mapper.xml的查找。
- 在application.properties里添加。
mybatis.mapper-locations =classpath:mapper/*Mapper.xml
- 删除CategoryMapper类的select注释。
- 运行后使用浏览器查看,数据应该与第二阶段的结果相同。如果是,恭喜你,你是今天的胜利者。
请留意,现在可以看出,Mapper.xml不需要放在类同与Mapper接口的文件夹结构里。
好,今天就到这里,我是姜友华,再见。
网友评论