欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【个人实习日志】Springboot项目创建

程序员文章站 2022-05-05 15:17:50
...

Springboot项目创建

一、spring项目的创建
File->New->Project
选择Spring Initializr以后选择custom http://start.aliyun.com/点击Next
【个人实习日志】Springboot项目创建
java版本选择8
【个人实习日志】Springboot项目创建
依赖web中选择Spring Web,sql中选择MyBatis Framework和Mysql Driver点击next下一步点击finish
【个人实习日志】Springboot项目创建
【个人实习日志】Springboot项目创建
【个人实习日志】Springboot项目创建
二、spring项目的配置
Maven仓库配置
File->settings->Build->Maven
将maven路径改为下载的Maven,repository改为本地仓库

【个人实习日志】Springboot项目创建
将配置文件改为application.yml,配置数据库信息
【个人实习日志】Springboot项目创建
三、项目测试
文件目录:
【个人实习日志】Springboot项目创建
(一)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>

四、运行展示
【个人实习日志】Springboot项目创建
【个人实习日志】Springboot项目创建

相关标签: 实习