-
创建一个maven项目作为父项目,在父项目的pom文件中导入springboot的依赖 spring-boot-starter 2.7.x版本以及h2依赖
-
创建第一个子模块core里面合并了common和dao直接包含了数据访问相关的一切,在pom文件中导入lombok依赖用于使用@Data注解,以及导入mybatis的依赖 mybatis-spring-boot-starter,这里我使用的是2.2.0版本 创建实体类User
package com.yejian.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
以及mapper接口
package com.yejian.Mapper;
import com.yejian.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Insert("insert into user (name,age) values (#{name},#{age})")
void insert(User user);
@Select("select * from user")
List<User> findAll();
}
在这里我直接使用了注解的方式省去了在xml文件中编写sql语句的过程
- 创建子模块service,并在其service模块中引入core依赖
- 创建Userservice接口以及UserserviceImpl类进行业务模块的逻辑应用设计
package com.yejian.Service.Impl;
import com.yejian.Mapper.UserMapper;
import com.yejian.Service.UserService;
import com.yejian.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void insert(User user) {
userMapper.insert(user);
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
package com.yejian.Service;
import com.yejian.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
void insert(User user);
List<User> findAll();
}
- 创建web子模块,并引入web依赖spring-boot-starter-web,依赖core,service,以及mysql依赖5.x版本(!若使用2.x版本的springboot在使用8.x版本的mysql会产生依赖冲突)
- 在resources文件夹下面添加application.properties用于存放数据库信息
spring.application.name=TestWeb
server.port=8080
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ts_db?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.yejian.entity
logging.level.com.yejian.Mapper=DEBUG
随后创建UserController类
package com.yejian.Controller;
import com.yejian.Service.UserService;
import com.yejian.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/ll")
public void insert(@RequestBody User user) {
userService.insert(user);
}
@GetMapping("/findall")
public List<User> findAll() {
return userService.findAll();
}
}
- OK到这里们基本的框架都已经写完了,接下来写启动类 在web模块下创建一个Test类
package com.yejian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.yejian.Mapper")
public class Test {
public static void main(String[] args) {
SpringApplication.run(Test.class,args);
}
}
随后启动项目 去postman测一下接口 如图所示
这里我的数据库只有一个数据所以只能查处一个数据
好了到此为止这个简单的用maven搭建多模块项目,基于springboot整合mvc和mybatis,完成简单单表操作就完成了
- 最后看一下我的项目结构