本文章以IDEA为开发工具,使用SSM框架进行项目编写
我们用一个简单的用户表进行操作演示
首先创建Data数据库
create database data;
创建User数据表,表中包含用户邮箱,用户姓名,用户密码
create table user
(
UserEamil varchar(50) not null primary key,
UserName varchar(50) not null,
UserPWd varchar(50) not null
) engine innoDB default charset=utf8;
放点数据进去
insert into user value('[email protected]','张三','admin123');
insert into user value('[email protected]','李四','admin456');
insert into user value('[email protected]','王五','admin789');
打开IDEA,在右上角点击NewProject。创建新的项目
选择Spring Initializr框架,依次输入项目名称、项目路径、类型、组名、java版本。最后选择Next下一步。(为了统一性,建议与下图保持一致)
这一步是要选择你项目中所需要的依赖。但是我这里要在配置文件中手动添加,因此直接点击右下角的create,完成创建。
在IDEA对话框的右上角找到竖着写的Database
根据图文提示创建新的mysql数据库连接
根据图文填写信息
pom.xml文件是maven的核心配置文件,主要用于导入maven依赖已经maven常规配置
注意:强烈不建议任何xml配置文件代码中存在中文注释
Springboot API接口开发需要Mysql、MyBatis、junit、lombok、Springboot、SpringWeb依赖
查找maven的依赖可以在:maven官方网站查询(需要科x上网)
在项目结构最顶端找到pom.xml文件
可以看到pom.xml文件中只有一些基本的配置信息,和一个Stringboot依赖
可以复制我的依赖配置信息到pom.xml文件中
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.9version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.32version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-apiartifactId>
<version>5.9.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.6.14version>
dependency>
静态资源导出,,不管是java文件下还是resources文件下,都导出
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.ymlinclude>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
<include>**/*.settinginclude>
includes>
<filtering>falsefiltering>
resource>
resources>
一、二小节的配置完成后,点击一些右上角带M的小图标就可以了
如果没有那个小图标,可以点击右侧的Maven里面的Install
这里是完整的pom.xml配置代码
文件路径:SpringBoot\pom.xml
<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.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demoname>
<description>demodescription>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring-boot.version>2.6.11spring-boot.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.9version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.32version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-apiartifactId>
<version>5.9.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.6.14version>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>${spring-boot.version}version>
<configuration>
<mainClass>com.example.demo.DemoApplicationmainClass>
<skip>trueskip>
configuration>
<executions>
<execution>
<id>repackageid>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.ymlinclude>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
<include>**/*.settinginclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
project>
在目录结构中创建一个resource文件夹,并在文件夹下创建一个Mybatis-config.xml文件
注意配置driver、url、username、password等信息
文件路径:SpringBoot\src\main\resources\Mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/data?characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="xxxxxxxxxx"/>
dataSource>
environment>
environments>
configuration>
按照图文所示,在main文件目录下依次创建java->com->zzj->demo,又在demo目录下创建controller、entity、mapper、util文件夹
文件夹名称 | 文件夹中文名 | 文件夹说明 |
---|---|---|
controller | api接口类 | 用于编写api接口方法 |
entity | 实体类 | 用于编写与数据库字段的实体类方法,要求必须与数据库的字段相同 |
mapper | mapper方法类 | 用于编写mybatis操作数据库的执行方法 |
util | 工具类 | 用于编写会在程序中多次调用的方法,并封装起来供多次调用 |
在entity文件夹中创建User.java(文件名最好与数据表表命保持一致)
这里用lombok注解的方式创建实体类
文件路径:demo\src\main\java\com\zzj\demo\entity\user.java
package com.zzj.demo.entity;
import lombok.*;
@Getter //Get方法
@Setter //Set方法
@ToString //ToString方法
@AllArgsConstructor //有产
@NoArgsConstructor //无产
public class user {
private String UserEamil;
private String UserName;
private String UserPwd;
}
这里以查询全部用户信息为例
在mapper文件夹中创建usermapper.java(注意:这里的usermapper.java需要创建interface)以及usermapper.xml两个文件
文件路径:demo\src\main\java\com\zzj\demo\mapper\UserMapper.java
package com.zzj.demo.mapper;
import com.zzj.demo.entity.user;
import java.util.List;
public interface UserMapper {
List<user> seleteUserList();
}
文件路径:demo\src\main\java\com\zzj\demo\mapper\UserMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzj.demo.mapper.UserMapper">
<select id="seleteUserList" resultType="com.zzj.demo.entity.user">
select * from user;
select>
mapper>
找到刚才创建的pom.xml核心配置文件文件,找到注释的代码,并取消注释
这里的mappers因mapper还没有开始编写,先暂时注释掉
文件路径:SpringBoot\src\main\resources\Mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/data?characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="XXXXXXXXXXXX"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="com\zzj\demo\mapper\UserMapper.xml"/>
mappers>
configuration>
到这里Mybatis的基本框架就算是搭建完成了,但是还需要在测试类运行一些。看看是否可以获取信息
src创建一个新的Test文件夹,并且创建一个与上面相同的文件夹结构
并且在文件夹最底层创建一个mapperTest.java测试类文件
文件路径:SpringBoot\src\test\java\com\zzj\UserMapperTest.java
package com.zzj;
import com.zzj.entity.User;
import com.zzj.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class UserMapperTest {
@Test
public void TestDemo01() throws IOException {
//获取Mybatis配置文件
String resource = "Mybatis-config.xml";
// 构建SqlSessionFactory
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().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();
}
}
点击下图的绿色小图标就可以运行测试类
在下面的控制台可以看见结果已经输出了
到这里Mybatis数据库查询操作就已经完成了。但是还没有实现利用APi接口进行数据库访问的功能,具体会在下节讲解。