快速搭建SpringBoot

快速搭建SpringBoot

1.创建工程的步骤参照1-5步

2.按如图所示结构创建这些文件
快速搭建SpringBoot_第1张图片

3.pom.xml


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.8.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.springbootgroupId>
    <artifactId>test-springbootartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>test-springbootname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.8version>
        dependency>
        <dependency>
            <groupId>javax.persistencegroupId>
            <artifactId>javax.persistence-apiartifactId>
            <version>2.2version>
        dependency>
        
        
        
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>2.0.2version>
        dependency>

    dependencies>


project>

4.com.springboot包

BootApplication.java

package com.springboot;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

/**
 *
 */
@SpringBootApplication
@MapperScan({"com.springboot.mapper"})
public class BootApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }
}

5.com.springboot.controller包

UserController.java

package com.springboot.controller;

import com.springboot.pojo.User;
import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("hello")
    public String hello() {

        return "你好";
    }

    @RequestMapping("user/{id}")
    @ResponseBody
    public String getUser(@PathVariable("id") long id /*, @RequestParam("name") String name, HttpServletRequest request*/) {
        //request.getParameter("username");
        User user = userService.queryById(id);
        return user.toString();
    }


}

6.com.springboot.mapper包

UserMapper.java

package com.springboot.mapper;

import com.springboot.pojo.User;
import tk.mybatis.mapper.common.Mapper;

/**
 *
 */

public interface UserMapper extends Mapper<User> {

}

7.com.springboot.pojo包

User.java

package com.springboot.pojo;

import lombok.Data;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

/**
 *
 */
@Table(name = "user")
@Data
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Date birthday;
    private int gender;
    private Date created;
    private Date updated;
}


8.com.springboot.service包

UserService.java

package com.springboot.service;

import com.springboot.mapper.UserMapper;
import com.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User queryById(long id){
        return this.userMapper.selectByPrimaryKey(id);
    }

    @Transactional  //事务注解
    public void deleteById(Long id){
        this.userMapper.deleteByPrimaryKey(id);
    }
}


9.resources文件夹

application.yml

logging:
  level:
    com.springboot: debug
server:
  port: 8088

spring:
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/crm?serverTimezone=UTC
    type: com.mysql.cj.jdbc.MysqlDataSource
mybatis:
  type-aliases-package: com.springboot.pojo
#  mapper-locations: classpath: mappers/*.xml
#  configuration:
#    map-underscore-to-camel-case: true

10.注入数据库sql

CREATE TABLE `user` (
  `id` int(11) DEFAULT NULL,
  `user_name` varchar(60) DEFAULT NULL,
  `password` varchar(60) DEFAULT NULL,
  `name` varchar(60) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `user` (`id`, `user_name`, `password`, `name`, `birthday`, `gender`, `created`, `updated`) values('1','zhangsan','123456','张三','2019-06-10','1','2019-06-10 10:03:52','2019-06-10 10:03:55');
insert into `user` (`id`, `user_name`, `password`, `name`, `birthday`, `gender`, `created`, `updated`) values('2','lisi','123456','李四','2019-06-03','0','2019-06-09 10:04:17','2019-06-12 10:04:21');

11.运行,在地址栏输入http://localhost:8088/user/1显示数据库内容则成功
在这里插入图片描述

你可能感兴趣的:(架构)