idea创建springboot项目

释放双眼,带上耳机,听听看~!

idea创建springboot项目

  • 打开idea,点击create new project

idea创建springboot项目

  • 选择spring Initializr 选择defalt 点击 next

idea创建springboot项目

  • 输入Group artifact 点击next

idea创建springboot项目

  • 选择web springweb 点击next

idea创建springboot项目

  • 点击next finish

idea创建springboot项目

  • 点击file settings 搜索到maven 设置成自己的maven路径

idea创建springboot项目

  • import changes

idea创建springboot项目

  • 打开SpringbootdemoApplication 运行成功

  • idea创建springboot项目

  • 新建controller 包 ,在包下新建HelloController类,

  • idea创建springboot项目

  • 输入如下代码,运行SpringbootdemoApplication


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1package com.run.springbootdemo.controller;
2
3import org.springframework.web.bind.annotation.RequestMapping;
4import org.springframework.web.bind.annotation.RestController;
5
6/**
7 * @author: zhaohb
8 * @create: 2019-10-22 16:28
9 **/
10
11@RestController
12public class HelloController {
13    @RequestMapping("/hello")
14    public String hello() {
15        return "hello,this is a springboot demo";
16    }
17}
18
19
  • 打开浏览器输入localhost:8080/hello

idea创建springboot项目

整合mybatis

  • 添加依赖


1
2
3
4
5
6
7
8
9
10
11
12
13
1<dependency>
2            <groupId>org.mybatis.spring.boot</groupId>
3            <artifactId>mybatis-spring-boot-starter</artifactId>
4            <version>1.3.2</version>
5        </dependency>
6
7        <dependency>
8            <groupId>mysql</groupId>
9            <artifactId>mysql-connector-java</artifactId>
10            <scope>runtime</scope>
11</dependency>
12
13
  • 把application.properties改为application.yml , 新建application-dev.yml

application.yml


1
2
3
4
5
1spring:
2  profiles:
3    active: dev
4
5

application-dev.yml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1server:
2  port: 8080
3
4spring:
5  datasource:
6    username: root
7    password: 123456
8    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
9    driver-class-name: com.mysql.jdbc.Driver
10
11mybatis:
12  mapper-locations: classpath:mapping/*Mapper.xml
13  type-aliases-package: com.run.springbootdemo.entity
14
15#showSql
16logging:
17  level:
18    com:
19      run:
20        springbootdemo:
21          mapper : debug
22
23
  • 创建springboot数据库,创建user表

idea创建springboot项目

  • 创建如下service,mapper包

idea创建springboot项目

  • 创建UserController、UserService、UserServiceImpl、UserMapper、UserMapper.xml

UserController


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1package com.run.springbootdemo.controller;
2
3import com.run.springbootdemo.service.UserService;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RestController;
7
8import java.util.List;
9import java.util.Map;
10
11/**
12 * @author: zhaohb
13 * @create: 2019-10-22 16:28
14 **/
15
16@RestController
17public class UserController {
18
19    @Autowired
20    private UserService userService;
21
22    @RequestMapping("/listuser")
23    public List<Map<String,Object>> listUser() {
24        return userService.listUser();
25    }
26}
27
28
29

UserService


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1package com.run.springbootdemo.service;
2
3import org.springframework.stereotype.Service;
4
5import java.util.List;
6import java.util.Map;
7
8/**
9 * @author: zhaohb
10 * @create: 2019-10-24 11:35
11 **/
12
13public interface UserService {
14    List<Map<String, Object>> listUser();
15}
16
17
18

UserServiceImpl


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1package com.run.springbootdemo.service.impl;
2
3import com.run.springbootdemo.mapper.UserMapper;
4import com.run.springbootdemo.service.UserService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.stereotype.Service;
7
8import java.util.List;
9import java.util.Map;
10
11/**
12 * @author: zhaohb
13 * @create: 2019-10-24 11:38
14 **/
15@Service
16public class UserServiceImpl implements UserService {
17
18    @Autowired
19    private UserMapper userMapper;
20
21    @Override
22    public List<Map<String, Object>> listUser() {
23        return userMapper.listUser();
24    }
25}
26
27
28

UserMapper


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1package com.run.springbootdemo.mapper;
2
3import org.springframework.stereotype.Repository;
4
5import java.util.List;
6import java.util.Map;
7
8/**
9 * @author: zhaohb
10 * @create: 2019-10-24 11:39
11 **/
12
13@Repository
14public interface UserMapper {
15    List<Map<String, Object>> listUser();
16}
17
18
19

UserMapper.xml


1
2
3
4
5
6
7
8
9
10
11
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3<mapper namespace="com.run.springbootdemo.mapper.UserMapper">
4
5    <select id="listUser" resultType="map">
6        select * from user
7    </select>
8
9</mapper>
10
11
  • 在启动类上加入mapperscan注解

idea创建springboot项目

  • 运行

idea创建springboot项目

给TA打赏
共{{data.count}}人
人已打赏
安全经验

职场中的那些话那些事

2021-9-24 20:41:29

安全经验

MMORPG服务器架构

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索