-
创建spring boot项目
项目结构
-
配置pom.xml文件(只列出了主要配置)
<!-- 数据库连接驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- Mybatis Plus 依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency> <!-- lombok插件,简化开发 --> <!-- 通过@Data等注解可以省略生成其属性的构造器、getter、setter、equals、hashcode、toString方法 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
-
application.yml 配置文件设置Mybatis Plus相关配置
#连接数据库的配置 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false username: root password: root #如果没有xml映射文件的话,可以省略 #mybatis-plus: # mapper-locations: classpath:mapper/*Mapper.xml
-
创建实体类
import lombok.Data; /** * @author WangBoYi * @date 2019/3/14 16:51 * @Description */ @Data public class User { private Integer id; private String username; private String password; private String Phone; }
-
定义mapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.mp.demo.entity.User; /** * @author WangBoYi * @date 2019/3/14 16:54 * @Description */ public interface UserMapper extends BaseMapper<User> { //如果有需要,可以在此添加方法并编写相应的xml映射文件,同mybatis }
BaseMapper < T > 该接口中封装了CRUD的基础操作方法。
-
创建mapper.xml映射文件(格式同mybatis,如果不需要可以省略)
-
在启动类中添加@MapperScan注解扫描mapper接口
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.mp.demo.mapper") public class SbmpApplication { public static void main(String[] args) { SpringApplication.run(SbmpApplication.class, args); } }
-
创建IUserService接口
import com.mp.demo.entity.User; import java.util.List; /** * @author WangBoYi * @date 2019/3/14 17:04 * @Description */ public interface IUserService { /** * 显示用户列表 * @return */ List<User> selectAllUser(); }
-
创建UserService实现IUserService接口
import java.util.List; /** * @author WangBoYi * @date 2019/3/14 17:07 * @Description */ @Service public class UserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; @Override public List<User> selectAllUser() { //selectList(null)方法调用的就是BaseMapper接口里面封装的方法 return userMapper.selectList(null); } }
-
创建UserController类
import com.mp.demo.entity.User; import com.mp.demo.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * @author WangBoYi * @date 2019/3/14 17:08 * @Description */ @Controller public class UserController { @Autowired IUserService iUserService; @ResponseBody @RequestMapping("/userList") public List<User> userList(){ return iUserService.selectAllUser(); } }
-
通过postman调用该接口
-
关键步骤
pom.xml添加依赖,application.yml添加配置,定义mapper接口继承BaseMapper,
添加@MapperScan()注解。 -
lombok常用注解
@Data:注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter:注解在属性上:为属性提供 setting 方法, 注解再类上表示当前类中所有属性都生成setter方法
@Getter:注解在属性上:为属性提供 getting 方法,注解再类上表示当前类中所有属性都生成getter方法
@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法