Merge pull request #3447 from BineshNarayanan/master
BAEL-1276 | Adding example of ActiveJDBC
This commit is contained in:
commit
3ecab4a0df
|
@ -0,0 +1,129 @@
|
||||||
|
<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.baeldung</groupId>
|
||||||
|
<artifactId>activejdbc</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>activejdbc</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<activejdbc.version>1.4.13</activejdbc.version>
|
||||||
|
<environments>development.test,development</environments>
|
||||||
|
</properties>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.6.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.javalite</groupId>
|
||||||
|
<artifactId>activejdbc-instrumentation</artifactId>
|
||||||
|
<version>${activejdbc.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>process-classes</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>instrument</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.javalite</groupId>
|
||||||
|
<artifactId>db-migrator-maven-plugin</artifactId>
|
||||||
|
<version>${activejdbc.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<configFile>${project.basedir}/src/main/resources/database.properties</configFile>
|
||||||
|
<environments>${environments}</environments>
|
||||||
|
</configuration>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.34</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.18.1</version>
|
||||||
|
<configuration>
|
||||||
|
<reportFormat>brief</reportFormat>
|
||||||
|
<trimStackTrace>true</trimStackTrace>
|
||||||
|
<useFile>false</useFile>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Spec*.java</include>
|
||||||
|
<include>**/*Test*.java</include>
|
||||||
|
</includes>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/helpers/*</exclude>
|
||||||
|
<exclude>**/*$*</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.javalite</groupId>
|
||||||
|
<artifactId>activejdbc</artifactId>
|
||||||
|
<version>${activejdbc.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>opensymphony</groupId>
|
||||||
|
<artifactId>oscache</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.34</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>1.7.9</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>snapshots1</id>
|
||||||
|
<name>JavaLite Snapshots1</name>
|
||||||
|
<url>http://repo.javalite.io/</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
<updatePolicy>always</updatePolicy>
|
||||||
|
<checksumPolicy>warn</checksumPolicy>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>snapshots2</id>
|
||||||
|
<name>JavaLite Snapshots2</name>
|
||||||
|
<url>http://repo.javalite.io/</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
<updatePolicy>always</updatePolicy>
|
||||||
|
<checksumPolicy>warn</checksumPolicy>
|
||||||
|
</snapshots>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
</project>
|
|
@ -0,0 +1,61 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import com.baeldung.model.Role;
|
||||||
|
import org.javalite.activejdbc.Base;
|
||||||
|
import org.javalite.activejdbc.LazyList;
|
||||||
|
import org.javalite.activejdbc.Model;
|
||||||
|
|
||||||
|
public class ActiveJDBCApp
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Base.open();
|
||||||
|
ActiveJDBCApp app = new ActiveJDBCApp();
|
||||||
|
app.create();
|
||||||
|
app.update();
|
||||||
|
app.delete();
|
||||||
|
app.deleteCascade();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
Base.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void create() {
|
||||||
|
Employee employee = new Employee("Hugo","C","M","BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
LazyList<Model> all = Employee.findAll();
|
||||||
|
System.out.println(all.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void update() {
|
||||||
|
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||||
|
employee.set("last_namea","Choi").saveIt();
|
||||||
|
employee = Employee.findFirst("last_name = ?","Choi");
|
||||||
|
System.out.println(employee.getString("first_name") + " " + employee.getString("last_name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void delete() {
|
||||||
|
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||||
|
employee.delete();
|
||||||
|
employee = Employee.findFirst("last_name = ?","Choi");
|
||||||
|
if(null == employee){
|
||||||
|
System.out.println("No such Employee found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deleteCascade() {
|
||||||
|
create();
|
||||||
|
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||||
|
employee.deleteCascade();
|
||||||
|
employee = Employee.findFirst("last_name = ?","C");
|
||||||
|
if(null == employee){
|
||||||
|
System.out.println("No such Employee found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.model;
|
||||||
|
|
||||||
|
|
||||||
|
import org.javalite.activejdbc.Model;
|
||||||
|
|
||||||
|
public class Employee extends Model {
|
||||||
|
|
||||||
|
public Employee(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(String firstName, String lastName, String gender, String createdBy) {
|
||||||
|
set("first_name1",firstName);
|
||||||
|
set("last_name",lastName);
|
||||||
|
set("gender",gender);
|
||||||
|
set("created_by",createdBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.model;
|
||||||
|
|
||||||
|
|
||||||
|
import org.javalite.activejdbc.Model;
|
||||||
|
import org.javalite.activejdbc.annotations.Table;
|
||||||
|
|
||||||
|
@Table("EMP_ROLES")
|
||||||
|
public class Role extends Model {
|
||||||
|
|
||||||
|
public Role(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role(String role,String createdBy){
|
||||||
|
set("role_name",role);
|
||||||
|
set("created_by",createdBy);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
# noinspection SqlNoDataSourceInspectionForFile
|
||||||
|
|
||||||
|
create table organisation.employees
|
||||||
|
(
|
||||||
|
id int not null auto_increment
|
||||||
|
primary key,
|
||||||
|
first_name varchar(100) not null,
|
||||||
|
last_name varchar(100) not null,
|
||||||
|
gender varchar(1) not null,
|
||||||
|
created_at datetime not null,
|
||||||
|
updated_at datetime null,
|
||||||
|
created_by varchar(100) not null,
|
||||||
|
updated_by varchar(100) null
|
||||||
|
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
|
||||||
|
|
||||||
|
create table organisation.emp_roles
|
||||||
|
(
|
||||||
|
id int not null auto_increment primary key,
|
||||||
|
employee_id int not null,
|
||||||
|
role_name varchar(100) not null,
|
||||||
|
created_at datetime not null,
|
||||||
|
updated_at datetime null,
|
||||||
|
created_by varchar(100) not null,
|
||||||
|
updated_by varchar(100) null
|
||||||
|
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
|
|
@ -0,0 +1,10 @@
|
||||||
|
development.driver=com.mysql.jdbc.Driver
|
||||||
|
development.username=root
|
||||||
|
development.password=123456
|
||||||
|
development.url=jdbc:mysql://localhost/organisation
|
||||||
|
|
||||||
|
|
||||||
|
development.test.driver=com.mysql.jdbc.Driver
|
||||||
|
development.test.username=root
|
||||||
|
development.test.password=123456
|
||||||
|
development.test.url=jdbc:mysql://localhost/organisation_test
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import com.baeldung.model.Role;
|
||||||
|
import org.javalite.activejdbc.test.DBSpec;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ActiveJDBCAppTest extends DBSpec
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreated_thenIsValid() {
|
||||||
|
Employee employee = new Employee("B", "N", "M", "BN");
|
||||||
|
the(employee).shouldBe("valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreatedWithRoles_thenShouldPersist() {
|
||||||
|
Employee employee = new Employee("B", "N", "M", "BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
employee.add(new Role("Lead Java Developer","BN"));
|
||||||
|
a(Role.count()).shouldBeEqual(2);
|
||||||
|
List<Role> roles = employee.getAll(Role.class).orderBy("created_at");
|
||||||
|
the(roles.get(0).getRoleName()).shouldBeEqual("Java Developer");
|
||||||
|
the(roles.get(1).getRoleName()).shouldBeEqual("Lead Java Developer");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreatedWithRoles_whenNameUpdated_thenShouldShowNewName() {
|
||||||
|
Employee employee = new Employee("Binesh", "N", "M", "BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
employee.add(new Role("Lead Java Developer","BN"));
|
||||||
|
employee = Employee.findFirst("first_name = ?", "Binesh");
|
||||||
|
employee.set("last_name","Narayanan").saveIt();
|
||||||
|
Employee updated = Employee.findFirst("first_name = ?", "Binesh");
|
||||||
|
the(updated.getLastName()).shouldBeEqual("Narayanan");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreatedWithRoles_whenDeleted_thenShouldNotBeFound() {
|
||||||
|
Employee employee = new Employee("Binesh", "N", "M", "BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
employee.delete();
|
||||||
|
employee = Employee.findFirst("first_name = ?", "Binesh");
|
||||||
|
the(employee).shouldBeNull();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue