008.Spring Boot Jpa Mysql Sample

一、目录结构

008.Spring Boot Jpa Mysql Sample_第1张图片
目录结构

二、pom.xml



    4.0.0

    com.airkisser
    spring-boot-sample-jpa-mysql
    0.0.1-SNAPSHOT
    war

    spring-boot-sample-jpa-mysql
    Spring boot data jpa mysql sample

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


三、application.properties

#----------------------------------------------------------------------
# Datasource Config
#----------------------------------------------------------------------
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?createDatabaseIfNotExist=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
#----------------------------------------------------------------------
# Jpa Config
#----------------------------------------------------------------------
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

四、java

Application.java

package com.airkisser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

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

User.java

package com.airkisser.entity;

import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Date;

@Entity
@Table(name = "tr_user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "username", length = 50, unique = true, nullable = false)
    private String username;
    @Column(name = "password", length = 50, nullable = false)
    private String password;
    @Column(name = "birth")
    private Date birth;
    @Column(name = "description")
    @Type(type = "text")
    private String description;

    public Long getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

UserRepository.java

package com.airkisser.repository;

import com.airkisser.entity.User;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository {

}

五、测试UserRepository

package com.airkisser.repository;

import com.airkisser.entity.User;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import java.sql.Date;
import java.text.ParseException;

@SpringBootTest
public class UserRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testSave() throws ParseException {
        User user = new User();
        user.setUsername("Admin");
        user.setPassword("admin");
        user.setBirth(Date.valueOf("1990-10-10"));
        user.setDescription("超级用户");
        long num = userRepository.count();
        userRepository.save(user);
        Assert.assertEquals(num + 1, userRepository.count());
        User user2 = userRepository.findOne(user.getId());
        Assert.assertNotNull(user2);
        Assert.assertEquals("Admin", user2.getUsername());
    }
}

你可能感兴趣的:(008.Spring Boot Jpa Mysql Sample)