1.创建一个springboot的maven项目(完成图中勾选处后,一路next继续)
设置项目的元数据,第一个是包结构(Group),第二个是项目标识(Artifact):
添加项目所需要的依赖,Web的依赖和数据库的依赖:
第一次建立项目,需要下载许多包,请耐心等待一下。最后pom.xml如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.ljy.project
springdemo
0.0.1-SNAPSHOT
springdemo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
手动添加的dependency需要 右键pom.xml->Maven->Reimport导入依赖。
2.在启动类同级目录下创建domain,dao,service,controller四个目录
在domain文件夹下创建User类:
package com.ljy.project.springdemo.domain;
public class User {
private String username;
private String password;
private String patternLock;
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;
}
public String getPatternLock() {
return patternLock;
}
public void setPatternLock(String patternLock) {
this.patternLock = patternLock;
}
}
然后在dao文件夹下创建UserDao接口:
package com.ljy.project.springdemo.dao;
import com.ljy.project.springdemo.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserDao {
// List selectUsers();
@Select("select * from user")
public List getAllUsers();
}
在service文件夹下创建UserService接口:
package com.ljy.project.springdemo.service;
import com.ljy.project.springdemo.dao.UserDao;
import com.ljy.project.springdemo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public interface UserService {
public ResultVo getAllUsers();
}
该结果返回值类型为ResultVo,在启动类同级目录下创建vo视图文件夹,在vo文件夹下创建ResultVo视图:
package com.ljy.project.springdemo.vo;
/*http请求返回的最外层对象*/
public class ResultVo {
private Integer code;
private String msg;
private T data;
private ResultVo(T data){
this.code = 0;
this.msg = "success";
this.data = data;
}
private ResultVo(CodeMsg cm){
if(cm==null){
return;
}else {
this.code = cm.getCode();
this.msg = cm.getMsg();
this.data = null;
}
}
public static ResultVo success(T data){
return new ResultVo(data);
}
public static ResultVo error(CodeMsg cm){
return new ResultVo(cm);
}
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;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
在vo文件夹下创建CodeMsg错误码视图:
package com.ljy.project.springdemo.vo;
public class CodeMsg {
private int code;
private String msg;
//错误码
public static CodeMsg SUCCESS = new CodeMsg(0,"success");
public static CodeMsg INSERT_ERROR = new CodeMsg(100001,"插入失败");
public static CodeMsg SELECT_ERROR = new CodeMsg(100002,"查询失败");
public static CodeMsg UPDATE_ERROR = new CodeMsg(100003,"更新失败");
public CodeMsg(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;
}
}
在service文件夹下创建Impl文件夹,在Impl文件夹下创建UserServiceImpl实现类:
package com.ljy.project.springdemo.service.Impl;
import com.ljy.project.springdemo.dao.UserDao;
import com.ljy.project.springdemo.domain.User;
import com.ljy.project.springdemo.service.UserService;
import com.ljy.project.springdemo.vo.CodeMsg;
import com.ljy.project.springdemo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public ResultVo getAllUsers() {
List allUsers = userDao.getAllUsers();
if(allUsers.size()==0){
return ResultVo.error(CodeMsg.SELECT_ERROR);
}else{
return ResultVo.success(allUsers);
}
}
}
此处别忘了加@Service注解,我忘加了,运行时报如下错误:
Description:
Field userService in com.ljy.project.springdemo.controller.UserController required a bean of type 'com.ljy.project.springdemo.service.UserService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.ljy.project.springdemo.service.UserService' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:54425', transport: 'socket'
Process finished with exit code 1
在controller文件夹下创建UserController类:
package com.ljy.project.springdemo.controller;
import com.ljy.project.springdemo.dao.UserDao;
import com.ljy.project.springdemo.domain.User;
import com.ljy.project.springdemo.service.UserService;
import com.ljy.project.springdemo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.xml.ws.RequestWrapper;
import java.util.List;
@RestController
@RequestMapping(value = "/try")
@EnableAutoConfiguration
public class UserController {
// @Autowired
// private UserDao userDao;
@Autowired
private UserService userService;
// @RequestMapping(value = "/user")
// public List getUsers(){
// return userDao.selectUsers();
// }
@RequestMapping(value = "/getAll")
public ResultVo getAllUsers(){
return userService.getAllUsers();
}
}
3.创建数据库(与domain中的实体类对应)
create table User
(
id int auto_increment
primary key,
username varchar(20) null,
password varchar(500) null,
patternlock varchar(30) null
);
向数据库中插入测试数据:
insert into User(username, password, patternlock) values ('zhangsan','123456','123456');
insert into User(username, password, patternlock) values ('lisi','123456','123456');
insert into User(username, password, patternlock) values ('wangwu','123456','123456');
4.在application.properties里面进行配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/你的数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
spring.datasource.username=你的用户名
spring.datasource.password=你的密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.typeAliasesPackage=你的实体类包名 (例子项目的包名是com.ljy.project.springdemo.domain)
mybatis.mapperLocations=classpath:mapper/*.xml
logging.level.com.shizhao.project.springdemo:DEBUG
server.port=你的服务端口号,要保证与其他服务不冲突,如8080
我的配置如下:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/dbgirl?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=newpass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.typeAliasesPackage=com.ljy.project.springdemo.domain
mybatis.mapperLocations=classpath:mapper/*.xml
logging.level.com.shizhao.project.springdemo:DEBUG
server.port=8080
5.启动程序,在浏览器地址栏输入http://localhost:8080/try/getAll,得到结果:
此处能看到返回值为json格式,是因为我使用了插件:
到此,创建springboot+mybatis且返回值带错误码的项目创建成功。
补充:
sql语句除了能通过注解写在UserDao中外,还能写在mapper.xml中。在resources文件夹下创建mapper文件夹,在mapper文件夹下创建UserMapper.xml文件:
在UserDao中使用:
在UserController中直接调用:
在浏览器地址栏中输入http://localhost:8080/try/user,结果如下:
完整版项目连接:https://download.csdn.net/download/skye_95/11120802
本来只是想分享,但是CSDN上传资源下载积分不能自己设定了,所以下面提供了百度网盘下载连接:
链接:https://pan.baidu.com/s/1v5CdCY-QOet5lG6Q0cGCHA
提取码:a07k