Loading... # 继续说怎么写接口 通过0x01,我们大概了解了java的springboot的核心思想和MVC设计模式,这时候,我们可以通过maven来创建一个SpringBoot的项目了。 这是pom.xml文件内容 ## 写pom文件,引入依赖 <div class="tip inlineBlock share"> 这里的内容可不是随便乱写的哦,想用哪些依赖,可以通过https://mvnrepository.com/来找所需要的依赖。 maven相关介绍可以参考下面的文章 <div class="preview"> <div class="post-inser post box-shadow-wrap-normal"> <a href="https://www.zunmx.top/archives/15/" target="_blank" class="post_inser_a no-external-link no-underline-link"> <div class="inner-image bg" style="background-image: url(https://www.zunmx.top/usr/themes/handsome/assets/img/sj/2.jpg);background-size: cover;"></div> <div class="inner-content" > <p class="inser-title">Maven 阶段性总结</p> <div class="inster-summary text-muted"> Maven部分maven入门 maven在一般场景类似于Linux系统中的rpm、yum、apt、dpkg... </div> </div> </a> <!-- .inner-content #####--> </div> <!-- .post-inser ####--> </div> </div> ```xml <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>top.zunmx</groupId> <artifactId>myshop</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.5</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.5</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies> </project> ``` 细心的人可以发现了,这里面有好多spring-boot-starter,这是什么呢?通过这些可以更快捷的创建我们的项目,如果你的spring想使用jdbc,那么就加上spring-boot-starter-jdbc,如果想使用mybatis,那么就加上mybatis-spring-boot-starter。 ### 点一点 我们可以看一下mybatis-spring-boot-starter的pom文件, ![image.png](https://www.zunmx.top/usr/uploads/2022/10/684127358.png) mybatis-spring-boot-starter自动帮我们把org.mybatis的依赖给加进来了。 在这个pom中,我们也管org.mybatis是mybatis-spring-boot-starter的子依赖 ### lombok lombok是真的很强大,我们在写实体对象的时候,我们需要写get和set方法,以及构造方法,而lombok通过一个注解就可以实现, 比如 @Data ## 开始写项目吧 除了test,按照这个目录层级创建目录和java文件 ![image.png](https://www.zunmx.top/usr/uploads/2022/10/3726765701.png) ### 创建数据库以及表结构 ```sql CREATE TABLE `users` ( `id` int NOT NULL, `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL, `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL, `phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL, `status` int DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC; insert into users values('1','zhangsan','123','18808881888','0'); ``` controller 就是我们0x01中的前后端通信的媒介 dao 是操作数据库的 pojo 实体对象 service 是服务层 ### UserController ```java package wuu.shop.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import wuu.shop.pojo.User; import wuu.shop.service.UserService; @RestController // 表示我是一个接口,返回的是一个JSON public class UserController { @Autowired // 注入我们的服务 private UserService userService; @RequestMapping("/getUserByUserName") // 接口地址 public User getUser(String username) { // 请求参数 User user = userService.getUserByUserName(username); return user; // 返回给View层 } } ``` ### UserService ```java package wuu.shop.service; import wuu.shop.pojo.User; public interface UserService { User getUserByUserName(String username); } ``` ### UserServiceImpl ```java package wuu.shop.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import wuu.shop.dao.UserDao; import wuu.shop.pojo.User; @Service //表示我是个服务,可以被Spring扫描,并且交给容器控制。 public class UserServiceImpl implements UserService { @Autowired // 注入我们的数据库操作对象 private UserDao userDao; public User getUserByUserName(String username) { return userDao.getUserByUserName(username); } } ``` ### UserDao ``` package wuu.shop.dao; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Component; import wuu.shop.pojo.User; @Mapper // mybatis的映射层,我也可以被spring容器扫描,并且创建对象 public interface UserDao { @Select("select * from users where username = #{username}") // 执行的SQL public User getUserByUserName(String username); } ``` ### User ```java package wuu.shop.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; @Data //lombok的注解,可以提供get和set方法 @AllArgsConstructor // lombok的注解,全参构造 @NoArgsConstructor // lombok的注解,无参构造 @ToString // lombok的注解,重写ToString方法 public class User { private Integer id; private String username; private String password; private String phone; private Integer status; } ``` ### myShopApplication ```java package wuu.shop; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // 我是springboot的主入口 public class myShopApplication { public static void main(String[] args) { SpringApplication.run(myShopApplication.class, args); //启动项目 } } ``` ### application.properties ```properties #服务启动的端口号 server.port=8762 #数据库用户名 spring.datasource.username=root #数据库密码 spring.datasource.password=****** #数据库连接字符串 spring.datasource.url=jdbc:mysql://localhost:3306/shop?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=UTF-8 #数据库连接驱动 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` ### 启动项目吧 此时此刻,你已经实现了一个简单的springboot的项目了。 ![image.png](https://www.zunmx.top/usr/uploads/2022/10/1028774590.png) © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏