Hiberante+Mysql+Maven2 XML配置原型

阅读更多
使用Maven3+Hibernate 3.1+Mysql实现一个简单的模型

使用到的工具:
   Eclipse 4.3
   Maven 3.1
   Hibernate 3.1rc2
   MySQL 5.0


具体搭建实现步骤:
  1.创建一个数据库为David。
  2.在David数据库中创建一个表
DROP TABLE IF EXISTS `stock`;
CREATE TABLE `stock` (
  `STOCK_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `STOCK_CODE` VARCHAR(10) NOT NULL,
  `STOCK_NAME` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`STOCK_ID`) USING BTREE,
  UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`),
  UNIQUE KEY `UNI_STOCK_ID` (`STOCK_CODE`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  3.在Eclipse中创建一个Maven Project
  4.编写POM.xml

  4.0.0

  com.david.common
  SpringExample
  0.0.1-SNAPSHOT
  jar

  SpringExample
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    

    hibernate
    hibernate
    3.1rc2

	
		mysql
		mysql-connector-java
		5.1.9
	
	
	
	
		dom4j
		dom4j
		1.6.1
	
 
	
		commons-logging
		commons-logging
		1.1.1
	
 
	
		commons-collections
		commons-collections
		3.2.1
	
 
	
		cglib
		cglib
		2.2
	
	
 
	
		javax.transaction
		jta
		1.1
	
	
    com.jolira
    asm
    3.3.0

  


5. 配置Stock.hbm.xml 这个文件要放在src/main/resources/com/david/common/Stock.hbm.xml
 

 

    
        
            
            
        
        
            
        
        
            
        
    


6.编写Stock实体类
public class Stock implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Integer stockId;
	private String stockCode;
	private String stockName;
	
	public Stock () {
		
	}
	
	public Stock(String stockCode, String  stockName){
		this.stockCode = stockCode;
		this.stockName = stockName;
	}

	public Integer getStockId() {
		return stockId;
	}

	public void setStockId(Integer stockId) {
		this.stockId = stockId;
	}

	public String getStockCode() {
		return stockCode;
	}

	public void setStockCode(String stockCode) {
		this.stockCode = stockCode;
	}

	public String getStockName() {
		return stockName;
	}

	public void setStockName(String stockName) {
		this.stockName = stockName;
	}

	public String toString(){
		return "[ stockId:"+stockId
				+" stockName:"+stockName+" stockCode:"+stockCode+"]";
	}
}


7.编写一个简单的Hibernate工具类。用来获取Session等操作
public class HibernateUtil {
	
	private static final SessionFactory sessionFactory = buildSessionFactory();
	
    public static SessionFactory buildSessionFactory(){
    	try {
			 return new Configuration().configure().buildSessionFactory();
		} catch (Exception ex) {
			
			System.err.println("Initial SessionFactory creation fails"+ex);
			throw new ExceptionInInitializerError(ex);
	   }
    }
    
    public static SessionFactory getSessionFactory(){
    	return sessionFactory;
    }
    
    public static void shutdown(){
    	getSessionFactory().close();
    }
}

8. 最后编写Hibernate.cfg.xml放在/src/main/resources/hibernate.cfg.xml



    
        false
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/david
        root
        123456
        org.hibernate.dialect.MySQLDialect
        true
        
        
        
    


9.写个测试类来验证下配置是否有问题
public class App 
{
    public static void main( String[] args )
    {
       System.out.println("Maven +hibernate + Mysql");
       
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Stock stock = new Stock();
        
        stock.setStockCode("47135");
        stock.setStockName("GENM");
        session.save(stock);
        session.getTransaction().commit();
        
    }
}


10.运行发现报错了。报错信息如下:
Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(I)V
	at net.sf.cglib.core.DebuggingClassWriter.(DebuggingClassWriter.java:47)
	at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
	at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
	at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:362)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1145)
	at com.david.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
	at com.david.utils.HibernateUtil.(HibernateUtil.java:12)
	at com.david.common.App.main(App.java:17)


11.解决这个问题:原来是包的冲突(asm没有引入3.3版本的话,由于maven的依赖传递会自动引入一个1.5.3版本的asmjar包,这个包会跟cglib 2.2版本的包发生冲突。因此要显示的引入一个高版本的asm包)

在pom.xml中显示加入
	
    com.jolira
    asm
    3.3.0


12.再次运行就没问题了。

你可能感兴趣的:(Hiberante+Mysql+Maven2 XML配置原型)