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>
 
 
 
 

 

posted @ 2019-07-03 16:26  txf-0324  阅读(189)  评论(0编辑  收藏  举报