Because Kotlin and Java are languages on the JVM, so they can be perfectly interchangeable, this is already known to the world, so the Magician developed with Java nature can also be used by Kotlin, and is perfect to use. \

This article will take a look at how to use Magician in Kotlin, as well as Magician’s Web and JDBC components

Start by creating a Maven project

1. Change the Java in the directory to Kotlin

2. Introduce the Kotlin compiler and jar package required by The Magician in POM.xml

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <kotlin.version>1.5.21</kotlin.version>
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
</properties>

<dependencies>
    <dependency>
        <groupId>com.github.yuyenews</groupId>
        <artifactId>Magician</artifactId>
        <version>1.1.17</version>
    </dependency>
    <dependency>
        <groupId>com.github.yuyenews</groupId>
        <artifactId>Magician-Web</artifactId>
        <version>1.1.7</version>
    </dependency>
    <dependency>
        <groupId>com.github.yuyenews</groupId>
        <artifactId>Magician-JDBC</artifactId>
        <version>1.1.3</version>
    </dependency>

    <! Slf4j supports any package that can be bridged with slf4J.
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.12</version>
    </dependency>

    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>

                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy the code

Creating the Magician Service

fun main(args: Array<String>) {

    var ioEventGroup = EventGroup(1);
    var workEventGroup = EventGroup(3);

    Magician.createTCPServer(ioEventGroup, workEventGroup)
            .scan("com.kotlin.demo")// You need to change the package name of your own handler
            .bind(8080);

}
Copy the code

Create a Handler

@TCPHandler(path = "/json")
class DemoHandler : TCPBaseHandler<MagicianRequest> {

    override fun request(t: MagicianRequest) {
        var name = t.getParam("name");

        println(name);

        t.response.sendJson(200."{'msg':'json'}"); }}Copy the code

Start the service, we visit: http://127.0.0.1:8080/json? name=123

On the console you will see 123 printed and the browser received {‘ MSG ‘:’json’}

How do YOU use Web components

Create a new handler. Set path to the root path. Create a new handler.

@TCPHandler(path = "/")
class DispatcherHandler : TCPBaseHandler<MagicianRequest> {

    override fun request(t: MagicianRequest){ MagicianWeb.createWeb().request(t); }}Copy the code

2. To create the Controller

@Route("/demo")
class DemoController {

    @Route(value = "index", requestMethod = [ReqMethod.GET])
    fun index(indexVO : IndexVO) : String{
        println(indexVO.name);
        return "ok"; }}Copy the code

3. The scanning range needs to be changed

fun main(args: Array<String>) {

    var ioEventGroup = EventGroup(1);
    var workEventGroup = EventGroup(3);

    Magician.createTCPServer(ioEventGroup, workEventGroup)
            // Change the package name of the handler and controller
            // Multiple package names can be separated by commas, or a common parent package name can be configured directly
            .scan("com.kotlin.demo")
            .bind(8080);

}
Copy the code

Start the service, we visit: http://127.0.0.1:8080/demo/index? name=123

On the console, you’ll see that 123 has been printed and the browser has received an OK

How do I use JDBC components

1. Create a data source

class JDBCConfig {

    companion object{
        fun initJDBC(a){
            // Create a data source

            MagicianJDBC.createJDBC()
                    .addDataSource("Data source name".null)// The second argument is passed to the DataSource object
                    .addDataSource("Data source Name 2".null)// Multiple data sources can be added multiple times
                    .defaultDataSourceName("Default data source name"); }}}Copy the code

2. Load the data source

fun main(args: Array<String>) {

    JDBCConfig.initJDBC();// Call the method created in the first step in the main method

    var ioEventGroup = EventGroup(1);
    var workEventGroup = EventGroup(3);

    Magician.createTCPServer(ioEventGroup, workEventGroup)
            // Change the package name of the handler and controller
            // Multiple package names can be separated by commas, or a common parent package name can be configured directly
            .scan("com.kotlin.demo")
            .bind(8080);

}
Copy the code

3. Create a DAO and call JDBCTemplate in it, basically the same as in Java

class DemoDAO {

    fun getIndexData(indexPO: IndexPO): List<IndexPO> {
        return JdbcTemplate
                .create("Name of data source to invoke")
                .selectList("select * from tb_index", indexPO, IndexPO::class.java); }}Copy the code

The Magician’s website: www.magician-io.com