Spring Boot Ebean集成

项目使用 maven构建,使用Spring boot 1.5.7 + ebean +mysql

user表:

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(20) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('00000000000000000001', '张三', '123');
INSERT INTO `user` VALUES ('00000000000000000002', '李四', '321');
  • 项目结构
Spring Boot Ebean集成_第1张图片
项目结构

pom.xml


    4.0.0

    com.test
    Ebean-demo
    0.0.1-SNAPSHOT
    jar

    Ebean-demo
    http://maven.apache.org

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

    
        UTF-8
        1.8
    



    
        
            org.springframework.boot
            spring-boot-starter-web
        

        

        
            mysql
            mysql-connector-java
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
        
            io.ebean
            persistence-api
            2.2.1
        

        
        
            io.ebean
            ebean
            10.4.2
        

        
            io.ebean
            querybean-generator
            10.2.1
            provided
        

        
            io.ebean
            ebean-querybean
            10.3.1
        

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                io.repaint.maven
                tiles-maven-plugin
                2.8
                true
                
                    
                        org.avaje.tile:java-compile:1.1
                        io.ebean.tile:enhancement:2.9
                    
                
            

        
    

Ebean配置文件 ebean.properties:

ebean.search.packages= com.demo          
# the name of the default server
datasource.default=db

## define these in external properties ...

datasource.db.username=root
datasource.db.password=root
datasource.db.databaseUrl=jdbc:mysql://localhost:3306/test?useSSL=false
datasource.db.databaseDriver=com.mysql.jdbc.Driver

User实体:

package com.demo;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import io.ebean.Model;

@Entity
@Table(name = "user")
public class User extends Model {
    @Id
    private Integer id;

    private String username;

    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(Integer id, String username, String password) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

}

自定义FactoryBean:

package com.demo;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import io.ebean.EbeanServer;
import io.ebean.EbeanServerFactory;
import io.ebean.config.ServerConfig;

/**
 * Simple Spring bean factory for creating the EbeanServer.
 */
@Component
public class MyEbeanServerFactory implements FactoryBean {

    public EbeanServer getObject() throws Exception {
        return createEbeanServer();
    }

    public Class getObjectType() {
        return EbeanServer.class;
    }

    public boolean isSingleton() {
        return true;
    }

    /**
     * Create a EbeanServer instance.
     */
    private EbeanServer createEbeanServer() {

        ServerConfig config = new ServerConfig();
        config.setName("db");

        // load configuration from ebean.properties
        config.loadFromProperties();
        config.setDefaultServer(true);
        // other programmatic configuration

        return EbeanServerFactory.create(config);
    }
}

UserService.java:

package com.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.ebean.EbeanServer;

@Service
public class UserService {

    @Autowired
    private EbeanServer ebeanServer;

    public List getAll() {
        return ebeanServer.find(User.class).findList();
    }

    public User getById(Integer id) {
        return ebeanServer.find(User.class).where().eq("id", id).findOne();
    }
}

Controller.java

package com.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @Autowired
    private UserService userService;

    @RequestMapping("user/getAll")
    public List getAll() {
        return userService.getAll();
    }

    @RequestMapping("user/getById")
    public User getById(@RequestParam(value = "id") Integer id) {
        return userService.getById(id);
    }

}

Application.java

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

启动 spring-boot:run

Spring Boot Ebean集成_第2张图片

测试
localhost:8080/user/getAll

Spring Boot Ebean集成_第3张图片

localhost:8080/user/getById?id=1

Spring Boot Ebean集成_第4张图片

示例代码github地址

你可能感兴趣的:(Spring Boot Ebean集成)