刚接触SSH,想模仿着写个项目,没有hbm.xml
实体类:
package cn.com.car.base.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="T_CAR")
public class Car implements Serializable{
private static final long serialVersionUID = 1L;
@GenericGenerator(name = "generator", strategy = "guid")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false, length = 38)
private String id;
@Column(name="models")
private String models;//具体车型
@Column(name="type")
private String type;//所属类别
@Column(name="price")
private int price;//价格
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getModels() {
return models;
}
public void setModels(String models) {
this.models = models;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Car [id=" + id + ", models=" + models + ", type=" + type
+ ", price=" + price + "]";
}
}
application.xml中有关配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:hibernate.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${hibernate.connection.driverClassName}"/>
<property name="url" value="${hibernate.connection.url}"/>
<property name="username" value="${hibernate.connection.username}"/>
<property name="password" value="${hibernate.connection.password}"/>
</bean>
<!--Hibernate SessionFatory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>cn.com.car.base.entity.Car</value>
</list>
</property>
<property name="hibernateProperties">
<value>classpath:hibernate.properties</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
hibernate.properties配置
hibernate.show_sql=true
hibernate.format_sql=false
#hibernate.hbm2ddl.auto=create
#hibernate.hbm2ddl.auto=update
hibernate.cache=org.hibernate.cache.EhCacheProvider
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.driverClassName=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/car
hibernate.connection.username=car
hibernate.connection.password=123456
hibernate.c3p0.minPoolSize=5
hibernate.c3p0.maxPoolSize=50
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statement=200
然后就是DAO层实现
package cn.com.car.base.dao.impl;
import java.util.HashMap;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.com.car.base.dao.CarDao;
import cn.com.car.base.entity.Car;
public class CarDaoImpl extends HibernateDaoSupport implements CarDao {
@Override
public List<Car> getAllCar() {
String sql="from T_CAR where 1=1";
List<Car> list=this.getHibernateTemplate().find(sql);
return list;
}
@Override
public List<Car> getCarByParam(HashMap<String, String> param) {
StringBuilder sql=new StringBuilder();
sql.append("select * from T_CAR where 1=1");
if(param!=null){
if(param.get("type")!=null){
String type=param.get("type");
sql.append("and type="+type);
}
if(param.get("price")!=null){
String price=param.get("price");
sql.append("and price="+price);
}
Query query = this.getSession().createSQLQuery(sql.toString());
List<Car> list=query.list();
return list;
}
List<Car> list=this.getHibernateTemplate().find(sql.toString());
return list;
}
@Override
public Car getCarByModels(String models) {
String hql = "from T_CAR t where 1=1 "
+" and t.models=? ";
Query query = this.getSession().createQuery(hql).setString(0, models);
return (Car) query.list().get(0);
}
@Override
public Car getCarById(String id) {
String hql = "from T_CAR t where 1=1 "
+" and t.id=? ";
Query query = this.getSession().createQuery(hql).setString(0, id);
return (Car) query.list().get(0);
}
}
现在就是一旦执行DAO层方法就报
org.hibernate.hql.ast.QuerySyntaxException: T_CAR is not mapped [from T_CAR where 1=1]
这样类似的异常,都是T_CAR is not mapped ,T_CAR是我数据库表名,还要在哪里配置吗???
使用的是mysql数据库。
------解决思路----------------------
使用hql的时候不能用表名要用类名你把T_CAR改为Car就可以了