Spring-Data-Jpa通过注解在数据库中创建表和字段

Spring-Data-Jpa实现通过实体类注解模式自动在数据库中创建表和字段

配置文件

package cn.cityworks.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.postgresql.jdbc2.optional.SimpleDataSource;
import org.postgresql.jdbc3.Jdbc3PoolingDataSource;
import org.postgresql.jdbc3.Jdbc3SimpleDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


/**
 * jpa的配置类,包括数据源,事务管理等
 * Created by gwcheng on 2017/4/21.
 */
// 标明该类使用Spring基于Java的配置
@Configuration
// 读取配置文件的,通过Environment读取
@PropertySource("classpath:application.yml")
// scan the package of the annotated configuration class for Spring Data repositories
@EnableJpaRepositories(basePackages = "cn.cityworks.repository")
// Enables Spring's annotation-driven transaction management
@EnableTransactionManagement
public class JpaConfig {

    @Autowired
    private Environment env;

    /**
     * 1.配置数据源
     *
     * @return DataSource
     */
    @Bean
    public DataSource dataSource() {
        //DruidDataSource source = new DruidDataSource();
    	Jdbc3PoolingDataSource source = new Jdbc3PoolingDataSource();
        source.setUrl(env.getRequiredProperty("datasource.url"));
        source.setUser(env.getRequiredProperty("datasource.username"));
        source.setPassword(env.getRequiredProperty("datasource.password"));
        //source.setDataSourceName("oauth");
       // source.setMaxConnections(10);
        return  source;
    }

    /**
     * 2.配置EntityManagerFactory
     *
     * @return
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        // 配置数据源
        factory.setDataSource(dataSource());
        // VendorAdapter
        HibernateJpaVendorAdapter jpaVendorAdapter=new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        factory.setJpaVendorAdapter(jpaVendorAdapter);
        // entity包扫描路径
        factory.setPackagesToScan("cn.cityworks.security.model");
        // jpa属性
        factory.setJpaProperties(hibernateProperties());
        factory.afterPropertiesSet();
        return factory;
    }


    /**
     * 3.事务管理器配置
     *
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager manager = new JpaTransactionManager();
        manager.setEntityManagerFactory(entityManagerFactory().getObject());
        return manager;
    }

    /**
     * 把HibernateExceptions转换成DataAccessExceptions
     */
    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }

    /**
     * hibernate配置
     * @return
     */
    private Properties hibernateProperties() {
        Properties properties = new Properties();
        // 显示sql语句
        properties.put("hibernate.show_sql", env.getRequiredProperty("jpa.show-sql"));
        // 格式化sql语句
        //properties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
        // 方言
       properties.put("hibernate.dialect", env.getRequiredProperty("jpa.hibernate.dialect"));
        // 自动生成表
        properties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("jpa.hibernate.ddl-auto"));
        // 名字策略
       // properties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));
        return properties;
    }

}

实体类代码如下

package cn.cityworks.security.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;

/*
 * 字典表
 * @author:zsx
 * */
@Entity
public class Dict implements Serializable {
    @Id
    @Column(name = "id", length = 64)
    private String id;//主键
    @Column(name = "pid", length = 32)
    private String pid;//	父id
    @Column(name = "datatype", length = 32)
    private String datatype;//数据类别
    @Column(name = "datacode", length = 32)
    private String datacode;//	数据编码
    @Column(name = "datavalue", length = 32)
    private String datavalue;//	数据值
    @Column(name = "datadesc", length = 32)
    private String datadesc;//	描述
    @Column(name = "sortno", length = 32)
    private int sortno;//排序
    @Column(name = "state", length = 32)
    private int state;//状态
    
    public Dict() {
    }

    public Dict(String id, String pid, String datatype, String datacode, String datavalue, String datadesc, int sortno, int state) {
        this.id = id;
        this.pid = pid;
        this.datatype = datatype;
        this.datacode = datacode;
        this.datavalue = datavalue;
        this.datadesc = datadesc;
        this.sortno = sortno;
        this.state = state;
    }


    public String getId() {
        return id;
    }

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

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getDatatype() {
        return datatype;
    }

    public void setDatatype(String datatype) {
        this.datatype = datatype;
    }

    public String getDatacode() {
        return datacode;
    }

    public void setDatacode(String datacode) {
        this.datacode = datacode;
    }

    public String getDatavalue() {
        return datavalue;
    }

    public void setDatavalue(String datavalue) {
        this.datavalue = datavalue;
    }

    public String getDatadesc() {
        return datadesc;
    }

    public void setDatadesc(String datadesc) {
        this.datadesc = datadesc;
    }

    public int getSortno() {
        return sortno;
    }

    public void setSortno(int sortno) {
        this.sortno = sortno;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}

猜你喜欢

转载自blog.csdn.net/IT_Java_Roy/article/details/88694117