Java 在不使用persistence.XML的情况下使用JPA和Spring连接到mysql数据库
我正在尝试学习如何使用spring和JPA连接到数据库。我不想使用persistence.xml,而是希望使用尽可能少的代码创建实体管理器 所以,我尝试了这个页面上的解决方案() 它成功了。但是,它看起来比我希望的要复杂,并且它没有包含任何spring元素或任何注释来减少代码量 我的代码包含了这个问题的解决方案() 我从Tobogganski那里获取了配置代码,他为Postgresql编写了配置代码,我正在努力使其在Mysql中工作 Config.java文件Java 在不使用persistence.XML的情况下使用JPA和Spring连接到mysql数据库,java,mysql,spring,maven,jpa,Java,Mysql,Spring,Maven,Jpa,我正在尝试学习如何使用spring和JPA连接到数据库。我不想使用persistence.xml,而是希望使用尽可能少的代码创建实体管理器 所以,我尝试了这个页面上的解决方案() 它成功了。但是,它看起来比我希望的要复杂,并且它没有包含任何spring元素或任何注释来减少代码量 我的代码包含了这个问题的解决方案() 我从Tobogganski那里获取了配置代码,他为Postgresql编写了配置代码,我正在努力使其在Mysql中工作 Config.java文件 package jj; impo
package jj;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
@Configuration
@EnableAutoConfiguration
@EntityScan(basePackages= "entity")
public class Config {
private final String DB_URL = "jdbc:mysql://localhost:3306/dictionary";
private final String DB_USER_NAME = "root";
private final String DB_PASSWORD = "";
@Bean
public Properties hibernateProperties(){
final Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.id.new_generator_mappings", false);
properties.put("hibernate.connection.datasource", getMysqlDataSource());
return properties;
}
@Bean
protected DataSource getMysqlDataSource() {
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setURL(DB_URL);
mysqlDataSource.setUser(DB_USER_NAME);
mysqlDataSource.setPassword(DB_PASSWORD);
return mysqlDataSource;
}
@Bean(name = "emf")
public EntityManagerFactory entityManagerFactory( DataSource dataSource, Properties hibernateProperties ){
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource( dataSource );
em.setJpaVendorAdapter( new HibernateJpaVendorAdapter() );
em.setJpaProperties( hibernateProperties );
em.setPersistenceUnitName( "dictionary" );
em.setPersistenceProviderClass(HibernatePersistenceProvider.class);
em.afterPropertiesSet();
return em.getObject();
}
}
DictElement类
package entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceUnit;
import org.springframework.stereotype.Component;
@Component
@Entity
@PersistenceUnit(unitName="dictionary")
public class DictElement {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column
private String word;
@Column
private String genre;
@Column
private String definition;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
}
司机班
package ui;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import entity.DictElement;
import jj.Config;
public class driver {
public static void main(String[] args) {
ApplicationContext ctx =new AnnotationConfigApplicationContext(Config.class);
EntityManagerFactory emf= (EntityManagerFactory) ctx.getBean("emf", EntityManagerFactory.class);
EntityManager em = null;
try {
em= emf.createEntityManager();
DictElement element= new DictElement();
element.setWord("word");
element.setGenre("type");
element.setDefinition("definition");
em.getTransaction().begin();
em.persist(element);
em.getTransaction().commit();
} catch (Exception ex)
{
ex.printStackTrace();
if (em != null)
{
em.getTransaction().rollback();
}
}
}
}
最后,数据库名为dictionary,它包含一个名为
“dict_表”,该表由三列组成,第一列是“单词”,第二列是“体裁”,第三列是“定义”
当我运行这段代码时,它会给我一个错误“找不到名为“dictionary”的持久化单元”,但我确信我的代码还有更多的问题
问题是,我如何替换persistence.xml中的这一行,我们这里不使用它
(持久化单元名称=“字典”事务类型=“资源\本地”)
只有java代码和注释
正如你从驾驶课上看到的。我正在尝试向本地数据库表插入一个元素。在驱动程序类中,请参见:
import java.io.ObjectInputFilter.Config代码>谢谢,这解决了没有名为“emf”的bean的问题,但现在代码没有名为“dictionary”的持久化单元的问题。代码仍然不起作用。