数据库启动后开始 #代表注释
-
在application.yml文件中绑定你的本地数据库
#(DBName代表数据库中名称) spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/DBName?characterEncoding=UTF-8&serverTimezone=GMT%2B8 username: root password: password #(classpath:sqlmapper/*.xml代表你在springboot中的sql的语句,sqlmapper是放mapper.xml的包名,注意空格是必须要有这是yml文件识别的一直样式) mybatis: typeAliasesPackage: com.app.demoapp.entiy mapperLocations: classpath:mapper/*.xml
2.你在springboot中的sql的语句(classpath:sqlmapper/*.xml的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" >
#(每个语句中 id=“方法名”中的方法名必须在UserDao中存在这个方法;parameterType代表输入的实体类,resultType代表输出的实体类)
<mapper namespace="com.app.demoapp.dao.UserDao" >
<select id="login" parameterType="com.app.demoapp.entiy.UserBean" resultType="com.app.demoapp.entiy.UserBean" >
select *
from `user`
where user_name = #{user_name,jdbcType=VARCHAR}
and user_pass=#{user_pass,jdbcType=VARCHAR}
</select>
</mapper>
3.在1和2中都是资源文件中的 UserDao
@Mapper
public interface UserDao {
User login(User user);
}
4.实体类User
public class User {
String user_name;
String user_pass;
int user_id;
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_pass() {
return user_pass;
}
public void setUser_pass(String user_pass) {
this.user_pass = user_pass;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
}
6.在DemoApplication中加@MapperScan("com.meapp.dao")声明dao存在包的位置
@SpringBootApplication
@MapperScan("com.app.demoapp.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
7.这个是返回值的基本样式
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResult<T> {
/** 代码 */
private int code = 0;
/** 消息 */
private String msg;
/** 数据 */
private T data;
public ApiResult(ApiResult<T> origin) {
this.code = origin.code;
this.msg = origin.msg;
this.data = origin.data;
}
public ApiResult() {
}
public ApiResult(T data) {
this.data = data;
}
/**
* 获取 数据列表
*
* @return data 数据列表
*/
public T getData() {
return this.data;
}
/**
* 设置 数据列表
*
* @param data 数据列表
*/
public void setData(T data) {
this.data = data;
}
/**
* 获取 代码
*
* @return code 代码
*/
public int getCode() {
return this.code;
}
/**
* 设置 代码
*
* @param code 代码
*/
public void setCode(int code) {
this.code = code;
}
/**
* 获取 消息
*
* @return msg 消息
*/
public String getMsg() {
return this.msg;
}
/**
* 设置 消息
*
* @param msg 消息
*/
public void setMsg(String msg) {
this.msg = msg;
}
}
8.返回值的样式
@SuppressWarnings("unchecked")
public class ApiResultBuilder {
private ApiResult target;
private ApiResultBuilder() {
this.target = new ApiResult();
}
public static ApiResultBuilder create() {
return new ApiResultBuilder();
}
public ApiResultBuilder code(int code) {
target.setCode(code);
return this;
}
public ApiResultBuilder msg(String msg) {
target.setMsg(msg);
return this;
}
public ApiResultBuilder data(Object data) {
target.setData(data);
return this;
}
public ApiResult build() {
return new ApiResult(target);
}
}
9.最后的胜利接口的写法
@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class Login {
@Autowired
UserDao mUserDao;
@PostMapping(value = "/login")
@ResponseStatus(code = HttpStatus.OK)
public ApiResult<User> login(@RequestParam(required = true) String name,
@RequestParam(required = true) String pass){
User model = new User();
model.setUser_name(name);
model.setUser_pass(pass);
User resultUser = mUserDao.login(model);
return ApiResultBuilder.create().code(1).msg("登录成功").data(resultUser).build();
}
@PostMapping(value = "/info")
@ResponseStatus(code = HttpStatus.OK)
public ApiResult<LoginModel> getInfo(@RequestBody LoginModel model1){
LoginModel model = new LoginModel();
model.setName("new123456");
model.setPass("222");
model.setToken("123456");
model.setIsNull("");
return ApiResultBuilder.create().code(1).msg("登录成功").data(model).build();
}
}