springboot+mybatis项目搭建

Posted txf0324

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot+mybatis项目搭建相关的知识,希望对你有一定的参考价值。

 

一.idea创建一个springBoot项目

技术图片

技术图片

技术图片

 

二.创建的目录结构

技术图片

技术图片

 

三.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 http://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.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
   
</parent>
    <groupId>com.example</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo</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-jersey</artifactId>
        </dependency>
        <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>

        <!--添加mybatis依赖-->
       
<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

        <!--添加mysql依赖-->
       
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>

    </dependencies>

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

</project>

四.application.properties文件#修改tomcat端口号

server.port=8088
#配制mysql数据库映射
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yz?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=yzltz_0324
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#配制mapper映射文件
mybatis.mapper-locations=classpath:/mappers/*.xml

 

五.SpringbootdemoApplication.java启动文件package com.example.springbootdemo;import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
@MapperScan(basePackages = "com.example.springbootdemo.mapper.SpringBootTestMapper")
public class SpringbootdemoApplication extends SpringBootServletInitializer
    public static void main(String[] args)
        SpringApplication.run(SpringbootdemoApplication.class, args);
   


六.Controllerimport java.util.List;



@RestController
public class SpringBootTest


    @RequestMapping("/home")
    public String home()
        return "Hello Spring Boot!";
   

    @Autowired
    private SpringBootTestService springBootTestService;

    @RequestMapping("/testList")
    public List<SpringBootTestPojo> getTestList()
        List<SpringBootTestPojo> list=springBootTestService.getTestList();
        return list;
    ;

七.Service

接口

public interface SpringBootTestService 
    public List<SpringBootTestPojo> getTestList();

 

实现类


import java.util.List;

@Service

public class SpringBootTestServiceImpl implements SpringBootTestService 
    @Autowired
    private SpringBootTestMapper springBootTestMapper;
    @Override
    public List<SpringBootTestPojo> getTestList()
        List<SpringBootTestPojo> list=springBootTestMapper.getTestList();
        return list;
   


八.Mapper

import java.util.List;
@Mapper
public interface SpringBootTestMapper
    public List<SpringBootTestPojo> getTestList();


九.Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.springbootdemo.mapper.SpringBootTestMapper">
    <select id="getTestList" resultType="com.example.springbootdemo.pojo.SpringBootTestPojo">
        SELECT * FROM `tb_springboot`
    </select>
</mapper>
 
 
 
 

 

以上是关于springboot+mybatis项目搭建的主要内容,如果未能解决你的问题,请参考以下文章

电商项目实战——SpringBoot+MyBatis搭建基本骨架

使用idea+springboot+Mybatis搭建web项目

idea+springboot+Mybatis搭建web项目

IDEA 搭建SpringBoot项目:SpringBoot + MyBatis + Druid +MyBatis-Generator

SpringBoot+Mybatis多模块(module)项目搭建教程

springboot+mybatis项目搭建