快速创建一个SpringBoot项目并整合Mybatis
2019-09-15
一、Maven环境搭建
1.导入jar坐标
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.panda</groupId>
<artifactId>Panda_Maven_SpringBoot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Panda_Maven_SpringBoot Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 父起步依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- web起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<!-- 热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 自动根据属性内容提示配置文件的书写依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- 导入关联mybatis坐标依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- 导入mysql坐标依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<finalName>Panda_Maven_SpringBoot</finalName>
</build>
</project>
2.创建入口类
package com.panda.controller; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @author Jpanda
*/
//springboot入口类
@SpringBootApplication
//扫描mappers包
@MapperScan("com.panda.mappers")
public class SpringBootApplicationConfig {
// 程序入口
public static void main(String[] args) {
// run:运行入口类,参数即为入口类的字节码对象
SpringApplication.run(SpringBootApplicationConfig.class);
}
}
3.创建bean类
package com.panda.beans; /**
* @author Jpanda
*/
public class User {
private Integer id;
private String username;
private String password; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} 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 User(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
} public User() {
super();
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
} }
4.创建mapper接口
package com.panda.mappers; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.panda.beans.User; /**
* @author Jpanda
*/
@Mapper
public interface UserMapper {
public List<User> getUsers();
}
5.创建User对应数据库表(省略)
6.sql映射
<?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.panda.mappers.UserMapper">
<select id="getUsers" resultType="com.panda.beans.User">
select * from user
</select>
</mapper>
7.配置数据库连接,关系springboot和mybatis
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/user?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root #spring and mybatis
#pojo alias package
mybatis.type-aliases-package=com.panda.beans
#upload mapperconfig files
mybatis.mapper-locations=classpath:UserMapperConfig.xml
8.创建Controller
package com.panda.controller; import java.util.List; 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; import com.panda.beans.User;
import com.panda.mappers.UserMapper; /**
* @author Jpanda
*/
@Controller
public class MybatisController {
@Autowired
private UserMapper userMapper; @RequestMapping("/mybatis")
@ResponseBody
public List<User> getUsers() {
List<User> users = userMapper.getUsers();
System.out.println(users);
return users;
}
}
9.访问测试

快速创建一个SpringBoot项目并整合Mybatis的更多相关文章
- 如何使用IDEA快速创建一个springboot项目
如何使用IDEA快速创建一个springboot项目 https://jingyan.baidu.com/article/0964eca24fdd938284f53640.html
- 快速创建一个springboot项目
创建一个maven项目(springboot.mybatis-plus) 目标:可以访问ftl页面.对象(json字符串),可以进行单元测试 1.新建一个maven项目,选择模板maven-arche ...
- SpringBoot2.x入门:快速创建一个SpringBoot应用
前提 这篇文章是<SpringBoot2.x入门>专辑的第2篇文章,使用的SpringBoot版本为2.3.1.RELEASE,JDK版本为1.8. 常规的套路会建议使用Spring官方提 ...
- 快速构建一个springboot项目(一)
前言: springcloud是新一代的微服务框架而springboot作为springcloud的基础,很有必要对springboot深入学习一下. springboot能做什么? (1)spri ...
- 从零开始的SpringBoot项目 ( 四 ) 整合mybatis
一.创建一个SpringBoot项目 从零开始的SpringBoot项目 ( 二 ) 使用IDEA创建一个SpringBoot项目 二.引入相关依赖 <!--mysql数据库驱动--> & ...
- springboot:快速构建一个springboot项目
前言: springboot作为springcloud的基础,springboot的热度一直很高,所以就有了这个springboot系列,花些时间来了解和学习为自己做技术储备,以备不时之需[手动滑稽] ...
- spting Boot 创建一个springBoot项目
spting Boot 创建一个springBoot项目 1)学习springBoot使用软件:IDEA软件(前面的文章有安装idea的过程). 也可以使用另一种方法在https://start.sp ...
- 使用IDEA创建一个springboot项目
工欲善其事,必先利其器. 不难发现,还是有很多小朋友在使用eclipse开发java项目.当你接触IDEA后,一切都变得美好了. 使用IDEA创建一个springboot项目是一件极其简单的事情.界面 ...
- 从零开始的SpringBoot项目 ( 二 ) 使用IDEA创建一个SpringBoot项目
工欲善其事 , 必先利其器 . IntelliJ IDEA 2019.3.3 x64的安装与破解 下面详细说明下如何使用idea创建我们的第一个springboot项目: 首先打开idea主界面选择 ...
随机推荐
- Zabbix监控win10系统
Zabbix监控win10系统 1. 在win10下安装zabbix-agent zabbix-agent下载地址:https://www.zabbix.com/downloads/4.2.6/zab ...
- 新的log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- OFF < FATAL < ERROR & ...
- Spring - 环境安装
安装IDEA的非Community版本和Java的包之后就可以用Java来HelloWorld了. 然后去这个链接:https://github.com/spring-guides/gs-rest-s ...
- Java中使用md5进行hash运算
public class Md5Util { /** * @author Bean_bag * @description 进行Hash运算 * * @param input 参数字符串 * @retu ...
- Centos7 node npm升级版本
安装npm 官网:https://nodejs.org/en/download/ 下载LTS Linux Binaries (x64)版本. # tar xf node-v10.16.3-linux- ...
- NetCore与 NET Framework 不同的地方
.net core 2.0没有了request.inputstream但是可以用request.body替代dataset 没有查看视图了控制台程序默认生成是dll 文件 public string ...
- 走进JavaWeb技术世界11:单元测试框架Junit
JUnit你不知道的那些事儿 转自 老刘 码农翻身 2016-02-24 话说有一次Eric Gamma 坐飞机的时候偶遇Kent Beck(对,就是极限编程和TDD的发起人) , 两位大牛见面寒暄 ...
- 2018-2019-2 《网络对抗技术》 Exp7 网络欺诈防范 20165222
1.实践目标 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 2.实践内容 (1)简单应用SET工具建立冒名网站 (1分) apachectl start开启服务 ser ...
- [Android] i2c-toos 在 Android 上使用
CPU:RK3399 系统:Android 7.1 i2c-tools 是一款免费开源的工具,可以检测 i2c 总线上的设备,可以读写寄存器等等 可以从下面路径下载需要的版本: https://www ...
- [RK3399] 调试串口ttyS2改为普通串口
CPU:RK3399 系统:Android 7.1 串口需求量大时,会选择使用 spi 转串口,但是数据量大或者波特率较高时,传输会丢包. 调试串口 ttyS2 也可以让出来,供上层使用,下面是将 t ...