超详细的 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.谁在使 ...
随机推荐
- 【TouchGFX】Callback
回调函数模板定义 单参数回调函数模板 实现回调函数接口: 实现合法性检查接口: 实现执行接口: 按键触发回调实现 定义回调数据结构对象 使用回调数据结构构造函数 执行接口实现 整个切换机制的管理主体对 ...
- tomcat 一闪而过 ( 解决方案 )
配置JAVA_HOME,注意变量值是jdk的主目录,不是bin目录,并且不要加分号
- [转帖]快速定位MySQL数据库当前消耗CPU最高的sql语句
概述 One of our customers recently asked whether it is possible to identify, from the MySQL side, the ...
- [转帖]一文说清 Linux System Load
https://zhuanlan.zhihu.com/p/447661302 双十一压测过程中,常见的问题之一就是load 飙高,通常这个时候业务上都有受影响,比如服务rt飙高,比如机器无法登录,比如 ...
- [转帖]HTTP2 Sampler for JMeter
https://www.cnblogs.com/a00ium/p/10462572.html 今天开发大大说能不能帮忙压一下HTTP2的链接,便去查了一下相关的东西. HTTP 2.0 的出现,相比于 ...
- [转帖]Fiddler抓取Chrome浏览器访问baiud.com报NET::ERR_CERT_COMMON_NAME_INVALID
错误现象 解决方法: 1.Chrome浏览器地址栏中输:chrome://net-internals/#hsts 2.在Query HSTS/PKP domain处搜索www.baidu.com网站, ...
- [转帖]jmeter SSL证书相关配置
在实际工作中,我们大多数接口都是用的HTTPS来保证安全,使用jmeter测试HTTPS请求是如何配置证书呢? 1.最简单的方法,在选项里选择SSL管理器,然后选择相应的证书即可 在弹出的选择框选择证 ...
- [转帖]利用Python调用outlook自动发送邮件
↓↓↓欢迎关注我的公众号,在这里有数据相关技术经验的优质原创文章↓↓↓ 使用Python发送邮件有两种方式,一种是使用smtp调用邮箱的smtp服务器,另一种是直接调用程序直接发送邮件.而在outlo ...
- [转帖]ssh时不输入YES
vim /etc/ssh/ssh_config 60行新添加 StrictHostKeyChecking no
- [转帖]LTP测试
https://zhuanlan.zhihu.com/p/381538099 整体测试 直接运行runltp命令,将测试/opt/ltp/scenario_groups/default文件中所有的 ...