码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+mybatis整合——基础版

时间:2018-04-12 19:58:22      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:src   dao   png   数据库连接   star   catch   select   注意   UI   

我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk

技术分享图片

技术分享图片

pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.20</version>
        </dependency>

 

TestBootController.java

package com.springbootmybatis.demo.controller;

import com.springbootmybatis.demo.entity.Gys;
import com.springbootmybatis.demo.service.GysServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/testboot")
public class TestBootController {

    @RequestMapping("/getgys")
    public Gys getUser() {
        Gys user = new Gys();
        user.setRoleName("test");
        return user;
    }

    @Autowired
    private GysServiceImpl gysService;
    @RequestMapping("/getlist")
    public List<Gys> getlist() {
        List<Gys> list=null;
       try {
        list=gysService.getGysList();
       }catch (Exception e){
           e.printStackTrace();
       }
        return list;
    }
}

ITestDao.java

package com.springbootmybatis.demo.dao;

import com.springbootmybatis.demo.entity.Gys;

import java.util.List;

public interface IGysDao {
    List<Gys> getUserList() throws  Exception;
}

GysServiceImpl.java

package com.springbootmybatis.demo.service;

import com.springbootmybatis.demo.dao.IGysDao;
import com.springbootmybatis.demo.entity.Gys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("gysService")
public class GysServiceImpl {
    @Autowired
    private IGysDao iGysDao;

    public List<Gys> getGysList() throws  Exception{
        return  iGysDao.getUserList();
    }
}

DemoApplication.java

package com.springbootmybatis.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
@MapperScan("com.springbootmybatis.demo.dao")
public class DemoApplication extends SpringBootServletInitializer{

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

Gys.java

package com.springbootmybatis.demo.entity;

public class Gys {
    private String roleName;

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}

Gys.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.springbootmybatis.demo.dao.IGysDao">
    <select id="getUserList" resultType="com.springbootmybatis.demo.entity.Gys">
        SELECT  * FROM  gys;
    </select>
</mapper>

application-dev.yml

mybatis:
  mapper-locations: classpath:mapper/*.xml
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springmvc-mybatis
    username: root
    password: gys
    type: com.alibaba.druid.pool.DruidDataSource
server:
  port: 8082
logging:
  level: debug

application.yml

spring:
  profiles:
    active: dev

 

springboot+mybatis整合——基础版

标签:src   dao   png   数据库连接   star   catch   select   注意   UI   

原文地址:https://www.cnblogs.com/guoyansi19900907/p/8809872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!