springboot +mybatis +mysql demo搭建

回到首页☞

先从基础的来,这个非常重要,但是实际项目中不会这么用,因为真正的项目都要用到连接池,从基础主键逐渐完善。

1、创建工程

特别说明如果做菊厂都知道,基本毫无权限,没有外网所有必须抛弃Spring initialize 安装。

1.1 创建maven工程

不用选择任何模板
springboot +mybatis +mysql demo搭建_第1张图片
取个名字
springboot +mybatis +mysql demo搭建_第2张图片
下一步
springboot +mybatis +mysql demo搭建_第3张图片

生成一个空的maven工程
springboot +mybatis +mysql demo搭建_第4张图片

1.2、配置pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wht.springboot.demo</groupId>
    <artifactId>com.wht.springboot.demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>mySpringbootDemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8
        </project.reporting.outputEncoding>
        <mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version>
        <mysql-connector.version>5.1.39</mysql-connector.version>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector.version}</version>
        </dependency>

        <!--线程池-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

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

</project>

现在已经把连接池引进来了,后面再看怎么用。

1.3、 配置application.properties文件

有两类 yml或者properties文件,这里不纠结先用传统的,放到resource下

server.context-path=/
server.port=9090
server.tomcat.uri-encoding=UTF-8
spring.datasource.url=jdbc:mysql://localhost:3306/dev?useUnicode=true&characterEncoding=utf8
spring.datasource.username=dev
spring.datasource.password=hcgk*2020
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

2、简单运行springboot

现在配置有了,但是工程目录还不完善

2.1、创建启动类–Application

JVM只有一个入口main方法,springboot特别的地方就是main方法自己提供,然后去调用tomcat等容器。

package com.wht.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author JDIT
 */
@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

2.2、搞一个测试controller–TestController

package com.wht.springboot.demo.controller;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JDIT
 */

@RestController
@EnableAutoConfiguration
@RequestMapping("/test")
public class TestController {
  @RequestMapping("sayHello")
  public String sayHello() {
    return "Hello World";
  }
}

2.3 运行启动类

浏览器访问地址:http://localhost:9090/test/sayHello
运行正常。

3、加入mybatis关联数据库

3.1、增加mybatis-config.xml

表都创建好了,下面配置只是demo,别名可以不用。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 配置文件的根元素 -->
<configuration>
    <!--配置全局属性-->
    <settings>
        <!--使用jdbc的getGeneratedKeys获取数据库自增主键值-->
        <setting name="useGeneratedKeys" value="true"/>
        <!--使用列标签替换列别名 默认未true-->
        <setting name="useColumnLabel" value="true"/>
        <!--开启驼峰式命名转换:Table{create_time} -> Entity{createTime}-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>
</configuration>

3.2、创建vo

引入了lombok,使用注解后不用再写什么get、set无聊的方法。

package com.wht.springboot.demo.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * @author JDIT
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmployeeVO {
  private String eName;
  private Date hireDate;
  private Integer id;
  private String remark;
  private Double salary;
}


3.3、创建mapper接口

package com.wht.springboot.demo.mapper;

import com.wht.springboot.demo.vo.EmployeeVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author JDIT
 */
@Mapper
@Component
public interface EmployeeMapper {
  /**
   * 查询所有员工信息
   * @return
   */
  List<EmployeeVO> findEmployeeList();
}

3.4、创建mapper.xml配置文件

mySpringboot\src\main\resources\mybatis\mapper\EmployeeMapper.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.wht.springboot.demo.mapper.EmployeeMapper">

    <select id="findEmployeeList"  resultType="com.wht.springboot.demo.vo.EmployeeVO">
      select * from t_emp;
    </select>

</mapper>

3.5、增加接口

package com.wht.springboot.demo.controller;

import com.wht.springboot.demo.mapper.EmployeeMapper;
import com.wht.springboot.demo.vo.EmployeeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author JDIT
 */

@RestController
@EnableAutoConfiguration
@RequestMapping("/test")
public class TestController {
  @Autowired
  private EmployeeMapper employeeMapper;
  @RequestMapping("sayHello")
  public String sayHello() {
    return "Hello World";
  }
  @RequestMapping("findEmployeeList")
  public List<EmployeeVO> findEmployeeList() {
    return employeeMapper.findEmployeeList();
  }
}

3.6 运行调试

http://localhost:9090/test/findEmployeeList

[
    {
        "hireDate": null,
        "id": 1,
        "remark": "尝试刷s新",
        "salary": 20000,
        "ename": "红海"
    },
    {
        "hireDate": null,
        "id": 2,
        "remark": "背调信息",
        "salary": 20000,
        "ename": "昆仑奴"
    },
    {
        "hireDate": null,
        "id": 3,
        "remark": null,
        "salary": 18555,
        "ename": "德纳西亚"
    },
……
]

4、小结

  • 初步了解springboot的搭建和运行机制。
  • 初步了解springboot和mybatis的结合使用配置方式。
  • 实际项目中事务管理是个核心,接口和不可能是裸调,总会有各种安全认证,传输加密,防止渗透入侵等工作。

回到首页☞

你可能感兴趣的:(spring生态,idea,springboot)