今天博主讲一下,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文件:
<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>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.5version>
<relativePath/>
parent>
<groupId>org.suncapergroupId>
<artifactId>spnaimsartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>spnaimsname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<spring-modulith.version>1.1.0spring-modulith.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.23version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-apiartifactId>
<version>RELEASEversion>
<scope>compilescope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.modulithgroupId>
<artifactId>spring-modulith-bomartifactId>
<version>${spring-modulith.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.30version>
<scope>providedscope>
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文件:
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);
}
}
之后我们就可以运行我们的代码了