maven mysql spring boot

<?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>cn.ccpg.im</groupId>
    <artifactId>springBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springBoot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>
        <!-- mybatis -->

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>

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


</project>

2,yml配置

spring:
  datasource:
   url: jdbc:mysql://localhost:3306/jasontest
   username: root
   password: password
   driverClassName: com.mysql.jdbc.Driver


mybatis:
    mapperLocations: classpath*:/mapping/*.xml

3.mapping

<?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="cn.ccpg.springboot.dome.mapper.TestMapper">

    <select id="getUsername"  parameterType="int" resultType="java.lang.String">
           SELECT u.name FROM  `USER` u where u.id=#{id}
    </select>

</mapper>

4  java 文件

package cn.ccpg.springboot.dome.controller;

import cn.ccpg.springboot.dome.mapper.TestMapper;
import cn.ccpg.springboot.dome.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2018/7/31.
 */
@RestController
public class TestController {

    @Resource
    private TestService testService;

    @RequestMapping(value = "/getSpringBoot/{id}")
    public String  getSpringBoot(@PathVariable("id") int id){
     return testService.getName(id);
    }

}

package cn.ccpg.springboot;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("cn.ccpg.springboot.dome.mapper")
public class Application {

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

package cn.ccpg.springboot.dome.mapper;

/**
 * Created by lenovo on 2018/7/31.
 */
public interface TestMapper {
    public String  getUsername(int  id);

}

package cn.ccpg.springboot.dome.service.impl;

import cn.ccpg.springboot.dome.mapper.TestMapper;
import cn.ccpg.springboot.dome.service.TestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2018/7/31.
 */
@Service
public class TestServiceImpl implements TestService {

    @Resource
    private  TestMapper testMapper;

    @Override
    public String getName(int id) {
        return testMapper.getUsername(id) ;
    }
}

package cn.ccpg.springboot.dome.service;


import org.springframework.stereotype.Service;

/**
 * Created by lenovo on 2018/7/31.
 */
public interface TestService {
    public String getName(int id);
}

package cn.ccpg.springboot.dome.mapper;

/**
 * Created by lenovo on 2018/7/31.
 */
public interface TestMapper {
    public String  getUsername(int  id);

}

猜你喜欢

转载自blog.csdn.net/www5256246/article/details/81480541