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');
  • 项目结构

项目结构

pom.xml

<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>com.test</groupId>
    <artifactId>Ebean-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Ebean-demo</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Use MySQL Connector-J -->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- <dependency> <groupId>io.ebean</groupId> <artifactId>ebean-spring</artifactId> 
            <version>10.4.2</version> </dependency> -->
        <!-- https://mvnrepository.com/artifact/io.ebean/persistence-api -->
        <dependency>
            <groupId>io.ebean</groupId>
            <artifactId>persistence-api</artifactId>
            <version>2.2.1</version>
        </dependency>

        <!-- ebean -->
        <dependency>
            <groupId>io.ebean</groupId>
            <artifactId>ebean</artifactId>
            <version>10.4.2</version>
        </dependency>

        <dependency>
            <groupId>io.ebean</groupId>
            <artifactId>querybean-generator</artifactId>
            <version>10.2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>io.ebean</groupId>
            <artifactId>ebean-querybean</artifactId>
            <version>10.3.1</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>io.repaint.maven</groupId>
                <artifactId>tiles-maven-plugin</artifactId>
                <version>2.8</version>
                <extensions>true</extensions>
                <configuration>
                    <tiles>
                        <tile>org.avaje.tile:java-compile:1.1</tile>
                        <tile>io.ebean.tile:enhancement:2.9</tile>
                    </tiles>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

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<EbeanServer> {

    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<User> 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<User> 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

测试
localhost:8080/user/getAll

localhost:8080/user/getById?id=1

示例代码github地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值