SpringBoot + MyBatis 查询语句 (根据条件查询) 1.新建数据库和表内容
全部根据 tt 中的数据进行查询
2.创建工程目录结构(springboot创建好后)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 <?xml version="1.0" encoding="UTF-8"?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.4.5</version > <relativePath /> </parent > <groupId > com.kejian</groupId > <artifactId > demo</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > demo</name > <description > Demo project for Spring Boot</description > <properties > <java.version > 1.8</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 2.2.0</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.5.6</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > <version > 1.2.3</version > </dependency > <dependency > <groupId > commons-lang</groupId > <artifactId > commons-lang</artifactId > <version > 2.3</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > 1.18.20</version > </dependency > </dependencies > <build > <resources > <resource > <directory > src/main/java</directory > <includes > <include > **/*.yml</include > <include > **/*.properties</include > <include > **/*.xml</include > </includes > <filtering > false</filtering > </resource > <resource > <directory > src/main/resources</directory > <includes > <include > **/*.yml</include > <include > **/*.properties</include > <include > **/*.xml</include > </includes > <filtering > false</filtering > </resource > </resources > </build > </project >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/kejian?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:/mapper/*.xml config-location: classpath:mybatis-config.xml
在resources目录下新建mybatis-config.xml(按需求添加,可以不要)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration > <settings > <setting name ="useGeneratedKeys" value ="true" /> <setting name ="useColumnLabel" value ="true" /> <setting name ="mapUnderscoreToCamelCase" value ="true" /> </settings > </configuration >
至此基础配置OK,在目录下添加类
3.entity层下添加实体类 entity包也有叫 pojo, damain, 都是一个意思
新建 Poem.java(添加实体类,和数据库对应)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 @Data public class Poem { private Integer id; private String tt; private String title; private String A; private String B; private String C; private String D; private String letter; public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getTt () { return tt; } public void setTt (String tt) { this .tt = tt; } public String getTitle () { return title; } public void setTitle (String title) { this .title = title; } public String getA () { return A; } public void setA (String a) { A = a; } public String getB () { return B; } public void setB (String b) { B = b; } public String getC () { return C; } public void setC (String c) { C = c; } public String getD () { return D; } public void setD (String d) { D = d; } public String getLetter () { return letter; } public void setLetter (String letter) { this .letter = letter; } @Override public String toString () { return "Poem{" + "id=" + id + ", time='" + tt + '\'' + ", title='" + title + '\'' + ", A='" + A + '\'' + ", B='" + B + '\'' + ", C='" + C + '\'' + ", D='" + D + '\'' + ", letter='" + letter + '\'' + '}' ; } }
4.添加 mapper 操纵数据库 dao 层名为持久层,也可叫 mapper 层
1 2 3 4 5 @Mapper public interface PoemMapper { List<Poem> findSome (@Param("tt") String time) ; }
resources 下新建 mapper 文件夹,存放dao 层 mapper 的映射文件
1 2 3 4 5 6 7 8 9 <?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.example.first_demo3.dao.PoemMapper" > <select id ="findSome" resultType ="com.example.first_demo3.entity.Poem" > select id,title,A,B,C,D,letter from poem where tt = #{tt} </select > </mapper >
5.添加业务逻辑 service层为业务逻辑层,处理算法并返回数据给界面层
service下新建 PoemService.java 选择接口类
1 2 3 public interface PoemService { List<Poem> findSome (String time) ; }
service下新建impl文件夹,管理映射
impl下新建PoemServiceImpl.java
1 2 3 4 5 6 7 8 9 10 @Service public class PoemServiceImpl implements PoemService { @Autowired private PoemMapper poemMapper; @Override public List<Poem> findSome (String tt) { return poemMapper.findSome(tt); } }
6.终于可以写接口了 controller 层又名界面层,调用 service
controller 下新建PoemController.java
1 2 3 4 5 6 7 8 @RestControllerpublic class PoemController { @Autowired private PoemService poemService; @PostMapping("/get/{tt}") public List<Poem>getSome(@PathVariable String tt){ return poemService.findSome(tt); } }
7.PostMan测试接口
其他的雷同,在 /get/ 输入对应的 tt 值就可调用
最终的目录