快速创建一个SpringBoot项目

5 篇文章 1 订阅

快速创建一个SpringBoot项目

1、idea创建maven项目

在这里插入图片描述

在这里插入图片描述

  整体架构如下图所示

在这里插入图片描述

2、引入相关依赖

在这里插入图片描述
  如果需要在 linux 上部署启动,加上:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3、编写类

  编写controller、service、dao、entity、mapper的xml文件夹如下

在这里插入图片描述

  编写启动类ApplicationContext、以及application.yml配置文件

在这里插入图片描述

  它们的注解、配置具体如下:

在这里插入图片描述

在这里插入图片描述

  接下来,编写controller、server、mapper、xml文件。如下:

在这里插入图片描述

emsp; UserController类:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/getUser")
    public User getUser(Integer id) {
        return userService.getUserById(id);
    }
}

  UserService、UserServiceImpl

public interface UserService {

    User getUserById(Integer id);

}
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public User getUserById(Integer id) {
        User user = userDao.getUserById(id);

        return user;
    }

}

  UserDao

public interface UserDao {

    User getUserById(Integer id);

}

  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.zhihong.dao.UserDao" >
    <resultMap id="BaseResultMap" type="com.zhihong.entity.User" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="gender" property="gender" jdbcType="VARCHAR" />
        <result column="other_id" property="other_id" jdbcType="INTEGER" />
        <result column="modify_time" property="modify_time" jdbcType="TIMESTAMP" />
    </resultMap>
    <sql id="Base_Column_List" >
        id, name, age, gender, other_id, modify_time
    </sql>
    <select id="getUserById" resultMap="BaseResultMap">
        select * from t_user where id = #{id}
    </select>
</mapper>

  User实体类

public class User {

    private Integer id;

    private String name;

    private Integer age;

    private Character gender;

    private Integer other_id;

    private Date modify_time;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Character getGender() {
        return gender;
    }

    public void setGender(Character gender) {
        this.gender = gender;
    }

    public Integer getOther_id() {
        return other_id;
    }

    public void setOther_id(Integer other_id) {
        this.other_id = other_id;
    }

    public Date getModify_time() {
        return modify_time;
    }

    public void setModify_time(Date modify_time) {
        this.modify_time = modify_time;
    }
}

  mysql建表语句:

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  `age` int(11) NOT NULL,
  `gender` char(1) NOT NULL,
  `other_id` int(11) NOT NULL,
  `modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`,`other_id`),
  KEY `idx_other_id` (`other_id`),
  KEY `idx_modify_time_age` (`modify_time`,`age`)
) ENGINE=InnoDB AUTO_INCREMENT=7000011 DEFAULT CHARSET=utf8

接下来,运行启动类,搞定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值