IDEA构建SpringBoot项目

202 阅读1分钟

1、构建SpringBoot项目

2、输入包名,项目名,选择JDK版本

3、添加依赖Spring Web(实际是jar包)

4、输入项目名

5、添加注解

6、启动项目

7、浏览器访问

8、整合Mybatis,连接Mysql,并插入一条数据

8.1 目录结构

8.2 代码

  • SpringBootDemoApplication.java
package com.hebei.example.springbootdemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@ComponentScan("com.hebei.example")
@MapperScan("com.hebei.example.dao.mappers")
@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

  • HelloController.java
package com.hebei.example.controller;

import com.hebei.example.dao.entity.User;
import com.hebei.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    IUserService userService;
    @GetMapping("/test")
    public String test(){
        User user = new User();
        user.setName("Alan");
        userService.insert(user);
        return "Hello Spring Boot";
    }
}
  • User.java
package com.hebei.example.dao.entity;

public class User {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

  • UserMapper.java
package com.hebei.example.dao.mappers;

import com.hebei.example.dao.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {

    int insert(User user);
}

  • IUserService
package com.hebei.example.service;

import com.hebei.example.dao.entity.User;

public interface IUserService {
    int insert(User user);
}

  • UserServiceImpl.java
package com.hebei.example.service.impl;

import com.hebei.example.dao.entity.User;
import com.hebei.example.dao.mappers.UserMapper;
import com.hebei.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }
}

  • application.properties
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.1.103:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC

mybatis.mapper-locations=classpath:*Mapper.xml
  • 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.hebei.example.dao.mappers.UserMapper">
    <resultMap id="BaseResultMap" type="com.hebei.example.dao.entity.User">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
    </resultMap>
    <sql id="Base_Column_List">
        id,name
    </sql>
    <insert id="insert" parameterType="com.hebei.example.dao.entity.User" keyColumn="id" useGeneratedKeys="true">
        insert into user_innodb(name) values(#{name,jdbcType=VARCHAR})
    </insert>
</mapper>
  • pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hebei.example</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-demo</name>
    <description>Demo project for Spring Boot</description>

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

    <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>1.3.2</version>
        </dependency>
        <!--增加-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>