我使用休眠作为持久层,这是我的代码示例,但这里是我的 hql 查询:
Query q = s.createQuery("from Login where name=:user and passw =:passw");
q.setParameter("user",username);
q.setParameter("passw", passw);
Query q = s.createSQLQuery("SELECT * from Good where Good.supplier =:supl AND Good.name =:gname AND Good.dates >=:sdate and Good.dates <=:fdate").addEntity(Good.class);
q.setParameter("supl",sup);
q.setParameter("gname", gname);
q.setParameter("sdate", sdate);
q.setParameter("fdate",fdate);
sdate 和 fdate 参数是字符串,在这种情况下可以吗?它抛出这个异常:
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 '.login login0_ where login0_.name='Tom'' at line 1
登录.hbm.xml
<hibernate-mapping>
<class name="kz.bimash.FoodSec.model.Login" table="login" catalog="foodsec">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="increment" />
</id>
<property name="name" type="string">
<column name="name" length="50" not-null="true" unique="true" />
</property>
<property name="passw" type="string">
<column name="passw" length="50" not-null="true" />
</property>
<property name="type" type="string">
<column name="type" length="45" not-null="true" />
</property>
<property name="userId" type="int">
<column name="userId" not-null="true" />
</property>
</class>
登录 pojo 类
public class Login implements java.io.Serializable {
private Integer id;
private String name;
private String passw;
private String type;
private int userId;
public Login() {
}
public Login(String username, String password, String type, int userId) {
this.name = username;
this.passw = password;
this.type = type;
this.userId = userId;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String username) {
this.name = username;
}
public String getPassw() {
return this.passw;
}
public void setPassw(String password) {
this.passw = password;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
登录DAO类
@Repository
public class LoginDAO {
@Autowired
private SessionFactory sf;
@SuppressWarnings("empty-statement")
public String[] Authorise(String username, String passw){
Session s = sf.getCurrentSession();
s.beginTransaction();
Query q = s.createQuery("from Login where name=:user and passw =:passw");
q.setParameter("user",username);
q.setParameter("passw", passw);
q.setMaxResults(1);
String[] str = null;
Login login = null;
for(Iterator it = q.iterate(); it.hasNext();){
login = (Login)it.next();
}
if(login != null){
str = new String[]{login.getType(), String.valueOf(login.getUserId())};
}
s.getTransaction().commit();
return str;
}
public boolean checkLogin(String username){
Session s = sf.getCurrentSession();
s.beginTransaction();
// String s_sql ="SELECT * FROM Login WHERE NAME="+username;
Query q = s.createQuery("from Login where name = :usern");
q.setParameter("usern", username);
// Query q=s.createSQLQuery(s_sql);
List<Login> logins =null;
logins= (List<Login>)q.list();
s.getTransaction().commit();
if(logins !=null)
return true;
else
return false;
}
}