第一步创建项目 一步步来 全截图更容易懂 坑都帮你们踩完了 嘤嘤嘤
第二步 连接数据库 这个就是创建的一个数据库 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
到这里差不多已经完了,运行起来看看,如果不行就删了重写,哈哈哈哈哈哈哈哈哈哈哈哈或或或或或或