Spring boot入门五 spring mvc spring boot mybatis配置整合

265 阅读1分钟

对于Spring Boot大家都已经有了基本的了解,不过Spring Boot他也仅仅是个封装集成引用的盒子而已,需要与具体的项目相结合才能真正的使用起来,

主要是记录下 使用Spring Boot实现 Spring Mvc 和mybatis的整合:

具体配置如下:

1、创建Maven项目,可以是web项目或者也可以是基本maven项目,

2、配置pom.xml

Spring boot入门五 spring mvc spring boot mybatis配置整合

3、配置属性文件

在resources目录下创建application.properties

#数据源驱动

Spring boot入门五 spring mvc spring boot mybatis配置整合

4、配置Mybatis

1)、创建Mapper接口文件

Spring boot入门五 spring mvc spring boot mybatis配置整合

2)、创建mapper.xml

在resources目录下创建mapper目录,并创建UserMapper.xml

/mapper/UserMapper.xml

Spring boot入门五 spring mvc spring boot mybatis配置整合

3)、创建实体类

Spring boot入门五 spring mvc spring boot mybatis配置整合

4、配置Controller类UserController

Spring boot入门五 spring mvc spring boot mybatis配置整合

5、配置数据库处理类

Spring boot入门五 spring mvc spring boot mybatis配置整合

数据库创建脚本:

Spring boot入门五 spring mvc spring boot mybatis配置整合

6、写个测试jason返回的公用类

package com.sam.project.mvc.common;

/**

* @ClassName: AjaxResult

* @Description: 封装返回数据

*/

public class AjaxResult {

private int retcode = 1;

private String retmsg = "操作成功";

private Object data;

public AjaxResult(int retcode, String retmsg, Object data){

this.retcode = retcode;

this.retmsg = retmsg;

this.data = data;

}

public AjaxResult(int retcode, String retmsg){

this.retcode = retcode;

this.retmsg = retmsg;

}

public AjaxResult(Object data){

this.retmsg = "查询成功";

this.data = data;

}

public AjaxResult(int retcode){

this.retcode = retcode;

this.retmsg = "操作失败";

}

public AjaxResult(String retmsg){

this.retcode = 0;

this.retmsg = retmsg;

}

public AjaxResult(){

}

public int getRetcode() {

return retcode;

}

public void setRetcode(int retcode) {

this.retcode = retcode;

}

public String getRetmsg() {

return retmsg;

}

public void setRetmsg(String retmsg) {

this.retmsg = retmsg;

}

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

@Override

public String toString() {

return "AjaxResult [retcode=" + retcode + ", retmsg=" + retmsg + ", data=" + data + "]";

}

}

7、创建启动入口:

Spring boot入门五 spring mvc spring boot mybatis配置整合

好了,至此,配置完成,创建完成后目录如下:

Spring boot入门五 spring mvc spring boot mybatis配置整合

启动Application 类Main方法进行开启Spring Boot

Spring boot入门五 spring mvc spring boot mybatis配置整合