0


后端Springboot框架搭建APi接口开发(第一章)

本文章以IDEA为开发工具,使用SSM框架进行项目编写

第一节:设计并创建数据库

我们用一个简单的用户表进行操作演示

首先创建Data数据库

createdatabasedata;

创建User数据表,表中包含用户邮箱用户姓名用户密码

createtableuser(
    UserEamil varchar(50)notnullprimarykey, 
    UserName varchar(50)notnull,
    UserPWd varchar(50)notnull)engineinnoDBdefaultcharset=utf8;

放点数据进去

insertintouservalue('[email protected]','张三','admin123');insertintouservalue('[email protected]','李四','admin456');insertintouservalue('[email protected]','王五','admin789');

第二节:创建简易的SpringBoot项目

打开IDEA,在右上角点击NewProject。创建新的项目

image-20230323164020979

选择Spring Initializr框架,依次输入项目名称、项目路径、类型、组名、java版本。最后选择Next下一步。(为了统一性,建议与下图保持一致)

image-20230323164306902

这一步是要选择你项目中所需要的依赖。但是我这里要在配置文件中手动添加,因此直接点击右下角的create,完成创建。

image-20230323164818450

第三节:配置IDEA Database文件

在IDEA对话框的右上角找到竖着写的Database

image-20230322143255319

根据图文提示创建新的mysql数据库连接

image-20230322143324409

根据图文填写信息

image-20230322143750589

第四节:配置Pom.xml文件

pom.xml文件是maven的核心配置文件,主要用于导入maven依赖已经maven常规配置

注意:强烈不建议任何xml配置文件代码中存在中文注释

第一小节:导入maven依赖

Springboot API接口开发需要Mysql、MyBatis、junit、lombok、Springboot、SpringWeb依赖

查找maven的依赖可以在:maven官方网站查询(需要科x上网)

在项目结构最顶端找到pom.xml文件

image-20230323165250383

可以看到pom.xml文件中只有一些基本的配置信息,和一个Stringboot依赖

image-20230323165319721

可以复制我的依赖配置信息到pom.xml文件中

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version><scope>provided</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.6.14</version></dependency>

第二小节:maven静态资源导出问题

静态资源导出,,不管是java文件下还是resources文件下,都导出

<resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.yml</include><include>**/*.properties</include><include>**/*.xml</include><!--主要是加这行--><include>**/*.setting</include></includes><filtering>false</filtering></resource></resources>

第三小节:刷新maven配置文件

一、二小节的配置完成后,点击一些右上角带M的小图标就可以了

image-20230322142707897

如果没有那个小图标,可以点击右侧的Maven里面的Install

image-20230323165635281

这里是完整的pom.xml配置代码

文件路径:SpringBoot\pom.xml

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>demo</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.11</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version><scope>provided</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.6.14</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><mainClass>com.example.demo.DemoApplication</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.yml</include><include>**/*.properties</include><include>**/*.xml</include><!--主要是加这行--><include>**/*.setting</include></includes><filtering>false</filtering></resource></resources></build></project>

第五节:配置Mysql配置文件

在目录结构中创建一个resource文件夹,并在文件夹下创建一个Mybatis-config.xml文件

image-20230323165858347

注意配置driver、url、username、password等信息

文件路径:SpringBoot\src\main\resources\Mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environmentsdefault="development"><environmentid="development"><transactionManagertype="JDBC"/><dataSourcetype="POOLED"><propertyname="driver"value="com.mysql.cj.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/data?characterEncoding=UTF-8&amp;serverTimezone=UTC"/><propertyname="username"value="root"/><propertyname="password"value="xxxxxxxxxx"/></dataSource></environment></environments><!--    这里的mappers因mapper还没有开始编写,先暂时注释掉--><!--    <mappers>--><!--        <mapper resource="org/mybatis/example/BlogMapper.xml"/>--><!--    </mappers>--></configuration>

第六节:创建基础文件结构

按照图文所示,在main文件目录下依次创建java->com->zzj->demo,又在demo目录下创建controller、entity、mapper、util文件夹
文件夹名称文件夹中文名文件夹说明controllerapi接口类用于编写api接口方法entity实体类用于编写与数据库字段的实体类方法,要求必须与数据库的字段相同mappermapper方法类用于编写mybatis操作数据库的执行方法util工具类用于编写会在程序中多次调用的方法,并封装起来供多次调用
image-20230323170528033

第七节:mybatis基础框架搭建

第一小节:编写实体类

在entity文件夹中创建User.java(文件名最好与数据表表命保持一致)

image-20230322152640202

这里用lombok注解的方式创建实体类

文件路径:demo\src\main\java\com\zzj\demo\entity\user.java

packagecom.zzj.demo.entity;importlombok.*;@Getter//Get方法@Setter//Set方法@ToString//ToString方法@AllArgsConstructor//有产@NoArgsConstructor//无产publicclass user {privateStringUserEamil;privateStringUserName;privateStringUserPwd;}

第二小节:编写mapper方法

这里以查询全部用户信息为例

在mapper文件夹中创建usermapper.java(注意:这里的usermapper.java需要创建interface)以及usermapper.xml两个文件

image-20230323171054912

UserMapper.java

文件路径:demo\src\main\java\com\zzj\demo\mapper\UserMapper.java

packagecom.zzj.demo.mapper;importcom.zzj.demo.entity.user;importjava.util.List;publicinterfaceUserMapper{List<user>seleteUserList();}

UserMapper.xml

文件路径:demo\src\main\java\com\zzj\demo\mapper\UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.zzj.demo.mapper.UserMapper"><selectid="seleteUserList"resultType="com.zzj.demo.entity.user">
        select * from user;
    </select></mapper>

找到刚才创建的pom.xml核心配置文件文件,找到注释的代码,并取消注释

这里的mappers因mapper还没有开始编写,先暂时注释掉

文件路径:SpringBoot\src\main\resources\Mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environmentsdefault="development"><environmentid="development"><transactionManagertype="JDBC"/><dataSourcetype="POOLED"><propertyname="driver"value="com.mysql.cj.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/data?characterEncoding=UTF-8&amp;serverTimezone=UTC"/><propertyname="username"value="root"/><propertyname="password"value="XXXXXXXXXXXX"/></dataSource></environment></environments><!--    这里的mappers因mapper还没有开始编写,先暂时注释掉--><mappers><mapperresource="com\zzj\demo\mapper\UserMapper.xml"/></mappers></configuration>

第三小节:编写测试类

到这里Mybatis的基本框架就算是搭建完成了,但是还需要在测试类运行一些。看看是否可以获取信息

src创建一个新的Test文件夹,并且创建一个与上面相同的文件夹结构

并且在文件夹最底层创建一个mapperTest.java测试类文件

image-20230322172456202

文件路径:SpringBoot\src\test\java\com\zzj\UserMapperTest.java

packagecom.zzj;importcom.zzj.entity.User;importcom.zzj.mapper.UserMapper;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Test;importjava.io.IOException;importjava.io.InputStream;publicclassUserMapperTest{@TestpublicvoidTestDemo01()throwsIOException{//获取Mybatis配置文件String resource ="Mybatis-config.xml";//        构建SqlSessionFactoryInputStream resourceAsStream =Resources.getResourceAsStream(resource);SqlSessionFactory build =newSqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = build.openSession();//        调用映射器接口UserMapper mapper = sqlSession.getMapper(UserMapper.class);//        遍历数据,并打印for(User user : mapper.seleteUserList()){System.out.println(user);}//        记得一点要关闭SqlSession,否者会导致Mysql连接数过高,无法访问
        sqlSession.close();}}

第四小节:运行测试类

点击下图的绿色小图标就可以运行测试类

image-20230323155710917

在下面的控制台可以看见结果已经输出了

image-20230323160410246

到这里Mybatis数据库查询操作就已经完成了。但是还没有实现利用APi接口进行数据库访问的功能,具体会在下节讲解。

标签: spring boot java spring

本文转载自: https://blog.csdn.net/qq_33957967/article/details/129800709
版权归原作者 盒子里的加菲猫 所有, 如有侵权,请联系我们删除。

“后端Springboot框架搭建APi接口开发(第一章)”的评论:

还没有评论