博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IDEA搭建Springboot+SpringMVC+Mybatis+Mysql(详细、易懂)
阅读量:4046 次
发布时间:2019-05-25

本文共 5740 字,大约阅读时间需要 19 分钟。

最详细,最易懂,一步一步执行搭建的框架~

话不多说,直接开始搭建吧~

目录

 


一、创建项目 

  1.点击创建新项目

      

  2.选择Spring Initializr     

   3.填写好项目的相关信息

  4.选择用到的依赖(也可以后期在pom.xml中添加)

  5.选择项目存放位置和设置项目名称

  6.创建成功后项目的初步结构

      

   7.pom.xml中依赖(添加了一点)

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.alibaba
fastjson
1.2.35
org.springframework.boot
spring-boot-starter-web
2.3.1.RELEASE
org.springframework.boot
spring-boot-maven-plugin
src/main/
**/*.xml
src/main/resources
**/*.*

二、修改结构以及创建具体内容

   1.点击File -->  Project Structure..

  2.将main中java文件设为Sources,resources设为Resources,test中java设为Tests。

  3.创建controller、service、dao、entity包

  4.在resources中创建mappers文件来存放mapper.xml配置文件

  5.创建mapper.xml配置文件:右键 --> New --> File

  6.填入配置文件名称 例如:UserMapper.xml

  8.默认的springboot配置文件application是.properties格式,我习惯用.yml格式,所以修改一下

  9.将properties替换为yml

  10.配置application.yml

spring:       datasource:         url: jdbc:mysql://localhost:3306/seven?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai         username: root         password: root         driver-class-name: com.mysql.cj.jdbc.Drivermybatis:  mapper-locations: classpath:/resources/mappers/*.xml  type-aliases-package: com.example.demo.daoserver:    port: 8080    servlet:          context-path: /demo

    11. 配置DemoApplication运行文件

package com.example.demo;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.example.demo.dao")//加上这句,运行项目时候要去扫描mybatis的接口文件public class DemoApplication {	public static void main(String[] args) {		SpringApplication.run(DemoApplication.class, args);	}}

三、连接数据库,使用mybatis,以写上传用户信息的接口为例。

  1.数据库中创建user表。

  2.entity包中创建User实体类

package com.example.demo.entity;public class User {    private int    userID;     //用户ID    private String userName;   //用户名    private int    userAge;    //用户年龄    public User() {    }    public User(int userID, String userName, int userAge) {        this.userID = userID;        this.userName = userName;        this.userAge = userAge;    }    public int getUserID() {        return userID;    }    public void setUserID(int userID) {        this.userID = userID;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public int getUserAge() {        return userAge;    }    public void setUserAge(int userAge) {        this.userAge = userAge;    }    @Override    public String toString() {        return "User{" +                "userID=" + userID +                ", userName='" + userName + '\'' +                ", userAge=" + userAge +                '}';    }}

  3.配置文件UserMapper.xml中

INSERT INTO user VALUES (#{userID,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR}, #{userAge,jdbcType=INTEGER})

  4.dao包中的UserMapper

package com.example.demo.dao;import com.example.demo.entity.User;public interface UserMapper {    int insertUserInfo(User user);}

  5.service中的UserService

package com.example.demo.service; import com.example.demo.dao.UserMapper;import com.example.demo.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class UserService {    @Autowired(required = false)    private UserMapper userMapper;    public int AddUserInfo(int ID,String name,int age) {        User user = new User();        user.setUserID(ID);        user.setUserName(name);        user.setUserAge(age);        int res = userMapper.insertUserInfo(user);        if(res>0)return 1;  //上传成功        else return 0;   //上传失败    }}

  6.controller中的UserController

package com.example.demo.controller;import com.example.demo.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;@Controllerpublic class UserController {    @Autowired    private UserService userService;    //上传用户信息    @GetMapping("/AddUserInfo")    @ResponseBody    public int AddUserInfo(@RequestParam("userID")int userID,                           @RequestParam("userName")String userName,                           @RequestParam("userAge")int userAge){        int res = userService.AddUserInfo(userID,userName,userAge);        return res;    }}

四、调用接口,上传数据

查看数据库user表中,数据也插入了。

转载地址:http://dizci.baihongyu.com/

你可能感兴趣的文章
一篇文章搞懂 HBase 的整体架构
查看>>
HBase 表的数据模型是什么?
查看>>
3 张图搞懂 HBase 的存储原理.md
查看>>
一篇文章搞懂 HBase 的 flush 机制和 compact 机制
查看>>
一篇文章搞懂 HBase 的 region 拆分机制
查看>>
HBase 表的预分区是什么?为什么要预分区?如何预分区?
查看>>
Flume 是什么?Flume 有什么特点?
查看>>
一篇文章搞懂 Flume 的架构设计
查看>>
Flume 是怎么保障可靠性的?
查看>>
Flume 怎样实现数据的断点续传?
查看>>
Flume 如何自定义 Mysql Source?
查看>>
Flume 如何自定义 Mysql Sink?
查看>>
Flume 的可靠性级别有哪些?
查看>>
Sqoop 是什么?Sqoop 有什么特点?
查看>>
Sqoop 的使用场景分析
查看>>
DAGScheduler 是什么?有什么作用?
查看>>
DAGScheduler 是如何划分 Stage 的?
查看>>
TaskScheduler 是什么?有什么作用?
查看>>
一篇文章搞懂 DAGScheduler 的调度流程
查看>>
SparkEnv 是什么?有什么作用?
查看>>