今天博主讲一下,IDEA创建springboot+mybatis项目的文章。
步骤分别是如下几步:
点击file 选择project 选择spring initializr项目
点击next
填写groupid和artifactId:
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
比如我创建一个项目,我一般会将groupId设置为cn.snowin,cn表示域为中国,snowin是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.zr.testProj打头的,如果有个StudentDao,它的全路径就是cn.snowin.testProj.dao.StudentDao
如果你本机安装java21就选 21 本机安装8就选8 安装17就选17 。
点击next:
开始选择配置项;
如果你想用mysql和mybatis框架选这两个就行。
然后点击next,最后创建好之后:
配置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>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.suncaper</groupId>
<artifactId>spnaims</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spnaims</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-modulith.version>1.1.0</spring-modulith.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-bom</artifactId>
<version>${spring-modulith.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
创建controller dao domain service mapper impl 五个package或者文件夹
首先创建mybatis.xml文件,但是这个文件先不用,可以后期自行配置
然后我们主要配置
application.properties文件
mybatis.type-aliases-package=com.suncaper.spnaims
#mysql驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#远程数据库链接 serverTimezone不可少
#emps为我们的数据库名字
spring.datasource.url=jdbc:mysql://localhost:3306/emps?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#MySQL数据库用户名、密码
spring.datasource.username=root
#自行更改密码
spring.datasource.password=123456
#xml格式的mapper文件位置
#mybatis文件读取配置,很重要哈
mybatis.mapper-locations=classpath:/mapper/*.xml
创建数据库
create database emps;
创建数据表
use emps;
create table student( sno int,sname varchar(20) ,sage int);
数据表生成结构如下:
我们可以自行插入几个数据进行测试。
配置mapper文件:
<?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 = "org.suncaper.spnaims.dao.StudentMapper">
<insert id="insert" parameterType="org.suncaper.spnaims.domain.Student" >
INSERT INTO student (Sno,Sname,Sage) VALUES (#{sNo},#{sName},#{sAge})
</insert>
<select id="select" parameterType="int" resultType="org.suncaper.spnaims.domain.Student">
select * from student
</select>
</mapper>
分别创建如下接口和类文件:
StudentController.class
package org.suncaper.spnaims.controller;
import org.suncaper.spnaims.domain.Student;
import org.suncaper.spnaims.dao.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.suncaper.spnaims.service.IStudentService;
import java.util.ArrayList;
@RestController
public class StudentController {
@Autowired()
private IStudentService studentService;
@RequestMapping("/")
public String index() throws Exception {
studentService.insert(new Student(2, "张三", 18));
return "OK";
}
@RequestMapping("/showdata")
public String showdata() throws Exception {
String s=studentService.select(2);
String data="the sql of student is:";
return s;
}
}
StudentMapper接口文件:
package org.suncaper.spnaims.dao;
import org.suncaper.spnaims.domain.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.ArrayList;
@Mapper
public interface StudentMapper {
void insert(Student student);
void delete(String sNo);
Student selectByNumber(Integer sNo);
void updateName(Student student);
ArrayList<Student> select(int id);
}
Student类:
public class Student {
public int sNo;
public String sName;
public int sAge;
public Student(int sNo, String sName, int sAge) {
this.sNo = sNo;
this.sName = sName;
this.sAge = sAge;
}
public String toString(){
return "id:"+sNo+"name:"+sName+"age:"+sAge;
}
//Getter、Setter方法,我省略了,你在代码中别省略
}
StudentServiceImpl类:
package org.suncaper.spnaims.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.suncaper.spnaims.dao.StudentMapper;
import org.suncaper.spnaims.domain.Student;
import org.suncaper.spnaims.service.IStudentService;
import java.util.ArrayList;
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired()
private StudentMapper studentMapper;
public String select( int id){
ArrayList<Student> students=studentMapper.select(id);
String data="the sql of student is:";
for (Student user:students
) {
data=data+user.toString();
}
return students.toString();
}
public void insert(Student usr){
studentMapper.insert(usr);
}
}
IStudentService接口:
package org.suncaper.spnaims.service;
import org.suncaper.spnaims.domain.Student;
public interface IStudentService {
public String select(int id);
public void insert(Student usr);
}
DemoApplication 主类:
package org.suncaper.spnaims;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
之后我们就可以运行我们的代码了
点击绿色箭头:
然后运行:
测试结果如下: