springboot学习笔记(三)使用JDBC以及整合spring data jpa

spring boot JDBC的使用:

1.引入依赖

            
            mysql
            mysql-connector-java
         
         
             
            org.springframework.boot
            spring-boot-starter-jdbc
          

2.数据库连接配置

 

编辑 application.properties

加入

spring.datasource.url=jdbc:mysql://192.9.8.144:3306/eym_lib?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.name=eym_lib
spring.datasource.username=xxxxx
spring.datasource.password=xxxx
spring.datasource.max-wait=10000
spring.datasource.max-active=50
spring.datasource.min-idle=2

 

 

 

ok  现在可以使用jdbc了:

 

	   @RequestMapping("/jdbc")
	   public void testJDBC(){
		   
		   String sql = "SELECT t.approvalBy,t.approvalDate,t.id,t.keywords FROM ym_manuscript t ";
		   
		   List> list= jdbcTemplate.queryForList(sql);
		   System.out.println(list.size());
	   }

输出60  与测试库一致。

 

 

spring boot 整合spring data jap

 

1.引入依赖:

完整pom文件如下:

 


  4.0.0

  com.springboot
  Test_myspringboot
  0.0.1-SNAPSHOT
  jar

  Test_myspringboot
  http://maven.apache.org
  
   
        org.springframework.boot
        spring-boot-starter-parent
        1.4.3.RELEASE
    

  
    UTF-8
  

  
  
  
  
      
            org.springframework.boot
            spring-boot-starter-web
        
       
            
            mysql
            mysql-connector-java
         
         
             
            org.springframework.boot
            spring-boot-starter-jdbc
          
          
              
        org.springframework.data
        spring-data-jpa
       
      
          
            org.hibernate
            hibernate-entitymanager
        

        
        
            org.aspectj
            aspectjweaver
        


    
        
            com.mchange
            c3p0
            0.9.5.2
            
                
                    commons-logging
                    commons-logging
                
            
        
  

  
  


配置spring data jpa 连接池:

 

application.properties

 

完整资源文件如下:

 

 

logging.file=log.log  
logging.level.com.xiaofangtech.sunt.controller = debug  
logging.level.com.xiaofangtech.sunt.helper = warn  

spring.datasource.url=jdbc:mysql://192.9.8.144:3306/eym_lib?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.name=eym_lib
spring.datasource.username=root
spring.datasource.password=
spring.datasource.max-wait=10000
spring.datasource.max-active=50
spring.datasource.min-idle=2


spring.data.jpa.repositories.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

 

编写与数据库对应的实体:

代码如下:

 

 

package com.springboot.Test_myspringboot;

import java.util.Date;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * 投诉实体
 * 
 * @author xuchuang
 * @version 1.0
 * @created 2017-10-10 16:28:32
 */
@Entity
@Table(name = "ym_complaint")
public class Complaint extends BaseEntity {

	private static final long serialVersionUID = -5244547508593077581L;

	@Column(name = "title", nullable = false)
	private String title;

	@Column(name = "qq")
	private String qq;

	@Column(name = "mobile")
	private String mobile;

	@Column(name = "email")
	private String email;

	@Column(name = "link")
	private String link;

	@Column(name = "details", nullable = false)
	private String details;

	@Column(name = "fileId")
	private String fileId;

	@Column(name = "createdDate")
	private Date createdDate;

	@Column(name = "handledBy")
	private String handledBy;

	@Column(name = "handledDate")
	private Date handledDate;

	@Column(name = "handledDetails")
	private String handledDetails;

	@Column(name = "status")
	private String status;

	public Complaint() {

	}

	public String getHandledDetails() {
		return handledDetails;
	}

	public void setHandledDetails(String handledDetails) {
		this.handledDetails = handledDetails;
	}

	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public String getTitle() {
		return title;
	}

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



	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public String getHandledBy() {
		return handledBy;
	}

	public void setHandledBy(String handledBy) {
		this.handledBy = handledBy;
	}

	public Date getHandledDate() {
		return handledDate;
	}

	public void setHandledDate(Date handledDate) {
		this.handledDate = handledDate;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}


编写对应试题的核心仓储类:如下:

 

 

/**
 * Test_myspringboot
 * JpaTest.java
 */
package com.springboot.Test_myspringboot;

import java.io.Serializable;

import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @author xuchuang
 * @updateTime 2017年12月20日 下午5:25:41
 */
public interface JpaTest extends JpaRepository{

}

 

 

 

 

 

ok,基本配置完毕,来测是一发:

 

 

	   @RequestMapping("/jpa")
	   public void testJpa(){
		   List  list=  jpatest.findAll();
		   System.out.println(list.size());
	
	   }

 

 

 

访问 localhost:6366/jpa

 

 


springboot学习笔记(三)使用JDBC以及整合spring data jpa_第1张图片


成功拿到数据! 及此 jdbc与springdatajpa的helloword 测试完毕!

你可能感兴趣的:(springboot)