美文网首页
SpringBoot+MySQL简单搭建

SpringBoot+MySQL简单搭建

作者: 懒眉 | 来源:发表于2019-03-06 15:19 被阅读0次

    一. 简单项目搭建

    1. 右键目录空白new project选择Maven Project
    1.png
    2. 创建一个简单的项目
    2.png
    3. Group IdArtifact IdCompiler Level填写

    Group IdArtifact Id是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。一般分为多个段,常用的为两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。列如tomcat:这个项目的groupIdorg.apache,它的域是org(因为tomcat是非营利项目),公司名称是apacheartigactIdtomcat
    Artifact Id : 项目的名称
    Compiler Level : 选择jdk版本

    3.png
    4. 点击完成
    4.png
    5. pom文件
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.tkb.settle</groupId>
      <artifactId>NCI_TKB_SETTLE_SSMK</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    
    • 添加parent
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    
    • 添加web支持
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    
    • 添加插件
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    

    添加之后:

    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.tkb.settle</groupId>
      <artifactId>NCI_TKB_SETTLE_SSMK</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
      </parent>
      <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
          </plugin>
          <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>
    
    6.创建Application.java类
    5.png
    package controller;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    //Spring Boot项目的核心注解,主要目的是开启自动配置
    @SpringBootApplication 
    @Controller
    public class Application {
        @RequestMapping("/hello")
        @ResponseBody
        public String hello() {
            return "hello world";
        }
        //应用入口
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    7. 右键Run as
    6.png
    8. 浏览器输入localhost:8080/hello回车测试效果
    8.png

    二. 添加MySQL支持

    1. pom文件导入依赖包
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!--mybatis-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    
    2. 创建测试表,此处省略过程
    8.png
    3. 添加资源配置文件application.properties
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=12345678
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    4. 建立MySQL业务流程
    10.png
    Application.java
    //Spring Boot项目的核心注解,主要目的是开启自动配置
    @MapperScan("com.tkb.demo.dao") 
    @SpringBootApplication 
    @Controller
    public class Application {
        @RequestMapping("/hello")
        @ResponseBody
        public String hello() {
            return "hello world";
        }
    
        //应用入口
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    TestController.java

    @RestController
    @EnableAutoConfiguration
    public class TestController {
        @Resource
        private UserTestService service;
    
        @RequestMapping("/getUser/{id}")
        @ResponseBody
        String getUser(@PathVariable Long id) {
            System.out.println("接收到浏览器的id为:" + id);
            return service.getById(id).toString();
        }
    }
    

    UserMapper.java

    @Mapper
    public interface UserMapper {
        
        @Select("select id,name,age from usertest where id=#{id}")
        User getById(@Param("id") Long id);
    }
    

    UserTestService.java

    @Service
    public class UserTestService {
        @Autowired
        private UserMapper userMapper;
        
        public User getById(long id){
            return   userMapper.getById(id);
        }
    }
    

    启动之后浏览器输入http://localhost:8080/getUser/1

    11.png
    参考

    SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目
    SpringBoot整合Mybatis完整详细版

    相关文章

      网友评论

          本文标题:SpringBoot+MySQL简单搭建

          本文链接:https://www.haomeiwen.com/subject/zrpkuqtx.html