Springboot整合mybatis试验,为员工管理系统配置数据库做准备
2021-10-27
使用IDEA完成查看依赖
自行建表user 代码结构 代码详情 UserController.java4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.6 com web02 0.0.1-SNAPSHOT web02 web02 1.8 //手动导入mybatis //JDBC org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 //Web org.springframework.boot spring-boot-starter-jdbc //数据库MySql org.springframework.boot spring-boot-starter-web //Lombok mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
package com.xie.controller;
import com.xie.mapper.UserMapper;
import com.xie.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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("addUser")
public String addUser(){
userMapper.addUser(new User(106,"amao","123456"));
return "ok";
}
@GetMapping("updateUser")
public String updateUser(){
userMapper.updateUser(new User(106,"amao","000456"));
return "ok";
}
@GetMapping("deleteUser")
public String deleteUser(){
userMapper.deleteUser(106);
return "ok";
}
}
UserMapper.java
package com.xie.mapper;
import com.xie.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);
}
User.java
package com.xie.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
Web02Application.java
UserMapper.xml保持默认,不可更改
application.properties
(个人配置文件)
spring.datasource.username=root spring.datasource.password=095810 spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.xie.pojo mybatis.mapper-locations=classpath:mybatis/mapper/*.xml测试连接 Web02ApplicationTests.java
package com.xie;
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.Connection;
import java.sql.SQLException;
@SpringBootTest
class Web02ApplicationTests {
//DI注入数据源
@Autowired
DataSource dataSource;
@Test
public void contextLoads() throws SQLException {
//看一下默认数据源
System.out.println(dataSource.getClass());
//获得连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
//关闭连接
connection.close();
}
}
连接结果
输出默认数据源,且获得连接
CURD测试localhost:8080/queryUserList
localhost:8080/addUser #以下操作均返回“ok”
localhost:8080/updateUser
localhost:8080/deleteUser



