这是测试方法:@Test
public void testProduct() { ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ProductDao productDao = (ProductDao) context.getBean("productDao");
Product pro = new Product();
pro.setName("pp");
System.out.println(productDao.saveOrUpdateProduct(pro));
}
这是实体
private Integer id;
private String num;
private String name;
private Integer price;
private Integer transFee;
private int use;
private int recod;
private String standard;
private String desc;
private String img;
private String type;
private String simpleDesc;
private int display;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public Integer getTransFee() {
return transFee;
}
public void setTransFee(Integer transFee) {
this.transFee = transFee;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSimpleDesc() {
return simpleDesc;
}
public void setSimpleDesc(String simpleDesc) {
this.simpleDesc = simpleDesc;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public int getUse() {
return use;
}
public void setUse(int use) {
this.use = use;
}
public int getRecod() {
return recod;
}
public void setRecod(int recod) {
this.recod = recod;
}
public int getDisplay() {
return display;
}
public void setDisplay(int display) {
this.display = display;
}
这是dao方法,此方法继承hibernatetemplete,sessionfactory由spring注入
public boolean saveOrUpdateProduct(Product product) {
try {
super.saveOrUpdate(product);
return true;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
这是映射文件:
<hibernate-mapping>
<class name="com.product.model.Product" table="t_product">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="num" />
<property name="price" />
<property name="transFee" column="trans_fee" />
<property name="use" column="is_use"/>
<property name="name"/>
<property name="recod" column="is_recod"/>
<property name="desc" />
<property name="img" />
<property name="type" />
<property name="standard"/>
<property name="simpleDesc" column="simple_desc" />
<property name="display" column="is_display" />
<property name="order" column="orderTime"/>
</class>
</hibernate-mapping>在执行测试方法时,报错:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, img, type, standard, simple_desc, is_display, orderTime) values (null, nul' at line 1
这个错很常见,但怎么改都不起作用!
public void testProduct() { ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ProductDao productDao = (ProductDao) context.getBean("productDao");
Product pro = new Product();
pro.setName("pp");
System.out.println(productDao.saveOrUpdateProduct(pro));
}
这是实体
private Integer id;
private String num;
private String name;
private Integer price;
private Integer transFee;
private int use;
private int recod;
private String standard;
private String desc;
private String img;
private String type;
private String simpleDesc;
private int display;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public Integer getTransFee() {
return transFee;
}
public void setTransFee(Integer transFee) {
this.transFee = transFee;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSimpleDesc() {
return simpleDesc;
}
public void setSimpleDesc(String simpleDesc) {
this.simpleDesc = simpleDesc;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public int getUse() {
return use;
}
public void setUse(int use) {
this.use = use;
}
public int getRecod() {
return recod;
}
public void setRecod(int recod) {
this.recod = recod;
}
public int getDisplay() {
return display;
}
public void setDisplay(int display) {
this.display = display;
}
这是dao方法,此方法继承hibernatetemplete,sessionfactory由spring注入
public boolean saveOrUpdateProduct(Product product) {
try {
super.saveOrUpdate(product);
return true;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
这是映射文件:
<hibernate-mapping>
<class name="com.product.model.Product" table="t_product">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="num" />
<property name="price" />
<property name="transFee" column="trans_fee" />
<property name="use" column="is_use"/>
<property name="name"/>
<property name="recod" column="is_recod"/>
<property name="desc" />
<property name="img" />
<property name="type" />
<property name="standard"/>
<property name="simpleDesc" column="simple_desc" />
<property name="display" column="is_display" />
<property name="order" column="orderTime"/>
</class>
</hibernate-mapping>在执行测试方法时,报错:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, img, type, standard, simple_desc, is_display, orderTime) values (null, nul' at line 1
这个错很常见,但怎么改都不起作用!
关键看你的sql语句
添上对应关系试试
或者是 数据库表设置为非空 但你却插入了空字符串的事。