搭建一个SpringBoot小项目,使用mybatis连接数据库,并对外提供数据库增删改查restful接口
由于要连接数据库,使用pojo实体类一套
项目结构
jdk 1.8
mysql 5.7
maven 3.8.2
IDEA
1.1 建表CREATE DATAbase `mybatis`; USE `mybatis`; DROp TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `name` varchar(10) DEFAULT NULL COMMENT '姓名', `age` int(2) DEFAULT NULL COMMENT '年龄', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;1.2 建立一个springboot项目
添加依赖
在pom.xml文件中添加依赖,配置后整个文件如下:
1.3 连接数据库并整合Mybatis4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.5 com.xingye test03 0.0.1-SNAPSHOT test03 test03 1.8 org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 org.projectlombok lombok org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
在如上application.properties文件中添加:
#连接数据库 spring.datasource.username= spring.datasource.password= spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #整合mybatis mybatis.type-aliases-package=com.xingye.test03.pojo mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
第一、二行username与password后输入自己的mysql账号密码
一般username为root
连接上数据库
成功后如图
2、搭建项目编写相应代码,对应前文项目结构
2.1 实体类pojo-Userpackage com.xingye.test03.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
2.2 接口类mapper-UserMapper
package com.xingye.test03.mapper;
import com.xingye.test03.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
List queryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
@Mapper表示这是一个MyBatis的mapper类
是Dao层使用@Repository,可不添加该注解
2.3 UserMapper.xml映射SQL语句,可在mybatis官方文档中找到例子
//此处绑定自己对应的接口类//sql语句
parameterType为接收参数类型
注:namespace中绑定自己对应的接口类
2.4 控制层controller-UserComtrollerpackage com.xingye.test03.controller;
import com.xingye.test03.mapper.UserMapper;
import com.xingye.test03.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List queryUserList(){
List userList = userMapper.queryUserList();
for (User user:userList){
System.out.println(user);
}
return userList;
}
@GetMapping("/add/{id}/{name}/{age}")
public String addUser(@PathVariable int id,@PathVariable String name,@PathVariable int age){
userMapper.addUser(new User(id,name,age));
return "已成功添加一个用户!";
}
@GetMapping("/updateUser/{id}/{name}/{age}")
public String updateUser(@PathVariable int id,@PathVariable String name,@PathVariable int age){
userMapper.updateUser(new User(id,name,age));
return "已成功更改用户信息!";
}
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable int id){
userMapper.deleteUser(id);
return "已成功删除一个用户!";
}
}
其中@Getmapping后为打开的接口
@PathVariable实现了restful风格
3、 测试 Test03ApplicationTestspackage com.xingye.test03;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
@SpringBootTest
class Test03ApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
查
localhost:8080/queryUserList
增
localhost:8080/add/{id}/{name}/{age}
删
localhost:8080/deleteUser/{id}
改
localhost:8080/updateUser/{id}/{name}/{age}



