从无到有,手把手教你用Maven怒草一个SpringBoot项目出来

Posted 小废柴的成长之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从无到有,手把手教你用Maven怒草一个SpringBoot项目出来相关的知识,希望对你有一定的参考价值。

1、使用Navicat 新建一个数据库

 

 

2、新建一个数据表

CREATE TABLE t_user(
id int(6) PRIMARY KEY auto_increment,
name VARCHAR(40),
age int(3),
bir TIMESTAMP
)

 

3、新建一个project,file->new->project
选择maven,webapp

 

 

填写项目组织和项目名

 

 

下面是你配置好的maven仓库

 

 

下一步不用管,finish

 

 

 

 

4、修改pom文件

<?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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.jackson</groupId>
<artifactId>vue_cli</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>vue_cli Maven Webapp</name>
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

</dependencies>

 

<build>

</build>


</project>

5、导入项目依赖
<dependencies>

<!--SpringBoot的web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--引入mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>

<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>

<!--druid驱动-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.13</version>
</dependency>

</dependencies>

 

6、修改项目结构
在main新建java文件夹,刷新maven项目

 

 

 

 

干掉整个webapp

 

 

新建springboot配置文件

 

 

java文件夹中构建主入口类

 

 

手撸启动类代码

package com.jackson;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @Author Jackson
* @Date 2020-05-02 19:41
* @Description
**/
@SpringBootApplication
public class VuecliApplication {
public static void main(String[] args) {
SpringApplication.run(VuecliApplication.class, args);
}
}

 

手撸配置文件

spring.application.name=vue
server.port=8989
server.servlet.context-path=/vue

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/vue?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=梵蒂冈

mybatis.mapper-locations=classpath:com/jackson/mapper/*.xml
mybatis.type-aliases-package=com.jackson.entity
comtroll
logging.level.root=info
logging.level.com.jackson.dao=debug

 

新建四个包结构

 

 

resources新建mapper配置文佳的位置

 

 

7、新建实体类

package com.jackson.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.Date;

/**
* @Author Jackson
* @Date 2020-05-02 19:58
* @Description
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Accessors(chain=true)
public class User {

private String id;

private String name;

private Integer age;

@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GTM+8")
private Date bir;
}

 


8、新建dao接口

package com.jackson.dao;

import com.jackson.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserDao {

void save(User user);

void update(User user);

void delete(User user);

List<User> findAll();

User findById(String id);

}

 


9、新建一个mapper模板

用新建的模板新建一个mapper文件,手撸mapper文件,添加的时候key自动生成

<?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.jackson.dao.UserDao">

<insert id="save" parameterType="com.jackson.entity.User" useGeneratedKeys="true" keyProperty="id">
insert into t_user values (#{id}, #{name}, #{age}, #{bir})
</insert>

<update id="update" parameterType="com.jackson.entity.User">
update t_user
set name = #{name}, age = #{age}, bir = #{bir}
where id = #{id}
</update>

<select id="findAll" resultType="com.jackson.entity.User">
select id, name, age, bir from t_user
</select>

<delete id="delete" parameterType="String">
delete from t_user where id = #{id}
</delete>

<select id="findById" parameterType="String" resultType="com.jackson.entity.User">
select id, name, age, bir from t_user where id = #{id}
</select>

</mapper>

 


10、新建service接口

package com.jackson.service;

import com.jackson.entity.User;

import java.util.List;

public interface UserService {

void save(User user);

void update(User user);

void delete(String id);

List<User> findAll();

User findById(String id);

}

 


11、接口的实现

package com.jackson.service;

import com.jackson.dao.UserDao;
import com.jackson.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @Author Jackson
* @Date 2020-05-02 20:37
* @Description
**/
@Service
@Transactional
public class UserServiceImpl implements UserService {

@Autowired
private UserDao userDao;

@Override
public void save(User user) {
userDao.save(user);
}

@Override
public void update(User user) {
userDao.update(user);
}

@Override
public void delete(String id) {
userDao.delete(id);
}

@Override
public List<User> findAll() {
return userDao.findAll();
}

@Override
public User findById(String id) {
return userDao.findById(id);
}

}

 


12、手撸controller

package com.jackson.controller;

import com.jackson.entity.User;
import com.jackson.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author Jackson
* @Date 2020-05-02 20:45
* @Description
**/
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {

@Autowired
private UserService userService;

/**
* 查询所有数据
* @param page
* @param rows
* @return
*/
@GetMapping("findAll")
public Map<String, Object> findAll(Integer page, Integer rows) {
HashMap<String, Object> map = new HashMap<>();
List<User> results = userService.findAll();
map.put("total", 10);
map.put("totalPage", 1);
map.put("page", page);
map.put("results", results);
return map;
}

/**
* 添加用户
* @param user
* @return
*/
@PostMapping("add")
public Map<String, Object> add(@RequestBody User user) {
HashMap<String, Object> map = new HashMap<>();
try {
userService.save(user);
map.put("success", true);
map.put("msg", "添加用户信息成功");
} catch (Exception e) {
e.printStackTrace();
map.put("success", false);
map.put("msg", "添加用户失败:" + e.getMessage());
}
return map;
}

/**
* 删除用户
* @param id
* @return
*/
@GetMapping("delete")
public Map<String, Object> delete(String id) {
HashMap<String, Object> map = new HashMap<>();
try {
userService.delete(id);
map.put("success", true);
map.put("msg", "删除用户信息成功");
} catch (Exception e) {
e.printStackTrace();
map.put("success", false);
map.put("msg", "删除用户失败:" + e.getMessage());
}
return map;
}

/**
* 查询一条信息
* @param id
* @return
*/
@GetMapping("findOne")
public User findOne(String id) {
return userService.findById(id);
}

/**
* 修改用户
* @param user
* @return
*/
@PostMapping("update")
public Map<String, Object> update(@RequestBody User user) {
HashMap<String, Object> map = new HashMap<>();
try {
userService.update(user);
map.put("success", true);
map.put("msg", "修改用户信息成功");
} catch (Exception e) {
e.printStackTrace();
map.put("success", false);
map.put("msg", "修改用户失败:" + e.getMessage());
}
return map;
}
}

 

引用:哔哩哔哩,编程不良人up主

 

以上是关于从无到有,手把手教你用Maven怒草一个SpringBoot项目出来的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你用Java实现一套简单的鉴权服务(SpringBoot,SSM)(未完待续...)

太棒了 | 手把手教你用Python做一个 “举牌小人” 生成器!

手把手教你用git

太棒了 | 手把手教你用Python做一个 “举牌小人” 生成器!

手把手教你用C语言编写一个哈希表

手把手教你用npm发布一个包,详细教程