Spring boot mybaits新手入门

第一步创建项目 一步步来 全截图更容易懂 坑都帮你们踩完了 嘤嘤嘤

第二步 连接数据库 这个就是创建的一个数据库 mysql的  

连接的信息都在这里,这个是我的呦,不要照抄

第三步 在application.properties这个里边写mysql配置 说明下 我是kotlin的也是最新的IEDA 可能跟之前的前辈有些不一样,照着来就好了

 

 数据库配置 配置玩之后,跑一下看是否连接成功

spring.datasource.url=jdbc:mysql://localhost:3306/bursh_questions?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=abc123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

 目测控制一切都正常的,是链接成功了

 数据库链接成功了,接下来就查询数据呗 

目录结构 

我是连包名的贴出来了,怕有些同学不知道那个包

BankChapterController.kt

import com.kotlin.bank.entity.BankChapter
import com.kotlin.bank.service.BankChapterService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/chapter")
class BankChapterController {
    @Autowired
     var bankChapterService: BankChapterService?=null
    @RequestMapping("getChapter")
    fun selectChapter():List<BankChapter>{
        return  bankChapterService!!.selectChapter()
    }
}
BankChapter.kt

class BankChapter{
    val chapterId=0
    val chapterContent=""
}

 

BankChapterMapping.kt

import com.kotlin.bank.entity.BankChapter
import org.springframework.stereotype.Repository

@Repository
interface BankChapterMapping {
    //查询所有
    fun selectChapter(): List<BankChapter>
}
BankChapterService.kt

import com.kotlin.bank.entity.BankChapter
import com.kotlin.bank.mapping.BankChapterMapping
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class BankChapterService {
    @Autowired
     var chapterMapping:BankChapterMapping?=null
    fun  selectChapter():List<BankChapter>{
        return chapterMapping!!.selectChapter()
    }
}
BankChapterMapping.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kotlin.bank.mapping.BankChapterMapping">

    <resultMap id="BaseResultMap" type="com.kotlin.bank.entity.BankChapter" autoMapping="true">
        <id column="chapter_id" jdbcType="INTEGER" property="chapterId" />
        <result column="chapter_content" jdbcType="VARCHAR" property="chapterContent" />
    </resultMap>

    <select id="selectChapter" resultType="com.kotlin.bank.entity.BankChapter">
        select * from bank_chapter
    </select>

</mapper>

 

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/bursh_questions?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=abc123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

#运行项目的端口
server.port=8083
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8

#mybatis映射
mybatis.mapper-locations=classpath:mapping/BankChapterMapping.xml
mybatis.type-aliases-package=com.kotlin.bank.entity

#mybatis 映射返回驼峰 注意这里来设置 不然在mapping.xml的映射就不起作用
mybatis.configuration.map-underscore-to-camel-case=true

 最后一步要在入口配置扫描mapping  QuestionBankApplication.kt

到这里差不多已经完了,运行起来看看,如果不行就删了重写,哈哈哈哈哈哈哈哈哈哈哈哈或或或或或或

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想吃辣条

觉得不错,就可怜可怜博主吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值