面试必备2-快速搭建springboot

面试必备2-快速搭建springboot

这是一个快速配置的springboot项目。包含lombok、springMVC、mybatis、mysql。

1、初始化springboot项目

方法1:使用idea初始化

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pqQEdhq6-1640575976860)(面试必备2-快速搭建springboot.assets/image-20211227100858495.png)]

方法2:使用去官方网站初始化(https://start.spring.io/),产生为压缩包文件

记得勾选Dependencies

Lombok、Spring web、thymeleaf、jdbc api、mybatis framework、mysql driver
方法3:直接导入依赖

<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.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--<scope>runtime</scope>-->
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
2、修改配置文件

application.yml

server:
  port: 8080
 
spring:
  datasource:
    username: 数据库用户名(通常为:root)
    password: 数据库密码(通常为:root)
    url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml # mapper文件所在的类路径
  type-aliases-package: pojo所在的包路径 # 使能够用pojo实体类的简称,不需要写全类名
#showSql
logging:
  level:
    com:
      example:
        mapper : debug
3、设计数据库
CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `passWord` varchar(50) NOT NULL,
  `realName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
4、配置lombok

1)安装Lombok插件

菜单栏File -> Settings -> Plugins,点击”install JetBrains plugin…“,然后输入Lombok搜索后进行安装,安装后会提示重启IDEA。下面是已经安装后的截图。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qIpnVqqg-1640575976862)(面试必备2-快速搭建springboot.assets/image-20211227105736360.png)]

2)配置注解处理器

菜单栏File -> Settings -> Plugins -> Build,Execution,Deployment -> Compiler -> Annotation Processors,勾选Enable annotation processing并保存。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ddjRhqoG-1640575976863)(面试必备2-快速搭建springboot.assets/201408-20191208224231369-1763995475.png)]

5、创建pojo实体

user.java

package com.springboot.pojo;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String userName;
    private String passWord;
    private String realName;
}
6、编写dao层

在以下路径中创建userMapper.xml文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wH6xJ6XV-1640575976864)(面试必备2-快速搭建springboot.assets/image-20211227110157839.png)]

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.springboot.dao.UserMapper">
 
    <resultMap id="BaseResultMap" type="com.example.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>
 
    <select id="方法名" resultType="com.springboot.pojo.User">
        select * from user where id = #{id}
    </select>
 
</mapper>

UserMapper.java

package com.springboot.dao;
 
import com.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface UserMapper {
    User query(int id);
}

在启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan(“com.springboot.dao”)

package com.springboot;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@MapperScan("com.springboot.dao") //扫描的mapper类的包
@SpringBootApplication
public class SpringbootDemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
7、编写service层

UserService.java

package com.springboot.service;
 
import com.springboot.pojo.User;
import com.springboot.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
/**
 * @Author:wjup
 * @Date: 2018/9/26 0026
 * @Time: 15:23
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User query(int id){
        return userMapper.query(id);
    }
}
8、编写controller层
package com.springboot.controller;
 
import com.springboot.pojo.User;
import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/test")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @RequestMapping("getUser/{id}")
    public String GetUser(@PathVariable int id){
        return userService.query(id).toString();
    }
}
9、运行效果

浏览器输入地址:http://localhost:8080/test/getUser/1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值