博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.13. Spring boot with MySQL
阅读量:5838 次
发布时间:2019-06-18

本文共 8377 字,大约阅读时间需要 27 分钟。

2.13.1. Maven

pom.xml

4.0.0
netkiller.cn
api.netkiller.cn
0.0.1-SNAPSHOT
jar
api.netkiller.cn
http://maven.apache.org
UTF-8
org.springframework.boot
spring-boot-starter-parent
1.3.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-data-mongodb
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-test
test
org.springframework.data
spring-data-mongodb
mysql
mysql-connector-java
com.google.code.gson
gson
compile
junit
junit
test
src
org.springframework.boot
spring-boot-maven-plugin
maven-compiler-plugin
3.3
maven-war-plugin
2.6
WebContent
false

2.13.2. Resource

src/main/resources/application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://192.168.6.1:3306/testspring.datasource.username=rootspring.datasource.password=passwordspring.jpa.database=MYSQLspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=update#spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/test				spring.datasource.username=root				spring.datasource.password=pasword				spring.datasource.driver-class-name=com.mysql.jdbc.Driver				spring.datasource.max-idle=10				spring.datasource.max-wait=10000				spring.datasource.min-idle=5				spring.datasource.initial-size=5				spring.datasource.validation-query=SELECT 1				spring.datasource.test-on-borrow=false				spring.datasource.test-while-idle=true				spring.datasource.time-between-eviction-runs-millis=18800				spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)

2.13.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" })@EnableMongoRepositoriespublic class Application {	public static void main(String[] args) {		SpringApplication.run(Application.class, args);	}}

2.13.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;@Controllerpublic 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));		});	}}

2.13.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;@Repositorypublic interface ArticleRepository extends CrudRepository
{ Page
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;@Controllerpublic 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";	}}

原文出处:Netkiller 系列 手札

本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
php页面编码设置详解
查看>>
if __name__=="__main__"
查看>>
第十四 jQuery及事件、ajax
查看>>
100以内素数的判断与输出-----程序
查看>>
可以用Navicat Cloud做什么?
查看>>
JavaScript之引用类型(ECMAScript5)
查看>>
CentOS关闭防火墙
查看>>
request内置对象
查看>>
利用Syslog Watcher在windows下部署syslog日志服务器
查看>>
emacs编辑器的使用
查看>>
黎曼流形
查看>>
背包九讲(转载)
查看>>
python中read()、readline()、readlnes()区别
查看>>
c链表
查看>>
wget 下载整个网站,或者特定目录
查看>>
吴恩达机器学习笔记2-代价函数I(cost function)
查看>>
securecrt安装
查看>>
leetcode动态规划
查看>>
软件工程第一次团队作业
查看>>
读书笔记之: 操作系统概念(第6版)-第七部分 案例研究-Windows XP
查看>>