<input id="ohw05"></input>
  • <table id="ohw05"><menu id="ohw05"></menu></table>
  • <var id="ohw05"></var>
  • <code id="ohw05"><cite id="ohw05"></cite></code>
    <label id="ohw05"></label>
    <var id="ohw05"></var>
  • kotlin+springboot+mybatis-puls+mysql搭建gradle-web工程

    kotlin+springboot+mybatis-puls+mysql搭建web工程

    ? 前段時間研究了spring security及OAuth2系列之后,本來打算研究spring的,但是部門發生了一些情況,我轉崗到了另一個部門,具體做內部CICD的產品,需要用kotlin開發,于是我需要從零開始學習Kotlin語言。于是我連忙找了本Kotlin的書籍看了幾天主要的語法章節,想著需要做點demo讓自己熟悉一下,于是有了這篇文章。

    ? 本片主要講怎么基于kotlin搭建springboot-web項目。

    在IDEA上創建gradle項目

    ? 在IDEA上創建kotlin項目有多種方式,這里我采用了如圖的方式

    引入所需依賴

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
        testCompile group: 'junit', name: 'junit', version: '4.12'
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.4.4'
        implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.27'
        implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.4.3.3'
    
    }
    

    ? 這里引入springboot\mysql-connector-java\mybatis-plus等組件,用的是基于groovy語言構建的gradle腳本,不知道這么腳本怎么來的,怎么到https://mvnrepository.com/ maven倉庫查找gradle對應的依賴腳本。

    新建各個文件(dao\service\mapper\entity\controller)

    新建啟動類

    @SpringBootApplication
    @MapperScan("com.canway.app.dao")
    open class MainApplicationClass
    
    fun main(args: Array<String>) {
        runApplication<MainApplicationClass>(*args)
    }
    

    新建entity(user表)

    import com.fasterxml.jackson.annotation.JsonFormat
    import java.util.Date
    
    class TUser {
        var id: Int? = null;
        
        var username: String? = null;
        
        var age: Int? = null;
        
        var sex: Int? = null;
        
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
        var createTime: Date? = null;
        
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
        var updateTime: Date? = null;
    }
    

    新建service/impl

    import com.baomidou.mybatisplus.extension.plugins.pagination.Page
    import com.canway.app.dto.PageQuery
    import com.canway.app.entity.TUser
    
    interface TUserService {
        
        fun queryById(id: Int): TUser?
        
        fun queryByPage(pageQuery: PageQuery): Page<TUser?>
        
        fun insert(tUser: TUser): TUser
        
        fun update(tUser: TUser): TUser
        
        fun deleteById(id: Int): Boolean
    }
    
    package com.canway.app.service.impl
    
    import com.baomidou.mybatisplus.core.toolkit.Wrappers
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page
    import com.canway.app.dao.TUserDao
    import com.canway.app.dto.PageQuery
    import com.canway.app.entity.TUser
    import com.canway.app.service.TUserService
    import org.springframework.beans.factory.annotation.Autowired
    import org.springframework.stereotype.Service
    
    @Service
    class TUserServiceImpl @Autowired constructor(private var tUserDao: TUserDao): TUserService{
        
        // 不同過在構造函數中注入屬性的話,可以通過尋常注入方式
    //    @Resource
    //    private lateinit var tUserDao: TUserDao;
        
        override fun queryById(id: Int): TUser? {
            return tUserDao.queryById(id)
        }
        
        override fun queryByPage(pageQuery: PageQuery): Page<TUser?> {
            val wrapper = Wrappers.lambdaQuery<TUser>()
    //        wrapper.like(TUser::getUsername, query.getUsername());
            //        wrapper.like(TUser::getUsername, query.getUsername());
            val page: Page<TUser?> = Page<TUser?>(pageQuery.getPageNum(), pageQuery.getPageSize(), true)
            val page1: Page<TUser?> = tUserDao.selectPage(page, wrapper)
            return page1
        }
        
        override fun insert(tUser: TUser): TUser {
            this.tUserDao.insert(tUser);
            return tUser;
        }
        
        override fun update(tUser: TUser): TUser {
            this.tUserDao.update(tUser);
            return tUser;
        }
        
        override fun deleteById(id: Int): Boolean {
            return this.tUserDao.deleteById(id) > 0
        }
    }
    

    新建dao

    package com.canway.app.dao
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page
    import com.canway.app.entity.TUser
    import org.apache.ibatis.annotations.Param
    
    interface TUserDao: BaseMapper<TUser> {
        /**
         * 通過ID查詢單條數據
         *
         * @param id 主鍵
         * @return 實例對象
         */
        fun queryById(id: Int): TUser?
        
        /**
         * 查詢指定行數據
         *
         * @param tUser 查詢條件
         * @return 對象列表
         */
        fun queryByPage(page: Page<TUser?>?, @Param("tUser") tUser: TUser?): List<TUser?>?
        
        /**
         * 統計總行數
         *
         * @param tUser 查詢條件
         * @return 總行數
         */
        fun count(tUser: TUser?): Long
        
        /**
         * 新增數據
         *
         * @param tUser 實例對象
         * @return 影響行數
         */
        override fun insert(tUser: TUser?): Int
        
        /**
         * 批量新增數據(MyBatis原生foreach方法)
         *
         * @param entities List<TUser> 實例對象列表
         * @return 影響行數
        </TUser> */
        fun insertBatch(@Param("entities") entities: List<TUser?>?): Int
        
        /**
         * 批量新增或按主鍵更新數據(MyBatis原生foreach方法)
         *
         * @param entities List<TUser> 實例對象列表
         * @return 影響行數
         * @throws org.springframework.jdbc.BadSqlGrammarException 入參是空List的時候會拋SQL語句錯誤的異常,請自行校驗入參
        </TUser> */
        fun insertOrUpdateBatch(@Param("entities") entities: List<TUser?>?): Int
        
        /**
         * 修改數據
         *
         * @param tUser 實例對象
         * @return 影響行數
         */
        fun update(tUser: TUser?): Int
        
        /**
         * 通過主鍵刪除數據
         *
         * @param id 主鍵
         * @return 影響行數
         */
        fun deleteById(id: Int?): Int
    }
    

    新建controller

    package com.canway.app.controller
    
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page
    import com.canway.app.dto.PageQuery
    import com.canway.app.entity.TUser
    import com.canway.app.service.TUserService
    import org.springframework.beans.factory.annotation.Autowired
    import org.springframework.http.ResponseEntity
    import org.springframework.web.bind.annotation.DeleteMapping
    import org.springframework.web.bind.annotation.GetMapping
    import org.springframework.web.bind.annotation.PathVariable
    import org.springframework.web.bind.annotation.PostMapping
    import org.springframework.web.bind.annotation.PutMapping
    import org.springframework.web.bind.annotation.RequestMapping
    import org.springframework.web.bind.annotation.RestController
    
    @RestController
    @RequestMapping("tUser")
    class UserController {
        
        @Autowired
        private val tUserService: TUserService? = null
        
    
        @GetMapping("/hello")
        fun helloworld(): String {
            return "Hello world"
        }
        
        @GetMapping("{id}")
        fun queryById(@PathVariable("id") id: Int): ResponseEntity<TUser?> {
            return ResponseEntity.ok(this.tUserService?.queryById(id))
        }
        
        @PostMapping
        fun add(tUser: TUser): ResponseEntity<TUser> {
            return ResponseEntity.ok(this.tUserService?.insert(tUser));
        }
        
        @GetMapping
        fun queryByPage(query: PageQuery): ResponseEntity<Page<TUser?>> {
            return ResponseEntity.ok(this.tUserService?.queryByPage(query))
        }
        
        @PutMapping
        fun edit(tUser: TUser): ResponseEntity<TUser> {
            return ResponseEntity.ok(this.tUserService?.update(tUser))
        }
        
        @DeleteMapping
        fun deleteById(id: Int): ResponseEntity<Boolean> {
            return ResponseEntity.ok(this.tUserService?.deleteById(id))
        }
    }
    

    新建mybatis配置類

    package com.canway.app.config
    
    import com.baomidou.mybatisplus.annotation.DbType
    import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor
    import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor
    import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor
    import org.springframework.context.annotation.Bean
    import org.springframework.context.annotation.Configuration
    
    @Configuration
    open class MybatisPlusConfig {
        
        @Bean
        open fun mybatisPlusInterceptor(): MybatisPlusInterceptor{
            var interceptor = MybatisPlusInterceptor();
            //向Mybatis過濾器鏈中添加分頁攔截器
            interceptor.addInnerInterceptor(PaginationInnerInterceptor(DbType.MYSQL))
            interceptor.addInnerInterceptor(OptimisticLockerInnerInterceptor())
            //還可以添加i他的攔截器
            return interceptor
        }
    }
    

    添加配置

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/java_web?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
        username: root
        password: 123456
        hikari:
          minimum-idle: 1
          maximum-pool-size: 3
          connection-timeout: 5000
    
    mybatis-plus:
      configuration:
        map-underscore-to-camel-case: true
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    

    總結

    ? 基于kotlin+springboot+mybatis-plus+mysql的gradle-web項目到這里就結束,整個過程中還是讓自己熟悉了一些kotlin的具體語法,像函數聲明、變量、類的定義等,后續寫業務的時候才能相對快速熟悉其它語法。

    posted @ 2021-12-30 21:26  尋找的路上  閱讀(121)  評論(0編輯  收藏  舉報
    国产美女a做受大片观看