springboot整合mybatis访问mysql数据库

环境

  • 构建工具:Gradle
  • 开发语言:Kotlin
  • mysql版本: 8.0

配置步骤

1.引入依赖

在build,gradle文件引入mybatis-spring-boot-starter-web的依赖:

1
implementation "org.springframework.boot:spring-boot-starter-web:2.4.9"

引入数据库连接依赖:

1
implementation "mysql:mysql-connector-java:8.0.26"

2.引入数据源

application.yml配置文件中引入数据源:

1
2
3
4
5
6
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
driver: com.mysql.cj.jdbc.Driver

这样,springboot就可以访问数据了。

3.创建数据库表

建表语句:

1
2
3
4
5
6
7
8
9
10
11
-- create table `account`
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');

4.具体实现

这篇文篇通过注解的形式实现。

5.创建实体:
1
2
3
4
5
data class Account (
var id:Int = 0,
var name: String? = null,
var money :Double= 0.0
)
6.Dao层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Mapperpublic interface AccountMapper {

@Insert("insert into account(name, money) values(#{name}, #{money})")
int add(@Param("name") String name, @Param("money") double money);

@Update("update account set name = #{name}, money = #{money} where id = #{id}")
int update(@Param("name") String name, @Param("money") double money, @Param("id") int id);

@Delete("delete from account where id = #{id}")
int delete(int id);

@Select("select id, name as name, money as money from account where id = #{id}")
Account findAccount(@Param("id") int id);

@Select("select id, name as name, money as money from account")
List<Account> findAccountList();}

7.service层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Mapper
interface AccountMapper {
@Insert("insert into account(name, money) values(#{name}, #{money})")
fun add(@Param("name") name: String?, @Param("money") money: Double): Int

@Update("update account set name = #{name}, money = #{money} where id = #{id}")
fun update(
@Param("name") name: String?,
@Param("money") money: Double,
@Param("id") id: Int
): Int

@Delete("delete from account where id = #{id}")
fun delete(id: Int): Int

@Select("select id, name as name, money as money from account where id = #{id}")
fun findAccount(@Param("id") id: Int): Account?

@Select("select id, name as name, money as money from account")
fun findAccountList(): List<Account?>?
}

8.controller层,构建restful API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

@CrossOrigin
@RestController
class MainControl {


@Autowired
lateinit var accountService: AccountService


@GetMapping(value = ["/list"])
fun getAccounts(): List<Account> {
return accountService.findAccountList()
}

@GetMapping(value = ["/{id}"])
fun getAccountById(@PathVariable("id") id: Int): Account? {
return accountService.findAccount(id)
}

@PutMapping(value = ["/{id}"])
fun updateAccount(
@PathVariable("id") id: Int, @RequestParam(value = "name", required = true) name: String?,
@RequestParam(value = "money", required = true) money: Double
): String {
val t = accountService.update(name, money, id)
return if (t == 1) {
"success"
} else {
"fail"
}
}

@DeleteMapping(value = ["/{id}"])
fun delete(@PathVariable(value = "id") id: Int): String {
val t = accountService.delete(id)
return if (t == 1) {
"success"
} else {
"fail"
}
}
}

配置完毕

最后启动程序, 浏览器访问:http://localhost:8080/list进行测试

本文为作者原创 转载时请注明出处 谢谢

img

乱码三千 – 点滴积累 ,欢迎来到乱码三千技术博客站

0%