超详细的 springboot & mybatis 程序入门
ps:网上有很多类似的入门案例,我也是看了被人的之后自己写的一个
估计有哥们懒 我把数据表格拿上来,数据自己填吧
CREATE TABLE `tb_user` (
`id` int(10) DEFAULT NULL,
`name` varchar(25) CHARACTER SET utf8 DEFAULT NULL,
`age` int(10) DEFAULT NULL,
`address` varchar(25) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
第一步 导入pom
<?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>
<groupId>com.bluecard.zh</groupId>
<artifactId>zh-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!--<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>-->
</resources>
</build>
</project>
第二步 建包
第三步 基本配置 application.properties
spring.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=gbk&zeroDateTimeBehavior\=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
#mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.mapper-locations=classpath*:com/bluecard/zh/mapper/sql/*Mapper.xml
mybatis.type-aliases-package=com.bluecard.zh.model
server.port= 9090
>>> UserController 类
package com.bluecard.zh.controller;
import com.bluecard.zh.model.User;
import com.bluecard.zh.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;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by zheng_fx on 2018-08-08 11:57
*/
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getUserInfo")
@ResponseBody
public List<User> getUserInfo(){
return userService.getUserInfo();
}
}
>>> UserService 类
package com.bluecard.zh.service;
import com.bluecard.zh.model.User;
import java.util.List;
/**
* Created by zheng_fx on 2018-08-08 13:23
*/
public interface UserService {
public List<User> getUserInfo();
}
>>> UserServiceImpl
package com.bluecard.zh.service.impl;
import com.bluecard.zh.mapper.UserMapper;
import com.bluecard.zh.model.User;
import com.bluecard.zh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by zheng_fx on 2018-08-08 13:23
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserInfo() {
return userMapper.getUserInfo();
}
}
>>> UserMapper
package com.bluecard.zh.mapper;
import com.bluecard.zh.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* Created by zheng_fx on 2018-08-08 13:24
*/
public interface UserMapper {
// @Select("select * from tb_user")
public List<User> getUserInfo();
}
>>> 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.bluecard.zh.mapper.UserMapper">
<resultMap id="userinfo" type="com.bluecard.zh.model.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="address" column="address"/>
</resultMap>
<select id="getUserInfo" resultType="User">
select * from tb_user
</select>
</mapper>
>>> 启动类 ApplicationStart
package com.bluecard.zh;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by zheng_fx on 2018-08-08 13:26
*/
@SpringBootApplication
@MapperScan("com.bluecard.*.mapper")
public class ApplicationStart {
public static void main(String[] args) {
SpringApplication.run(ApplicationStart.class,args);
System.out.println("================== SpringBoot Start Success ==================");
}
}
最后启动项目
测试地址: http://localhost:9090/user/getUserInfo
大功告成 !!!!
超详细的 springboot & mybatis 程序入门的更多相关文章
- SpringBoot+Mybatis整合入门(一)
SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...
- Java微服务(Spring-boot+MyBatis+Maven)入门教程
1,项目创建 新建maven项目,如下图: 选择路径,下一步 输入1和2的内容,点完成 项目创建完毕,结构如下图所示: 填写pom.xml里内容,为了用于打包,3必须选择jar,4和5按图上填写 ...
- 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)
[原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...
- (转)Springboot日志配置(超详细,推荐)
Spring Boot-日志配置(超详细) 更新日志: 20170810 更新通过 application.yml传递参数到 logback 中. Spring Boot-日志配置超详细 默认日志 L ...
- SpringCloud+MyBatis+Redis整合—— 超详细实例(二)
2.SpringCloud+MyBatis+Redis redis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写 ...
- 超强、超详细Redis数据库入门教程
这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么2.redis的作者何许人也3.谁在使用red ...
- 超强、超详细Redis数据库入门教程(转载)
这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么 2.redis的作者何许人也 3.谁在使 ...
- 超强、超详细Redis入门教程【转】
这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么2.redis的作者何许人也3.谁在使用red ...
- Python入门教程 超详细1小时学会Python
Python入门教程 超详细1小时学会Python 作者: 字体:[增加 减小] 类型:转载 时间:2006-09-08我要评论 本文适合有经验的程序员尽快进入Python世界.特别地,如果你掌握Ja ...
- 超详细Redis入门教程【转】
这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么 2.redis的作者何许人也 3.谁在使 ...
随机推荐
- 如何查看centos对于 TIME_WAIT 状态的 Socket 回收时间
要查看系统对于 TIME_WAIT 状态的 Socket 回收时间,可以通过以下方式查询 TCP 数据结构中的相关字段值: cat /proc/sys/net/ipv4/tcp_fin_timeout ...
- 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.12.18)
一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...
- [转帖]TLS 加速技术:Intel QuickAssist Technology(QAT)解决方案
https://zhuanlan.zhihu.com/p/631184323 3 人赞同了该文章 作者:vivo 互联网服务器团队- Ye Feng 本文介绍了 Intel QAT 技术方案,通过 ...
- [转帖]Strong crypto defaults in RHEL 8 and deprecation of weak crypto algorithms
https://access.redhat.com/articles/3642912 TABLE OF CONTENTS What policies are provided? Removed c ...
- [转帖]HikariCP常用监控指标与故障排查实战
编者有言:本书由资深数据库连接池专家撰写,褚霸.德哥.张亮.吴晟等近10位专家高度评价,从设计思想.功能使用.原理实现.工程实践.技术扩展5个维度对HikariCP进行全面讲解和深入分析. 本文将带你 ...
- 一个简单的科普-延迟与RT时间
一个简单的科普-延迟与RT时间 背景 发现稍微一复杂就没人看. 这次像是写一个简单的科普文章. 主要说一下网络延迟还有网络的响应时间. 这里想通过一个题目进行引申. 如果Skylink全球商用: 中国 ...
- Linux的free命令与OOM的简单总结
简介 查看操作系统配置最关键的几个命令是 lscpu 查看CPU信息 以及free 查看内存信息. 不过free信息有一个疑惑点 他的 free的值可能很小. 会让搭建产生误解. 这里简单说明一下. ...
- 使用Ant Design Vue的select搜索框出现的问题
Select 选择器进行搜索 <template> <div> <a-form-item label="分类:"> <a-select p ...
- Vue中this.$options.data()和this.$data知多少?
1.场所描述 如何获取vue-data中的所有值? 如何获取vue-data中的某一个值? 如何获取vue-data中的初始值? 如何设置data中的值位初始值? 2.主角登场 this.$optio ...
- windbg-windows调试工具来抓端游crash dump
windbg下载有两种方式: Install WinDbg - Windows drivers | Microsoft Learn 从微软应用商店下载 dump上的windows的局部变量解析部分进行 ...