【个人实习日志】Springboot项目创建
程序员文章站
2022-05-05 15:17:50
...
Springboot项目创建
一、spring项目的创建
File->New->Project
选择Spring Initializr以后选择custom http://start.aliyun.com/点击Next
java版本选择8
依赖web中选择Spring Web,sql中选择MyBatis Framework和Mysql Driver点击next下一步点击finish
二、spring项目的配置
Maven仓库配置
File->settings->Build->Maven
将maven路径改为下载的Maven,repository改为本地仓库
将配置文件改为application.yml,配置数据库信息
三、项目测试
文件目录:
(一)bean包中新建User实体类
User.java
package com.wzx.bean;
public class User {
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
(二)dao包中新建UserDao
UserDao.java
package com.wzx.dao;
import com.wzx.bean.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserDao {
List<User> findAll();
}
(三)service包中新建UserService
UserService.java
package com.wzx.service;
import com.wzx.bean.User;
import com.wzx.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> findAll(){
return userDao.findAll();
}
}
(四)controller包中新建HelloSpringboot
HelloSpringboot.java
package com.wzx.service;
import com.wzx.bean.User;
import com.wzx.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> findAll(){
return userDao.findAll();
}
}
(五)启动类
Springboot3Application.java
package com.wzx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot3Application {
public static void main(String[] args) {
SpringApplication.run(Springboot3Application.class,args);
}
}
(六)resource下新建mapper包,在mapper包中新建UserMapper.xml
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.wzx.dao.UserDao">
<select id="findAll" resultType="user">
select * from tb_user
</select>
</mapper>
(七)配置文件
porm.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot3</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四、运行展示
推荐阅读
-
eclipse创建springboot项目
-
SpringBoot 2.x (1):手动创建项目与自动创建项目
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
SpringBoot技术栈搭建个人博客【项目准备】
-
(三)创建基于maven的javaFX+springboot项目创建
-
IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能
-
使用iDea创建一个Springboot项目
-
(四)创建基于maven的javaFX+springboot项目,用户界面与后台逻辑分离方式
-
SpringBoot学习笔记(创建SpringBoot项目)
-
idea创建springboot项目,一直在reading pom.xml