Spring Boot应用连接数据库MySQL、及一个简单的demo
一、修改pom.xml文件
在项目的pom.xml文件上增加如下代码,添加依赖文件。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
二、设置全局配置文件
在src/main/resources/application.properties中设置数据源和jpa配置。标红处是自己建的库
spring.datasource.url=jdbc:mysql://123.207.46.41:3306/gwfdemo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=
spring.datasource.max-wait=
spring.datasource.min-idle=
spring.datasource.initial-size=
server.port=
server.servlet.session.timeout=
server.tomcat.uri-encoding=UTF-
三、分层业务逻辑

POJO:domain层
package com.example.demo.Domain;
public class Student {
private Integer id;
private String name;
private Integer age;
private String grade;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
service层:
package com.example.demo.Service; import com.example.demo.Dao.StudentDao;
import com.example.demo.Domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class StudentService {
@Autowired StudentDao studentDao;
public List<Student> getAll(){
return studentDao.getAll();
}
}
dao层:
package com.example.demo.Dao; import com.example.demo.Domain.Student;
import org.apache.ibatis.annotations.Select; import java.util.List; public interface StudentDao {
@Select("select * from student")
public List<Student> getAll();
}
Controller层:
package com.example.demo.controller; import com.example.demo.Domain.Student;
import com.example.demo.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@ResponseBody
@RequestMapping("/student")
public List<Student> getStudents(){
return studentService.getAll();
}
}
然后运行,即可查出数据,很简单的一个demo,但是代码结构大部分如此
Spring Boot应用连接数据库MySQL、及一个简单的demo的更多相关文章
- Spring Boot+Jpa(MYSQL)做一个登陆注册系统(前后端数据库一站式编程)
Spring Boot最好的学习方法就是实战训练,今天我们用很短的时间启动我们第一个Spring Boot应用,并且连接我们的MySQL数据库. 我将假设读者为几乎零基础,在实战讲解中会渗透Sprin ...
- (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...
- Spring Boot 快速入门 史上最简单
1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...
- 面试被问为什么使用Spring Boot?答案好像没那么简单
面试官:项目中有使用Spring Boot吗? 小小白:用过. 面试官:说一下为什么要使用Spring Boot? 小小白:在使用Spring框架进行开发的过程中,需要配置很多Spring框架包的依赖 ...
- spring boot中连接数据库报错500(mybatis)
spring boot中连接数据库报错500(mybatis) pom.xml中的依赖 <!-- 集成mybatis--> <dependency> <groupId&g ...
- spring boot开发,jar包一个一个来启动太麻烦了,写一个bat文件一键启动
spring boot开发,jar包一个一个来启动太麻烦了,写一个bat文件一键启动 @echo offcd D:\workProject\bushustart cmd /c "title ...
- Spring Boot MyBatis 连接数据库
最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...
- (转) Spring Boot MyBatis 连接数据库
最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...
- Spring Boot JPA 连接数据库
本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...
随机推荐
- 1017 Queueing at Bank (25)(25 point(s))
problem Suppose a bank has K windows open for service. There is a yellow line in front of the window ...
- opencv 加载 修改 保存 图像
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; /* 1 加载图像 cv::imre ...
- loj#2537. 「PKUWC2018」Minimax
题目链接 loj#2537. 「PKUWC2018」Minimax 题解 设\(f_{u,i}\)表示选取i的概率,l为u的左子节点,r为u的子节点 $f_{u,i} = f_{l,i}(p \sum ...
- 【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences
求最长上升子序列方案数. 转载自:http://blog.csdn.net/u013445530/article/details/47958617,如造成不便,请博主联系我. 数组A包含N个整数(可能 ...
- EXECL中怎么中把换行符换成任意字符
作文本处理的时候,数据是从execl中拷贝出来的,但是execl中是带有格式的. 导出到txt文本中的时候会出现换行,不满足一行一条数据的要求. 公式 =SUBSTITUTE(A1,),"A ...
- java集合之一(框架介绍)
本文转载自:http://www.cnblogs.com/skywang12345/p/3308498.html Java集合主要可以划分为4个部分:List列表.Set集合.Map映射.工具类(It ...
- Problem A: 象棋比赛
Description 1月6日,教职工象棋协会在6号楼办了一次比赛,很多老师都参加了.比赛共进行了5轮,赢1局积3分,和了1分,输了0分,你能帮忙算一下各位老师的积分吗? Input 多组测试数据, ...
- 河南省队选拔 HAOI2015 解题报告
其实省选在四天前就已经结束了,但由于题目难度略大我到今天上午才补完所有题目……(捂脸逃)考场上很幸运,打完了所有我会写的部分分,最后Round1的110分 + Round2的70分,勉强算是没有被 ...
- hdoj 5198 Strange Class 水题
Strange Class Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- 几张图理解Roll, Pitch, Yaw的含义
Roll:翻滚 Pitch:俯仰 Yaw:偏航 有时候不知道它到底绕着哪个轴旋转得到的角,一个比较容易的记法是根据字母的排列顺序PRY分别对应XYZ轴进行旋转得到的角,即: Pitch是绕 ...