简介

从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boot 的作用在于创建和启动新的基于 Spring 框架的项目。它的目的是帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用。Spring Boot 会选择最适合的 Spring 子项目和第三方开源库进行整合。大部分 Spring Boot 应用只需要非常少的配置就可以快速运行起来。

Spring Boot 包含的特性如下:

创建可以独立运行的 Spring 应用。

直接嵌入 Tomcat 或 Jetty 服务器,不需要部署 WAR 文件。

提供推荐的基础 POM 文件来简化 Apache Maven 配置。

尽可能的根据项目依赖来自动配置 Spring 框架。

提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。

没有代码生成,也没有 XML 配置文件。

好了,上面说这么多都是给下文做铺垫,感兴趣的朋友继续往下阅读吧。

大家都知道springboot搭建一个spring框架只需要秒秒钟。

下面给大家介绍一下springboot与mybatis的完美融合:

首先:创建一个名为springboot-mybatis的maven项目,记住:一定要maven哦,不懂maven的可以自己恶补一下maven知识,这里就不介绍maven了。

下面给出pom.xml的完整配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
springboot-mybatis
springboot-mybatis
1.0.0
war
springBoot-mybatis
Spring Boot project
org.springframework.boot
spring-boot-starter-parent
1.3.2.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
5.1.21
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-surefire-plugin
true

之后创建一个启动类:

package org.shenlan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by wangwei on 2016/9/2.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}

这样一个完整的springboot项目就完成了,是不是很简单。

接下来就可以整理与mybatis的东东了。

首先,创建配置文件:application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=1111

这里server.port=1111是定义了改项目的端口,默认的是8080.

然后,定义一个java的实体类:

package org.shenlan.web;
/**
* Created by wangwei on 2016/9/2.
*/
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

这里实体类的字段要和数据库的字段对应起来,不然就要取别名了。

之后,定义一个dao的接口:

package org.shenlan.web;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* Created by Administrator on 2016/9/2.
*/
@Mapper
public interface UserMapper {
@Select("select * from user where name = #{name}")
User findUserByName(@Param("name")String name);
}

@Mapper就是我们要与mybatis融合关键的一步,只要一个注解就搞定了。

哈哈哈,最后我们就来写一个测试类吧:

package org.shenlan.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by wangwei on 2016/9/2.
*/
@RestController
@RequestMapping({"/home"})
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping(value = "/user")
@ResponseBody
public String user(){
User user = userMapper.findUserByName("王伟");
return user.getName()+"-----"+user.getAge();
}
}

@RestController是对应的restful风格的控制器,@RequestMapping里面可以对应一个数组哦

效果如下:

springboot 文件下载range 合并 springboot整合了什么_maven

以上所述是小编给大家介绍的springboot与mybatis整合实例详解(完美融合),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。