目录结构如下,从第二步开始做起,不要直接全创了
创建项目的时候导入Web,Log4j,JDBC,Mysql驱动,等依赖
3.resources下创建application.properties4.0.0 org.springframework.boot spring-boot-starter-parent2.6.4 com.springboot_mybatis springboot_mybatis0.0.1-SNAPSHOT springboot_mybatis Demo project for Spring Boot 1.8 com.alibaba druid1.1.21 log4j log4j1.2.17 org.mybatis.spring.boot mybatis-spring-boot-starter2.1.1 org.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-starter-webmysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-plugin
spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #ÕûºÏmybatis mybatis.type-aliases-package=com.springboot_mybatis.pojo mybatis.mapper-locations=classpath:mapper/*.xml4.在test中测试能否连接成功
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
5.测试成功后,创建实体类User和Usermapper接口
User.class
@Component
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
'}';
}
}
接口UserMapepr.class
@Mapper
@Repository
public interface UserMapper {
//获取所有员工的信息
List queryUserList();
//通过id查询一个员工
User queryUserById(int id);
//新增一个员工
int addUser(User user);
//修改一个员工
int updateUser(User user);
//通过id删除员工信息
int deleteUser(int id);
}
6.UserMapper接口的.xml文件
resource/mapper/UserMapper.xml
7.我这里直接创建Controller,可以先创建一个Service进行封装,然后用Controller去调
controller/UserController.class
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/queryUserList")
public List queryUserList() {
List userList = userMapper.queryUserList();
return userList;
}
@RequestMapping("/queryUserById/{id}")
public User queryUserById(@PathVariable("id") Integer id) {
User user = userMapper.queryUserById(id);
return user;
}
@RequestMapping("/addUser")
public String addUser() {
User user = new User();
user.setUsername("五毛");
user.setPassword("5555555");
userMapper.addUser(user);
return "add_ok";
}
@RequestMapping("/updateUser")
public String updateUser() {
User user = new User(8, "五毛", "6666");
userMapper.updateUser(user);
return "update_ok";
}
@RequestMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") Integer id) {
userMapper.deleteUser(id);
return "delete_ok";
}
}
8.启动主启动类进行测试,访问http://localhost:8080/+路径
StudentApplication.class
@SpringBootApplication
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}
}



