SpringBoot整合SpringData和Mysql数据库
1.新建maven项目(具体的新建过程就不细说了)
2.添加maven依赖,也就是在pom.xml文件添加项目的依赖jar包:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot-test</groupId>
<artifactId>bootTest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>bootTest Maven Webapp</name>
<url>http://maven.apache.org</url> <!-- SpringBoot版本(核心) -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath />
</parent> <dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- 持久层(SpringData) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- JDBC依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- 模板(渲染HTML页面) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies> <build>
<finalName>spring-boot-data-jpa</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build> </project>
3.在application.properties(放在resources目录下)中添加配置信息:
##JDBC Setting(Mysql的配置信息)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boottest?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
##服务器的配置信息
server.port=8080
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8 ## SpringDataJPA Settings
#spring.jpa.generate-ddl:true ----->将这个属性设置为TRUE,就是设置数据库自动更新,即但数据库没有实体所对应的的表时,自动创建,有对应的表时执行更新,和hibernate的hbm2ddl.auto:updata功能差不多
spring.jpa.generate-ddl: true
spring.jpa.show-sql: true
spring.jpa.properties.hibernate.format_sql: false
#设置模板位置,即html文件的存放位置
spring.thymeleaf.prefix=classpath:/templates/
4.项目的文件目录

5.测试
5.1 实体类User
package com.yxl.springboot.entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name = "user") //声明实体对应得表,如果没有则创建(前提是application.properties文件中有相应的配置)
public class User {
@Id
@GeneratedValue
private int id;
private String name;
private String password; public User() {
super();
// TODO Auto-generated constructor stub
} public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
5.2 Mapper(Dao)层
package com.yxl.springboot.mapper; import org.springframework.data.jpa.repository.JpaRepository; import com.yxl.springboot.entity.User; //继承JpaRepository类
public interface UserMapper extends JpaRepository<User, Integer>{ public User findByName(String name); }
5.3 controller层
package com.yxl.springboot.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.yxl.springboot.entity.User;
import com.yxl.springboot.mapper.UserMapper; @Controller
public class SpringBootTestController { @Autowired
private UserMapper userMapper; @RequestMapping(value = "/find")
public String springBootTest(Model model) {
User findByName = userMapper.findByName("test");
model.addAttribute("user", findByName);
return "test";
} @RequestMapping(value = "/all")
public String selectAll(Model model) {
List<User> selectAll = userMapper.findAll();
model.addAttribute("user", selectAll);
return "test1";
} }
5.4 test.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
<h1>哈哈哈!成功了!</h1>
<!-- 模板渲染 -->
<p th:text="${user.name}"></p>
</body>
</html>
5.5 test1.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
<h1>哈哈哈!成功了!</h1>
<table>
<thead>
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
</tr>
</thead>
<tbody>
<!-- 循环遍历 -->
<tr th:each = "user : ${user}">
<td th:text = "${user.id}"></td>
<td th:text = "${user.name}"></td>
<td th:text = "${user.password}"></td>
</tr>
</tbody>
</table>
</body>
</html>
5.5 程序入口Application.java
package com.yxl.springboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
5.6 数据库数据

5.7 运行结果
test.html

test1.html

参考博客:http://blog.csdn.net/je_ge/article/details/53294949
http://blog.csdn.net/quuqu/article/details/52511933
SpringBoot整合SpringData和Mysql数据库的更多相关文章
- SpringBoot 整合 hibernate 连接 Mysql 数据库
前一篇搭建了一个简易的 SpringBoot Web 项目,最重要的一步连接数据库执行增删改查命令! 经过了一天的摸爬滚打,终于成功返回数据! 因为原来项目使用的 SpringMVC + Hibern ...
- springboot整合mybatis连接mysql数据库出现SQLException异常
在springboot整合mybatis连接数据库的时候,项目中遇到一个SQLException,我检查了properties配置文件,看数据源有没有配错,检查有没有打错字,在数据库中把sql语句查询 ...
- kotlin + springboot整合mybatis操作mysql数据库及单元测试
项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...
- freeswitch用户整合(使用mysql数据库的用户表)
转:freeswitch用户整合(使用mysql数据库的用户表) freeswitch是一款强大的voip服务器,可以语音和视频.但是它默认是采用/directory文件夹下的xml来配置用户的,对于 ...
- 如何用IDEA创建springboot(maven)并且整合mybatis连接mysql数据库和遇到的问题
一.New->Project 二.点击next 三.在Group栏输入组织名,Artifact就是项目名.选择需要的java版本,点击next 四.添加需要的依赖 在这里我们也可以添加sql方面 ...
- django整合原有的mysql数据库
虽然django适合从零开始构建一个项目,但有时候整合原有的数据库也在所难免,下面以django整合我的mysql作说明. mysql数据是我从京东上抓取的数据,数据表名为jd,演示如图 下面将jd整 ...
- springboot学习-springboot使用spring-data-jpa操作MySQL数据库
我们在上一篇搭建了一个简单的springboot应用,这一篇将介绍使用spring-data-jpa操作数据库. 新建一个MySQL数据库,这里数据库名为springboot,建立user_info数 ...
- SpringBoot配置JDBC连接MySql数据库的时候遇到了报错:HikariPool-1 - Exception during pool initialization
使用SpringBoot做JAVA开发时,JDBC连接MySql数据库的时候遇到了报错: ERROR 10392 --- [ main] com.zaxxer.hikari.pool.HikariPo ...
- Springboot+mybatis中整合过程访问Mysql数据库时报错
报错原因如下:com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone.. 产生这个 ...
随机推荐
- lapis 集成openresty最新版本cjson 问题的解决
备注: 为了解决安装了lapis.同时又希望使用新版nginx 以及openresty 的特性(stream ...) 1. 解决方法 参考: https://github.com/leaf ...
- CAS原理分析
在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁(后面的章节还会谈到锁). 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度 ...
- c++中重载,重写,覆盖
1.重载 重载从overload翻译过来,是指同一可访问区内被声明的几个具有不同参数列表(参数的类型,个数,顺序不同)的同名函数,根据参数列表确定调用哪个函数,重载不关心函数返回类型. 相同的范围(在 ...
- Oracle 之 配置HugePages内存
HugePages是通过使用大页内存来取代传统的4kb内存页面,使得管理虚拟地址数变少,加快了从虚拟地址到物理地址的映射以及通过摒弃内存页面的换入换出以提高内存的整体性能.尤其是对于8GB以上的内存以 ...
- 执行npm install报错:npm ERR! code EINTEGRITY
命令行执行npm install报错如下: D:\frontend\viewsdev>npm install npm ERR! code EINTEGRITY npm ERR! sha512-8 ...
- Ubuntu 14.04 配置安卓5.1编译环境
Ubuntu 14.04版本 电脑cpu必须是64位 硬盘分配大约100G的空间 1.ubuntu中更新源 $ sudo apt-get update 2.android5.1需要安装openjdk- ...
- 关于RedHat Enterprise Linux 6.4使用Centos 6 的yum源
思路:卸载redhat自带yum,然后下载centos的yum,安装后修改配置文件 1.首先到http://mirrors.163.com/centos下载软件包 x86 地址:http://mirr ...
- MxNet C++和python环境配置
MxNet C++和python环境配置 安装文件: 1.为了与python已经安装好的版本一致,在这个网站下载mxnet 1.0.0的源码 https://github.com/apache/inc ...
- Unit08: 过滤器、监听器
Unit08: 过滤器.监听器 下面这些小案例主要演示的filter的执行时机: web package web; import java.io.IOException; import javax.s ...
- C程序花括号嵌套层次统计(新)
[问题描述] 编写程序,统计给定的C源程序中花括号的最大嵌套层次,并输出花括号嵌套序列,该程序没有语法错误. 注意:1)源程序注释(/* … */)中的花括号应被忽略,不参与统计.2)源程序中的字符串 ...