网上找了很多资料,学习如何搭建springboot,由于刚刚接触springboot,不是很熟练,通过参考网上别人搭建的例子,自己也搭建了一个简单的springboot+mybaits+maven

网上说springboot很简单,可是很多人都掉了很多次坑,但是说破了什么都不是~跟着我的步骤,你也很快可以迅速搭建一个框架啦~

spring boot 主要是专注于后端的东西,最后能放回一个json数据算成功

如果使用spring boot搭建框架,最好不要用jsp来作为视图的展示,spring boot有内嵌的thymeleaf模板引擎(这东西正在了解中),所以最好用thymeleaf来作为前端页面展示,thymeleaf可以完全替代jsp

一、项目结构

Application必须跟其他包在同一级,Application跟其他包放在springboottest目录下(springboottest记得要跟groupid一样?不大确定)

二、maven

具体安装maven,请结合上一篇文章——springboot搭建Hello World

三、启动类

package springboottest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* Created by JC on 2017/2/21.
* 启动文件
*/
@SpringBootApplication
public class Application {
public static void main(String[] args)
{
SpringApplication.run(Application.class,args);
} }

  

四、controller

package springboottest.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboottest.entity.User;
import springboottest.service.UserService; import javax.annotation.Resource;
import java.util.List; /**
* Created by JC on 2017/2/22.
*/
@RestController
@RequestMapping(value = "/user")
public class UserController { @Resource(name = "userService")
private UserService userService; @RequestMapping(value = "/findAll")
public ResponseEntity<List<User>> findAll(){
return new ResponseEntity( userService.findAll(), HttpStatus.OK);
} }

  

五、service

package springboottest.service;

import org.omg.CORBA.Object;
import springboottest.entity.User; import java.util.List; /**
* Created by JC on 2017/2/22.
*/
public interface UserService { public List<Object> findAll();
}

  

package springboottest.service.impl;

import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboottest.dao.DaoSupport;
import springboottest.entity.User;
import springboottest.service.UserService; import javax.annotation.Resource;
import java.util.List; /**
* Created by JC on 2017/2/22.
*/
@Service("userService")
public class UserServiceImpl implements UserService { @Resource(name = "daoSupport")
private DaoSupport dao; @Override
public List<Object> findAll() {
return dao.findAll("UserMapper");
}
}

  

六、dao 

package springboottest.dao;

import org.omg.CORBA.Object;
import springboottest.entity.User; import java.util.List; /**
* Created by JC on 2017/2/22.
*/
public interface Dao { /**
* 查找全部user
*/
public List<Object> findAll(String str);
}

  

package springboottest.dao;

import org.mybatis.spring.SqlSessionTemplate;
import org.omg.CORBA.Object;
import org.springframework.stereotype.Repository; import javax.annotation.Resource;
import java.util.List; /**
* Created by JC on 2017/2/22.
*/
@Repository("daoSupport")
public class DaoSupport implements Dao{ @Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate; /**
* 查询全部
* @param str
* @return
*/
@Override
public List<Object> findAll(String str) {
return sqlSessionTemplate.selectList(str+".findAll");
} }

  

 

七、entity

package springboottest.entity;

/**
* Created by JC on 2017/2/22.
*/
public class User { private String F_ID; private String F_USERNAME; private String F_PASSWORD; public String getF_ID() {
return F_ID;
} public void setF_ID(String f_ID) {
F_ID = f_ID;
} public String getF_USERNAME() {
return F_USERNAME;
} public void setF_USERNAME(String f_USERNAME) {
F_USERNAME = f_USERNAME;
} public String getF_PASSWORD() {
return F_PASSWORD;
} public void setF_PASSWORD(String f_PASSWORD) {
F_PASSWORD = f_PASSWORD;
}
}

  

八、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="UserMapper"> <!--表名 -->
<sql id="tableName">
t_user
</sql> <!-- 字段 -->
<sql id="Field">
F_ID,
F_USERNAME,
F_PASSWORD
</sql> <!-- 字段值 -->
<sql id="FieldValue">
#{F_ID},
#{F_USERNAME},
#{F_PASSWORD}
</sql> <!-- 列表(全部) -->
<select id="findAll" resultType="springboottest.entity.User">
select
<include refid="Field"></include>
from
<include refid="tableName"></include>
</select> </mapper>

  

九、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> <groupId>springboottest</groupId>
<artifactId>springboottest</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
</parent> <dependencies> <!--springboot的基础包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--mysql的依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!--加入mybatis-spring-boot-stater-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies> </project>

  

十、application.yml

# Server settings
server:
port: 8080
address: localhost # DATASOURCE
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&charaterEncoding=utf-8
username: root
password: root # Spring profiles
spring:
http:
encoding:
charset: utf-8
enabled: true
force: true # Mybatis
mybatis:
mapper-locations: classpath:/mybatis/*.xml
type-aliases-package: springboottest.entity

  

十一、数据库

/*
SQLyog v10.2
MySQL - 5.1.62-community : Database - test
*********************************************************************
*/ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `test`; /*Table structure for table `t_user` */ DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` (
`F_ID` int(11) NOT NULL,
`F_USERNAME` varchar(200) DEFAULT NULL,
`F_PASSWORD` varchar(200) DEFAULT NULL,
PRIMARY KEY (`F_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Data for the table `t_user` */ insert into `t_user`(`F_ID`,`F_USERNAME`,`F_PASSWORD`) values (1,'小李','123'),(2,'小张','456'); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

  

十二、访问,结果返回json数据

右击Application.java-->Run'Application' 启动启动类

访问路径:不需要加项目名字

菜鸟——springboot+mybatis+maven的更多相关文章

  1. SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查

    SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...

  2. springboot+mybatis+maven角色权限框架

    发布时间:2018-10-24   技术:springboot,mybatis,maven,shiro   概述 Springboot作为基础框架,使用mybatis作为持久层框架 使用官方推荐的th ...

  3. Spring-boot+Mybatis+Maven+MySql搭建实例

    转自:https://www.jianshu.com/p/95fb7be049ae 最近读了spring-boot开发手册,spring-boot相比于spring-mvc封装了很多常用的依赖,并且内 ...

  4. IDEA+SpringBoot+Mybatis+maven分布式项目框架的搭建

    参考文章:https://blog.csdn.net/qq_34410726/article/details/98214992 一.maven分布式工程的基本架构 demo  #父工程模块,主要用来定 ...

  5. SpringBoot+Mybatis+Maven+MySql小案例

    数据准备: 建表t_user ,插入数据..... 创建工程 构建pom.xml <?xml version="1.0" encoding="UTF-8" ...

  6. Java微服务(Spring-boot+MyBatis+Maven)入门教程

    1,项目创建    新建maven项目,如下图: 选择路径,下一步 输入1和2的内容,点完成 项目创建完毕,结构如下图所示: 填写pom.xml里内容,为了用于打包,3必须选择jar,4和5按图上填写 ...

  7. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  8. (一 、上)搭建简单的SpringBoot + java + maven + mysql + Mybatis+通用Mapper 《附项目源码》

    最近公司一直使用 springBoot 作为后端项目框架, 也负责搭建了几个新项目的后端框架.在使用了一段时间springBoot 后,感觉写代码 比spring 更加简洁了(是非常简洁),整合工具也 ...

  9. springboot + mybatis 前后端分离项目的搭建 适合在学习中的大学生

    人生如戏,戏子多半掉泪! 我是一名大四学生,刚进入一家软件件公司实习,虽说在大学中做过好多个实训项目,都是自己完成,没有组员的配合.但是在这一个月的实习中,我从以前别人教走到了现在的自学,成长很多. ...

随机推荐

  1. 电脑连接树莓派Pi Zero W

    作者:陈拓 chentuo@ms.xab.ac.cn 2018.05.16/2018.06.09 0.  概述 本位介绍两种电脑连接树莓派Pi Zero W的方法: 电脑通过USB以太网连接树莓派Ze ...

  2. python 解析 yaml文件

    import yaml with open("./test.yaml") as f: x = yaml.load(f) print(x) [{'tasks': [{'yum': { ...

  3. mysql异常

    一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分析:这说明“localhost”计算机 ...

  4. Mysql操作日志

    任何一种数据库中,都有各种各样的日志.MySQL也不例外,在Mysql中有4种不同的日志.分别错误日志.二进制日志.查询日志和慢查询日志.这些日志记录着Mysql数据库不同方面的踪迹.下文将介绍这4种 ...

  5. nginx 的 upstream timed out 问题

    nginx 作为负载服务,表现为网站访问很慢,有些文件或页面要等待到60s才会返回,我注意到60s就是超时时间,但是超时后返回状态是正常值200,网站可以正常打开,就是会一直等待到超时才打开,而且问题 ...

  6. Python requests 使用心得

    最近在用requests写一些项目,遇见了一些问题,百度了很多,有些都不太好使,最后看了下requestsAPI文档,才明白了很多,最后项目趋于稳定.看来学东西还是API文档比较权威啊~ 问题场景 项 ...

  7. OC线程操作-GCD介绍

    1. GCD介绍 1.11.2 1.3 异步具备开启能力但是不是 一定可以开启 1.4 1.5 67. 8.

  8. 动态加载JS,并执行回调函数

    有些时候我们需要动态的加载一些JS,并在JS加载完成后执行一些回调函数. var loadscript = { $$: function (id) { return document.getEleme ...

  9. jdeveloper基础教程(中文版)

    jdeveloper基础教程(中文版) 程序员的基础教程:菜鸟程序员

  10. struts2框架值栈的概述之问题一:什么是值栈?

    1. 问题一:什么是值栈? * 值栈就相当于Struts2框架的数据的中转站,向值栈存入一些数据.从值栈中获取到数据. * ValueStack 是 struts2 提供一个接口,实现类 OgnlVa ...