实现了数据层的功能

This commit is contained in:
2026-05-13 03:11:56 +08:00
commit 3a86d29e5c
18 changed files with 607 additions and 0 deletions
@@ -0,0 +1,11 @@
package com.msksbr.bookmgr
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class BookMgrApplication
fun main(args: Array<String>) {
runApplication<BookMgrApplication>(*args)
}
@@ -0,0 +1,14 @@
package com.msksbr.bookmgr.entity
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
@TableName("book")
data class Book(
@TableId(type = IdType.AUTO)
val id: Long?=null,
var name: String,
var author: String,
var stock:Int
)
@@ -0,0 +1,17 @@
package com.msksbr.bookmgr.entity
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import java.util.Date
@TableName("book_record")
data class BorrowRecord(
@TableId(type = IdType.AUTO)
val id:Long?=null,
var userId:Long,
var bookId:Long,
var borrowTime: Date,
var returnTime:Date?=null,
var status:String
)
@@ -0,0 +1,14 @@
package com.msksbr.bookmgr.entity
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
@TableName("user")
data class User(
@TableId(type = IdType.AUTO) // 设置自增主键
val id: Long?=null,
var username: String,
var password: String,
var role: String,
)
@@ -0,0 +1,8 @@
package com.msksbr.bookmgr.mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.msksbr.bookmgr.entity.Book
import org.apache.ibatis.annotations.Mapper
@Mapper
interface BookMapper: BaseMapper<Book>
@@ -0,0 +1,8 @@
package com.msksbr.bookmgr.mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.msksbr.bookmgr.entity.Book
import org.apache.ibatis.annotations.Mapper
@Mapper
interface BorrowRecordMapper: BaseMapper<Book>
@@ -0,0 +1,8 @@
package com.msksbr.bookmgr.mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.msksbr.bookmgr.entity.User
import org.apache.ibatis.annotations.Mapper
@Mapper
interface UserMapper: BaseMapper<User>
+17
View File
@@ -0,0 +1,17 @@
spring:
profiles:
active: dev
application:
name: bookMgr
datasource:
driver-class-name: ${DB_DRIVER}
url: jdbc:${DB_TYPE}://${DB_URL}:${DB_PORT}/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
mybatis-plus:
configuration:
# 开启驼峰命名法
map-underscore-to-camel-case: true
# 开启日志输出sql语句
log-impl: org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl
@@ -0,0 +1,13 @@
package com.msksbr.bookmgr
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class BookMgrApplicationTests {
@Test
fun contextLoads() {
}
}
@@ -0,0 +1,67 @@
package com.msksbr.bookmgr.mapper
import com.msksbr.bookmgr.entity.User
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.transaction.annotation.Transactional
@SpringBootTest
@Transactional // 测试完可以自动回滚
class UserMapperTest {
// Spring自动装配userMapper
@Autowired
private lateinit var userMapper: UserMapper
@Test
// 新增测试
fun testInsert() {
// 准备用户
val user = User(null,"Test", "123456", "admin")
// 插入
userMapper.insert(user)
// 拿到插入后的自增id
val userId = user.id ?: throw AssertionError("用户插入失败,ID为空")
// 查询插入后的用户
val res = userMapper.selectById(userId)
// 比对
assertNotNull(res)
assertEquals(user.username, res.username)
assertEquals(user.password, res.password)
assertEquals(user.role, res.role)
}
// 修改测试
@Test
fun testUpdate() {
// 先新增
val user = User(null, "OldName", "123456", "admin")
userMapper.insert(user)
val userId = user.id!!
// 修改字段
user.username="newName"
user.password="654321"
userMapper.updateById(user)
// 比对
val res = userMapper.selectById(userId)
assertNotNull(res)
assertEquals(user.username, res.username)
assertEquals(user.password, res.password)
assertEquals(user.role, res.role)
}
// 删除测试
@Test
fun testDelete() {
// 新增
val user = User(null,"DelTest", "123456", "admin")
userMapper.insert(user)
val userId = user.id!!
// 删除
userMapper.deleteById(userId)
// 确认删除
val res = userMapper.selectById(userId)
assertNull(res)
}
}