Skip to content

Instantly share code, notes, and snippets.

@carolosf
Last active March 9, 2018 03:42
Show Gist options
  • Save carolosf/1adbb93e3894a0656064355442a32144 to your computer and use it in GitHub Desktop.
Save carolosf/1adbb93e3894a0656064355442a32144 to your computer and use it in GitHub Desktop.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
var kotlin_version: String by extra
kotlin_version = "1.2.30"
repositories {
mavenCentral()
}
dependencies {
classpath(kotlinModule("gradle-plugin", kotlin_version))
}
}
group = "com.example"
version = "1.0-SNAPSHOT"
apply {
plugin("kotlin")
}
val kotlin_version: String by extra
repositories {
mavenCentral()
jcenter()
maven {
setUrl("https://dl.bintray.com/kotlin/exposed")
}
maven {
setUrl("https://dl.bintray.com/kotlin/ktor")
}
maven {
setUrl("https://dl.bintray.com/kotlin/kotlinx")
}
}
val junitPlatformVersion: String = "1.0.2"
val junitJupiterVersion: String = "5.0.2"
val exposedVersion: String = "0.9.1"
val mysqlConnectorVersion: String = "5.1.13"
dependencies {
compile(kotlinModule("stdlib-jdk8", kotlin_version))
compile("mysql:mysql-connector-java:$mysqlConnectorVersion")
compile("org.jetbrains.exposed:exposed:$exposedVersion")
testCompile("org.junit.platform:junit-platform-runner:$junitPlatformVersion")
testCompile("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion")
testRuntime("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class CreateMonsterRepositorySql implements CreateMonsterRepository {
private final JdbcTemplate jdbcTemplate;
public CreateMonsterRepositorySql(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Monster findById(String id) {
List<Monster> monsters = jdbcTemplate.query(
"select id, name from monster where id = ?",
new Object[]{id},
(rs, rowNum) -> new Monster(rs.getString("id"), rs.getString("name"))
);
if (monsters.isEmpty()) {
throw new IllegalArgumentException("Monster {" + id + "} not found");
}
return monsters.get(0);
}
@Override
public void insertMonster(Monster monster) {
this.jdbcTemplate.update(
"INSERT INTO monster (id, name) VALUES (?,?)",
monster.getId(),
monster.getName()
);
}
}
--liquibase formatted sql
--changeset carolosf:BC-123
CREATE TABLE monster (db_id int unsigned auto_increment not null, id VARCHAR(255), name VARCHAR(255), primary key (db_id));
CREATE UNIQUE INDEX monster_id ON monster(id);
--rollback DROP TABLE monster;
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
<include file="db.changelog.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>
#contexts: ${liquibase.contexts}
#changeLogFile: com/company/client/project/db.changelog.xml
#driver: ${dataSource.project.driverClass}
#url: ${dataSource.project.jdbcURL}
#username: ${dataSource.project.user}
#password: ${dataSource.project.password}
#verbose: true
#dropFirst: false
changeLogFile: /home/cfoscolos/projects/monster/src/main/resources/liquibase/db.changelog.xml
#changeLogFile: liquibase/changes.sql
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/monster?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: example
verbose: true
dropFirst: false
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.vintage.version>${junit.version}.0</junit.vintage.version>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.platform.version>1.0.0</junit.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- To run tests on Intellij -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.5.5</version>
<configuration>
<propertyFile>src/main/resources/liquibase/liquibase.properties</propertyFile>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/monster?useUnicode=true&characterEncoding=utf-8&useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("example");
CreateMonsterRepository createMonsterRepository = new CreateMonsterRepositorySql(new JdbcTemplate(dataSource));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment