工具:idea,maven,mysql5.7
第一步:创建数据库,创建表,添加数据
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(32) NOT NULL,
`passWord` varchar(50) NOT NULL,
`realName` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
第二步:创建SpringBoot项目
一:pom.xml中导入Mybatis,mysql,lombok依赖
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--msql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--可能会出现xml配置文件无法生成,建议添加此配置-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
二:配置application.yml【修改数据库名】
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8&useSSL=true
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
第三步:编写代码
一:创建实体类
@Data //Set,Get
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
@ToString //toString方法
public class User {
private int id;
private String UserName;
private String passWord;
private String realName;
}
二:创建mapper接口
@Mapper
public interface UserMapper {
List<User> queryUserAll(User user);
}
三:创建service接口
public interface UserService {
List<User> queryUserAll(User user);
}
四:创建serviceImpl实现类
@Service
public class UserServiceImpl implements UserService {
@Resource
UserMapper userMapper;
@Override
public List<User> queryUserAll(User user) {
return userMapper.queryUserAll(user);
}
}
五:在resources下创建mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demohello.dao.UserMapper">
<select id="queryUserAll" resultType="com.example.demohello.pojo.User"
parameterType="com.example.demohello.pojo.User">
select * from user
<where>
<if test="id!=null and id!=''">
id=#{id}
</if>
<if test="UserName!=null and UserName!=''">
and UserName=#{UserName}
</if>
</where>
</select>
</mapper>
六:创建utils工具类,编写返回状态码
1、
//枚举类
public enum ResultCode {
SUCCESS(200,"成功"),
FAILED(201,"无数据返回"),
VALIDATE_FAILED(202,"参数验证失败"),
ERROR(500,"未知错误");
private int code;
private String msg;
ResultCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2、
public class BaseResponse {
private Integer code;
private String msg;
public BaseResponse() {
}
public BaseResponse(ResultCode code) {
this.code = code.getCode();
this.msg = code.getMsg();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3、
public class ResponseData<T> extends BaseResponse{
private T data;
public ResponseData(ResultCode code) {
super(code);
}
public ResponseData(ResultCode code, T data) {
super(code);
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> ResponseData<T> out(ResultCode code,T data){
return new ResponseData<>(code,data);
}
}
七:创建controller类
@Controller
public class UserController {
@Autowired
UserServiceImpl userService;
@ResponseBody
@GetMapping("/getListUserALL")
public ResponseData<List<User>> getListUserAll(User user){
List<User> users = userService.queryUserAll(user);
for (int i = 0; i < users.size(); i++) {
if(null != users.get(i)){
return ResponseData.out(ResultCode.SUCCESS,users);
}
}
return ResponseData.out(ResultCode.FAILED,users);
}
}
八:启动项目,浏览器访问
http://localhost:8080/getListUserALL
?九:用postman请求
?结束!
|