首页 > Java > java教程 > 正文

Java中SpringBoot与2.SpringBoot整合Mybatis的实例

黄舟
发布: 2017-09-16 10:08:13
原创
2147 人浏览过

一、创建Springboot的配置文件:application.properties,并添加MyBatis依赖

SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序:


  1. 当前目录下的/config子目录中

  2. 当前目录中

  3. 一个 classpath 包下的 /config 目录中

  4. classpath 根目录中

 大家根据自己习惯来即可。

(1)application.properties 文件配置如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1/test
spring.datasource.username=root
spring.datasource.password=root
登录后复制

(2)添加 MyBatis 以及 MYSQL


        <!-- SpringBoot - MyBatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>
        <!-- Mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
登录后复制

二、创建POJO、Mapper、Service 以及 Controler

(1)User.java


package org.rcddup.app.domain;public class User {    
private Long id;    
private String name;    
private Short age;    
private Short status;    
public Long getId() {        
return id;
    }    
    public void setId(Long id) {        
    this.id = id;
    }    
    public String getName() {        
    return name;
    }    
    public void setName(String name) {        
    this.name = name;
    }    
    public Short getAge() {        
    return age;
    }    
    public void setAge(Short age) {        
    this.age = age;
    }    
    public Short getStatus() {        
    return status;
    }    
    public void setStatus(Short status) {        
    this.status = status;
    }

}
登录后复制

(2)UserMapper.java


package org.rcddup.app.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.rcddup.app.domain.User;

@Mapperpublic interface UserMapper {
    
    @Select("select id, name, age, status from user")
    List<User> listUser();
    
    @Select("select id, name, age, status from user where id=#{id}")
    User getUserById(Long id);
    
}
登录后复制

(3)IUserService.java


package org.rcddup.app.service;
import java.util.List;
import org.rcddup.app.domain.User;
public interface IUserService {
    
    List<User> listUser();
    
    User getUserById(Long id);
    
}
登录后复制

(4)UserService.java


package org.rcddup.app.service.impl;
import java.util.List;
import org.rcddup.app.dao.UserMapper;
import org.rcddup.app.domain.User;
import org.rcddup.app.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Servicepublic class UserService implements IUserService{
    
    @Autowired    
    private UserMapper userMapper;
    
    @Override    
    public List<User> listUser() {        
    return userMapper.listUser();
    }

    @Override    
    public User getUserById(Long id) {        
    return userMapper.getUserById(id);
    }
    
}
登录后复制

(5)UserController.java


package org.rcddup.app.web.controller;
import java.util.List;
import org.rcddup.app.domain.User;
import org.rcddup.app.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")public class UserController {
    
    @Autowired    
    private IUserService userService;
    
    @RequestMapping("/list")    
    public List<User> list() {        
    return userService.listUser();
    }
    
    @RequestMapping("/get")    
    public User list(Long id) {        
    return userService.getUserById(id);
    }
    
}
登录后复制

  重新启动 App.java

  输入:http://localhost:8080/user/get?id=1

  输入:http://localhost:8080/user/list

  到此,SpringBoot整合Mybatis就成功了。 

以上是Java中SpringBoot与2.SpringBoot整合Mybatis的实例的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板