直接上码!
public class OrderDao extends BaseDao<String, Orders> implements IOrderDao {
@SuppressWarnings("unchecked")
public List<OrderDTO> getOrdersByUsername(String username) {
String sql = "SELECT"
+ " BILLADDR1 as {billaddr1},"
+ "BILLADDR2 as {billaddr2},"
+ "BILLCITY as {billCity},"
+ "BILLCOUNTRY as {billCountry},"
+ "BILLSTATE as {billState},"
+ "BILLTOFIRSTNAME as {billToFirstName},"
+ "BILLTOLASTNAME as {billToLastName},"
+ "BILLZIP as {billZip},"
+ "SHIPADDR1 as {shipaddr1},"
+ "SHIPADDR2 as {shipaddr2},"
+ "SHIPCITY as {shipCity},"
+ "SHIPCOUNTRY as {shipCountry},"
+ "SHIPSTATE as {shipState},"
+ "SHIPTOFIRSTNAME as {shipToFirstName},"
+ "SHIPTOLASTNAME as {shipToLastName},"
+ "SHIPZIP as {shipZip},"
+ "CARDTYPE as {cardType},"
+ "COURIER as {courier},"
+ "CREDITCARD as {creditCard},"
+ "EXPRDATE as {exprdate},"
+ "LOCALE as {locale},"
+ "ORDERDATE as {orderDate},"
+ "ORDERS.ORDERID as {orderId},"
+ "TOTALPRICE as {totalPrice},"
+ "USERID as {userid},"
+ "STATUS as {status}"
+ " FROM ORDERS ,ORDERSTATUS"
+ " WHERE ORDERS.USERID = ?"
+ " AND ORDERS.ORDERID = ORDERSTATUS.ORDERID"
+ " ORDER BY ORDERDATE";
Query query = this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(OrderDTO.class));
query.setString(0, username);
return query.list();
}
上面是ssh集成,hibernate查询。
返回非受管实体,结果转换器返回OrderDTO的列表。
以下是OrderDTO类:
public class OrderDTO implements Serializable {
private static final long serialVersionUID = 6321792448424424931L;
private int orderId;
// private String username;
private String userid;
private Date orderDate;
// private String shipAddress1;
// private String shipAddress2;
private String shipaddr1;
private String shipaddr2;
private String shipCity;
private String shipState;
private String shipZip;
private String shipCountry;
// private String billAddress1;
// private String billAddress2;
private String billaddr1;
private String billaddr2;
private String billCity;
private String billState;
private String billZip;
private String billCountry;
private String courier;
private BigDecimal totalPrice;
private String billToFirstName;
private String billToLastName;
private String shipToFirstName;
private String shipToLastName;
private String creditCard;
// private String expiryDate;
private String exprdate;
private String cardType;
private String locale;
private String status;
private List<LineItemDTO> lineItems = new ArrayList<LineItemDTO>();
get,set方法省略。
执行上面的查询,有语法错误:
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 'BILLADDR2 as ,BILLCITY as ,BILLCOUNTRY as ,BILLSTATE as ,BILLTOFIRSTNAME as ,BIL' at line 1
应该是不能取别名。
如果不用别名注射(alias injection names)
,OrderDTO中属性不能使用驼峰命名,属性必须全部小写才能正确返回。
求教育!求爽!
分享到:
相关推荐
16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...
16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...
16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...
16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...
16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...
16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. ...
14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. ...
16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. ...
14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. ...