spring boot+mybatis 简单的应用

阅读更多
项目结构

spring boot+mybatis 简单的应用_第1张图片

pom.xml


    4.0.0

    springboot
    springboot
    1.0-SNAPSHOT
    war

    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
    

    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
            1.0.18
        
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
        
            com.alibaba
            fastjson
            1.2.21
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application.yml
server:
  port : 8080
  context-path: /test

spring:
  mvc:
    view:
      prefix: classpath:/templates/
      suffix: .html

  datasource:
    url: jdbc:mysql://localhost:3306/study
    username: root
    password: qq123456
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

Application
package com.study.hgf;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by hasee on 2017/6/25.
 */
@EnableAutoConfiguration
@ComponentScan
@MapperScan("com.study.hgf.dao.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

User
package com.study.hgf.dao.bo;

/**
 * Created by hasee on 2017/8/14.
 */

public class User {
    private Integer id_;
    private String name_;
    private String pass_;
    private Integer age_;

    public Integer getId_() {
        return id_;
    }

    public void setId_(Integer id_) {
        this.id_ = id_;
    }

    public String getName_() {
        return name_;
    }

    public void setName_(String name_) {
        this.name_ = name_;
    }

    public String getPass_() {
        return pass_;
    }

    public void setPass_(String pass_) {
        this.pass_ = pass_;
    }

    public Integer getAge_() {
        return age_;
    }

    public void setAge_(Integer age_) {
        this.age_ = age_;
    }
}

UserMapper
package com.study.hgf.dao.mapper;

import com.study.hgf.dao.bo.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by hasee on 2017/8/14.
 */
public interface UserMapper {
    @Select("select id_,name_,pass_,age_ from users")
    public List getUserList();
}

UserService
package com.study.hgf.service;

import com.study.hgf.dao.bo.User;
import com.study.hgf.dao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by hasee on 2017/8/14.
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List getUserList(){
        return userMapper.getUserList();
    }
}

LoginController
package com.study.hgf.controller;

import com.alibaba.fastjson.JSON;
import com.study.hgf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by hasee on 2017/6/25.
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping("/")
    @ResponseBody
    public String home(){
        return JSON.toJSONString(userService.getUserList());
    }
}

运行结果


  • spring boot+mybatis 简单的应用_第2张图片
  • 大小: 24.5 KB
  • spring boot+mybatis 简单的应用_第3张图片
  • 大小: 2.8 KB
  • 查看图片附件

你可能感兴趣的:(spring boot+mybatis 简单的应用)