赞
踩
本文章以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('admin123@qq.com','张三','admin123');
insert into user value('admin456@qq.com','李四','admin456');
insert into user value('admin789@qq.com','王五','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.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>
静态资源导出,,不管是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>
一、二小节的配置完成后,点击一些右上角带M的小图标就可以了
如果没有那个小图标,可以点击右侧的Maven里面的Install
这里是完整的pom.xml配置代码
文件路径:SpringBoot\pom.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 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>
在目录结构中创建一个resource文件夹,并在文件夹下创建一个Mybatis-config.xml文件
注意配置driver、url、username、password等信息
文件路径:SpringBoot\src\main\resources\Mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!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>
<!-- 这里的mappers因mapper还没有开始编写,先暂时注释掉-->
<!-- <mappers>-->
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
<!-- </mappers>-->
</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
<?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.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
<?xml version="1.0" encoding="UTF-8" ?>
<!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还没有开始编写,先暂时注释掉-->
<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接口进行数据库访问的功能,具体会在下节讲解。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。