创建springboot项目(mybatics)
创建springboot项目(mybatics)
1.新建springboot项目
选择springboot输入基本项目信息
蓝色区域选择maven该处为构建方式,maven好上手也最常用,JDK选择17-24,Java必须和你所选jdk版本相同,负责报工件jdk错误,打包方式,后台服务打jar包偏多,带前段jsp打war包(后面可以切换)
接下来选择spring版本以稳定版3.5.5为例添加如下插件
出现这个选择跳过,先配置maven
点击设置
构建,执行,部署——构建工具——maven
解压压缩包将全部文件放在英文目录内
将maven3.9.9添置maven主目录,用户配置文件选择maven3.9.9——conf——setting.xml,本地仓库选择压缩包中repo文件夹,如果黑色无法更改,先勾选后面重写
确定完找到侧边栏
图标点击左上角重新加载
找到src中resources中application的启动文件,默认扩展名properties,个人建议用yml
yml代码如下
server:
# 配置服务端口
port: 8080
spring:
application:
# 配置服务名称
name: spring
# 数据库连接信息
datasource:
# 修改数据库连接参数并添加权限验证参数
url: jdbc:mysql://127.0.0.1:3306/数据库名?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置mybatis
mybatis:
# 配置映射文件位置
mapper-locations: classpath:mapper/*.xml
demo:
abc:
application:
name: ${spring.application.name}
version: ${JAVA_HOME}
接下来在resources文件夹下创建mapper文件夹并找到Java下该文件夹也创建mapper文件夹
然后打开idea插件市场安装mybatisX
然后打开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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
//配置spring
<groupId>com.example</groupId>//项目基本信息的工件名
<artifactId>spring</artifactId>//项目id约等于项目名
<version>0.0.1-SNAPSHOT</version>//代码版本
<name>spring</name>
<description>spring</description>
<url/>//从这开始
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>//到这里暂时用不到,不用理会
<properties>
<java.version>17</java.version>//项目Java版本
</properties>
<dependencies>
//这里就是上面选的插件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>//JDBC插件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>//spring主体
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>//mysql驱动
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>//高效代码简写插件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>//spring测试工件
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
//新手勿动更改此处会导致无法构建
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
接下来找到static文件夹
在这里写前端html,css,js文件
在java下项目名文件夹下创建Config文件夹里创建MybiticsConfig类
``
/*
*
* * Copyright (c) 2025, fengliang477
* * All rights reserved.
* *
* * Redistribution and use in source and binary forms, with or without
* * modification, are permitted provided that the following conditions are met:
* * * Redistributions of source code must retain the above copyright
* * notice, this list of conditions and the following disclaimer.
* * * Redistributions in binary form must reproduce the above copyright
* * notice, this list of conditions and the following disclaimer in the
* * documentation and/or other materials provided with the distribution.
* * * Neither the name of the fengliang nor the
* * names of its contributors may be used to endorse or promote products
* * derived from this software without specific prior written permission.
* *
* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* * ANY AND EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* * ARE DISCLAIMED. IN NO EVENT SHALL fengliang BE LIABLE FOR ANY
* * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT THE OF USE OF
* * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package 你的包名;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
public class MybaticsConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
try {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
// 扫描mapper文件
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml");
sessionFactory.setMapperLocations(resources);
return sessionFactory.getObject();
} catch (IOException e) {
// 修改此处:使用标准IOException替代已移除的NestedIOException
throw new IOException("Failed to create SqlSessionFactory: " + e.getMessage(), e);
}
}
}
找到yml文件
点击运行出现如下日志表示项目启动成功
这样我们的springboot项目就大功告成啦接下来就是创建链接数据库,渲染数据到前端啦
创建springboot项目(mybatics)
https://fengliangit.cn:9999/archives/chuang-jian-springbootxiang-mu-mybatics