作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决。
为基于Spring的开发提供更快的入门体验
开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求
提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等
SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式
下面给大家介绍一下,SpringBoot+SpringMVC+MyBatis快速整合搭建的过程,实现数据数据查询返回到前端进行展示;
1、首先使用Navicat for MySql连接MySql数据库,创建一个新的数据库或者使用某一个已经存在的数据库,我这里新创建了一个test数据库,并新建一张user表,建表SQL和插入测试数据的SQL如下:
<!--SQL建表语句--> CREATE TABLE `user` ( `id` INT (50) NOT NULL, `name` VARCHAR (100) NOT NULL, `age` INT (50) NOT NULL, `address` VARCHAR (200) NOT NULL, PRIMARY KEY (`id`) ); <!--插入数据--> INSERT INTO USER (id,name,age,address)VALUES(1,'小谢',18,'上海市'); INSERT INTO USER (id,name,age,address)VALUES(2,'小尚',18,'上海市'); INSERT INTO USER (id,name,age,address)VALUES(3,'小楠',18,'天津市'); INSERT INTO USER (id,name,age,address)VALUES(4,'小勋',18,'郑州市'); INSERT INTO USER (id,name,age,address)VALUES(5,'小乔',18,'郑州市'); INSERT INTO USER (id,name,age,address)VALUES(6,'小威',18,'郑州市'); INSERT INTO USER (id,name,age,address)VALUES(7,'小欢',18,'武汉市');
项目的相关信息填写一下;
点击Finish,等待项目创建完成;
1、SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent;
<!--SpringBoot的起步依赖spring-boot-starter-parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent>
同时整合SpringMVC,要导入web的启动依赖;
<dependencies> <!--web的启动依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
整合MyBatis,要导入mybatis起步依赖;
<!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
还有MySql连接驱动;
<!-- MySQL连接驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2、导入依赖坐标后的pom.xml文件为:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" 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"> <modelVersion>4.0.0</modelVersion> <!--SpringBoot的起步依赖spring-boot-starter-parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <groupId>com.xyfer</groupId> <artifactId>springbootDemo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--SpringBoot要集成SpringMVC启动依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- MySQL连接驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project>
3、配置数据库信息;
SpringBoot默认扫描classpath下面的application.properties文件,在src/main/resources文件夹下面创建application.properties文件:
#DB Configuration: spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root #spring集成Mybatis环境 #pojo别名扫描包 mybatis.type-aliases-package=com.xyfer.domain #加载Mybatis映射文件 mybatis.mapper-locations=classpath:mapper/*Mapper.xml
1、根据数据库表信息,在com.xyfer.domain包下面编写实体类User.java;
package com.xyfer.domain; public class User { private int id; private String name; private int age; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '/'' + ", age=" + age + ", address='" + address + '/'' + '}'; } }
2、在com.xyfer.controller包下面编写UserController.java类;
package com.xyfer.controller; import com.xyfer.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/test") @ResponseBody public String test(){ return userService.findAll().toString(); } }
3、在com.xyfer.service包下面编写接口UserService.java;
package com.xyfer.service; import com.xyfer.domain.User; import java.util.List; public interface UserService { /** * 查询所有用户信息 * @return */ public List<User> findAll(); }
4、在com.xyfer.service.impl包下编写实现类UserServiceImpl.java;
package com.xyfer.service.impl; import com.xyfer.dao.UserDao; import com.xyfer.domain.User; import com.xyfer.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> findAll() { return userDao.findAll(); } }
5、在com.xyfer.service.dao下编写接口UserDao.java;
package com.xyfer.dao; import com.xyfer.domain.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper public interface UserDao { /** * 查询所有用户信息 * @return */ public List<User> findAll(); }
6、由于在application.properties中配置的Mapper文件的位置如下;
7、所以在resources下新建文件夹mapper,并新建文件UserMapper.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.xyfer.dao.UserDao"> <select id="findAll" resultType="user"> select * from user; </select> </mapper>
至此,SpringBoot+SpringMVC+MyBatis快速整合搭建一个项目已经完成;
1、通过引导类启动项目,控制台打印项目信息,项目启动成功;
2、在浏览器输入访问地址:http://localhost:8080/test,成功返回数据库中一开始插入的数据;