Kotlin 是一门现代但已成熟的编程语言,旨在让开发人员更幸福快乐。 它简洁、安全、可与 Java 及其他语言互操作,并提供了多种方式在多个平台间复用代码,以实现高效编程。
kt入门的合集文章如下:
1.使用kt进行web开发;
2.使用h2database进行初始化数据库;
3.使用 lateinit 关键字,变量在定义时不需要初始化;
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.14version>
<relativePath/>
parent>
<groupId>com.tianjugroupId>
<artifactId>spfa-rpc-demoartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<kotlin.version>1.8.22kotlin.version>
<kotlin.compiler.incremental>truekotlin.compiler.incremental>
<netty.version>4.1.91.Finalnetty.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.3.1version>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.jetbrains.kotlingroupId>
<artifactId>kotlin-stdlib-jdk8artifactId>
<version>1.8.22version>
dependency>
<dependency>
<groupId>io.nettygroupId>
<artifactId>netty-allartifactId>
<version>${netty.version}version>
<scope>compilescope>
dependency>
dependencies>
project>
h2database为我们提供了十分轻量,十分快捷方便的内嵌式数据库
server:
port: 9099
spring:
datasource:
dbcp2:
driver: org.h2.Driver
url: jdbc:h2:mem:test
username: root
password: test
sql:
init:
schema-locations: classpath:db/schema-h2.sql
data-locations: classpath:db/data-h2.sql
mode: always
schema-h2.sql
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`tel` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ;
data-h2.sql
INSERT INTO `t_user` (`id`, `name`, `tel`) VALUES
(1,'peter','2154652'),
(2,'Shirley','25451232'),
(3,'tom','78942131');
package com.tianju.ktWeb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApp {
public static void main(String[] args) {
SpringApplication.run(WebApp.class);
}
}
package com.tianju.ktWeb.entity
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
@TableName("t_user")
data class User(
@TableId("id")
val id:Int,
val name:String,
val tel:String
)
package com.tianju.ktWeb.mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.tianju.ktWeb.entity.User
import org.apache.ibatis.annotations.Mapper
@Mapper
interface UserMapper :BaseMapper<User> {
}
使用 lateinit 关键字,变量在定义时不需要初始化,不需要加上 ? 和 !! 操作符。在使用第一次变量之前,要保证为其赋值 , 不然会出现空指针异常。
package com.tianju.ktWeb.controller
import com.tianju.ktWeb.entity.User
import com.tianju.ktWeb.mapper.UserMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
@Controller
class UserController {
@Autowired
private lateinit var userMapper:UserMapper
private val userList = listOf(
User(1, "peter1", "13455"),
User(2, "peter2", "13455"),
User(3, "peter3", "13455"),
)
@GetMapping("/all")
@ResponseBody
fun getUser():List<User> {
return userList
}
@GetMapping("/id")
@ResponseBody
fun getById(@RequestParam("byId") byId:Int) :User{
val users = userList.filter { it.id == byId }
return users[0]
}
@GetMapping("/all/byMapper")
@ResponseBody
fun getAllByMapper(): List<User> {
return userMapper.selectList(null);
}
}
1.使用kt进行web开发;
2.使用h2database进行初始化数据库;
3.使用 lateinit 关键字,变量在定义时不需要初始化;