文档章节

Spring Boot with MySQL

netkiller-
 netkiller-
发布于 2016/08/08 09:32
字数 718
阅读 155
收藏 7

Spring boot with MySQL 实例参考

原创 2016-08-08 景峯 Netkiller

节选择《Netkiller Java 手札》· Spring 框架

 

9.7. Spring boot with MySQL

9.7.1. Maven

pom.xml

<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>netkiller.cn</groupId>
	<artifactId>api.netkiller.cn</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>api.netkiller.cn</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
<!-- 
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		-->		 
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</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-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
		</dependency>
	 	<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source />
					<target />
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

9.7.2. Resource

src/main/resources/application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.6.1:3306/test
spring.datasource.username=root
spring.datasource.password=password

spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.ddl-auto=create-drop

9.7.3. Application

package api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import api.ApplicationConfiguration;

@SpringBootApplication
@EnableConfigurationProperties(ApplicationConfiguration.class)
@EnableAutoConfiguration
@ComponentScan({ "api.web", "api.rest","api.service" })
@EnableMongoRepositories
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

9.7.4. JdbcTemplate

package api.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import api.domain.City;
import api.domain.Article;
import api.ApplicationConfiguration;
import api.repository.CityRepository;
import api.repository.ArticleRepository;
import api.service.TestService;

@Controller
public class IndexController {

	@Autowired
	private CityRepository repository;

	@Autowired
	private TestService testService;

	@Autowired
	private ApplicationConfiguration propertie;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@RequestMapping(value = "/article")
	public @ResponseBody String dailyStats(@RequestParam Integer id) {
		String query = "SELECT id, title, content from article where id = " + id;

		return jdbcTemplate.queryForObject(query, (resultSet, i) -> {
			System.out.println(resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));
			return (resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));
		});
	}
}

9.7.5. CrudRepository

ArticleRepository

package api.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import api.domain.Article;

@Repository
public interface ArticleRepository extends CrudRepository<Article, Long> {

    Page<Article> findAll(Pageable pageable);

}

Article.java

package api.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;

@Entity
@Table(name = "article")
public class Article implements Serializable {
	private static final long serialVersionUID = 7998903421265538801L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id", unique=true, nullable=false, insertable=true, updatable = false)
	private Long id;
	private String title;
	private String content;

	public Article(){
		
	}
	public Article(String title, String content) {
		this.title = title;
		this.content = content;
	}

	public Long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public String toString() {
		return "Article [id=" + id + ", title=" + title + ", content=" + content + "]";
	}

}
package api.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import api.domain.City;
import api.domain.Article;
import api.ApplicationConfiguration;
import api.repository.CityRepository;
import api.repository.ArticleRepository;
import api.service.TestService;

@Controller
public class IndexController {

	@Autowired
	private CityRepository repository;

	@Autowired
	private TestService testService;

	@Autowired
	private ApplicationConfiguration propertie;

	@Autowired
	private ArticleRepository articleRepository;

	@RequestMapping("/save")
	@ResponseBody
	public String save() {
		articleRepository.save(new Article("Neo", "Chen"));
		return "OK";
	}
	
	@RequestMapping("/mysql")
	@ResponseBody
	public String mysql() {
		
		for (Article article : articleRepository.findAll()) {
			System.out.println(article);
		}
		return "OK";
	}
}

 

延伸阅读

Spring Boot 快速开始

Spring Boot MongoDB 实例

Spring Boot · Mongo Repository

Elasticsearch 瞬间入门

Elasticsearch 急速入门·文档与搜索

 

长按下面二维码,关注我的公众号,每天推推送原创技术文章。

© 著作权归作者所有

共有 人打赏支持
netkiller-

netkiller-

粉丝 694
博文 264
码字总数 368425
作品 10
深圳
部门经理
私信 提问
Spring boot的Gradle配置

问题 最近尝试把自己现有的Maven工程转到Gradle工程上面来。 build.gradle 这是转化后的Gradle依赖管理。 pom.xml 这是原来的Maven依赖配置。

亚林瓜子
2018/11/02
0
0
Spring boot的简单rest服务(非xml方式配置)

背景 简单使用spring boot配置一个rest服务,数据库主要使用mysql没有使用spring演示时候使用的h2(一种内存数据库)和lombok(通过注解的方式生成getter和setter方法,因为要在IDEA上面安装...

亚林瓜子
2018/06/14
0
0
Spring boot在Docker中以JPA方式连接Mysql

背景 最近在了解Docker的使用,发现docker在集群部署方面和运维方面有比较大的优势,通过统一的依赖关系,以镜像的方式,将已经打好包的镜像文件,部署到各个节点。如果不用考虑集群的同学,...

亚林瓜子
2018/11/09
0
0
Spring Boot 整合MyBatis

Spring Boot官方提供了Spring Data的支持,但是却没有提供MyBatis的支持。 因为,Spring Data是Spring旗下的子产品,而MyBatis却不是。 本文通过一个小例子来实现Spring Boot 与MyBatis的整合...

_流云_
2017/04/17
0
0
Spring Boot 和 testNG 和 eclipse背景色

通过之前的博文,我们已经验证,Spring环境下完成access数据访问没有问题。下面我们直接在Spring环境下部署我们的升级项目。 1.导入Spring Boot Spring Boot是Spring的子项目,用来解决项目配...

pandamonica
2017/09/19
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Mac 10.14 编译Android 8.1源码及刷入nexus 6p

环境准备 官网 描述得已经相当清楚了 ,这里稍微总结一下: 创建区分大小写的磁盘映像 mac系统默认是不区分大小写的,所以我们需要创建一个区分大小写的文件系统 hdiutil create -type SPARS...

猴亮屏
12分钟前
0
0
js获取url参数

function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var url =decodeURI(decodeURI(window.location.search)); var r = url.substr(1).match(reg);//s......

ArlenXu
19分钟前
1
0
(转)Spring MVC的常用注解

@Controller(常用) 注解一个类表示控制器,Spring MVC会自动扫描标注了这个注解的类。 @RequestMapping(常用) 请求路径映射,可以标注类,也可以是方法,可以指定请求类型,默认不指定为...

政旭Arvin
25分钟前
0
0
Navicat使用教程:在Navicat Monitor for MySQL/MariaDB中配置实例

下载Navicat Monitor最新版本 Navicat Monitor 是一套安全、简单而且无代理的远程服务器监控工具。它具有强大的功能使你的监控发挥最大效用。受监控的服务器包括 MySQL、MariaDB 和 Percona ...

电池盒
28分钟前
0
0
sql根据日期查询,本周,本月,本年,今日相关统计

sql根据日期查询,本周,本月,本年,今日相关统计 昨天 select * from tb where datediff(day, 时间字段 ,getdate()) = 1 今天 select * from tb where datediff(day, 时间字段 ,getdate()) = ...

BraveLN
38分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部