超详细的 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.谁在使 ...
随机推荐
- 【BUS】LIN Bus
概述 随着汽车内电子设备的增多,市场上对于成本低于 CAN 的总线的需求日益强烈,不同的车厂相继开发各自的串行通信(UART/SCI)协议,以在低速和对性能要求不高的场合取代CAN.由于不同车厂定义的 ...
- [官网]微软服务器TLS的支持情况
https://learn.microsoft.com/en-us/windows/win32/secauthn/protocols-in-tls-ssl--schannel-ssp-#tls-pro ...
- 通过宿主机查看K8S或者是容器内的Java程序的简单方法
通过宿主机查看K8S或者是容器内的Java程序的简单方法 背景 最近一个项目的环境出现了 cannot create native process 的错误提示 出现这个错误提示时, docker ex ...
- [转帖]SkyWalking告警使用
SkyWalking告警 SkyWalking提供了强大的监控告警功能,在监控到应用出现问题的时候,会调用webhook或者gRPC hook或者Wechat DingDing等工具报告警告信息 而且 ...
- [转帖]Kafka 性能优化与问题深究
Kafka 性能优化与问题深究 一.Kafka深入探究 1.1 kafka整体介绍 1. 1.1 Kafka 如何做到高吞吐.低延迟的呢? Kafka是一个分布式高吞吐量的消息系统,这里提下 Kafk ...
- node使用nodemailer发送邮件
安装模块 npm install nodemailer 代码 const nodemailer = require('nodemailer'); // 查找到有关QQ邮箱的相关信息在 /node_mo ...
- vite配置开发环境和生产环境
为什么需要境变量的配置 在很多的时候,我们会遇见这样的问题. 开发环境的接口是:http://test.com/api 但是我们的生产环境地址是:http://yun.com/api 此时,我们打包的 ...
- 【小测试】rust中的数组越界——好吧,这下证明rust不是零成本抽象了吧
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 1.编译期发现的数组越界 在数组下标是常量的情况下,编译期 ...
- python中可变参数与装饰器的例子
python的可变参数 方法定义 #*args是可以传list类型的可变参数,**kwargs是可以传dict的可变参数 def wrapper(*args, **kwargs): 使用示例 def ...
- 手撕Vuex-实现getters方法
经上一篇章介绍,完成了实现共享数据的功能,实现方式是在 Store 构造函数中将创建 Store 时将需要共享的数据添加到 Store 上面,这样将来我们就能通过 this.$store 拿到这个 S ...