table:
create table users
(
id int auto_increment not null primary key,
name varchar(32) not null,
password varchar(32) not null
);
select * from users
insert into users (name,password) values('text','text');
insert into users(name,password) select name,password from users;
dao:
package com.ssh.dao;
import java.util.List;
import com.ssh.orm.Users;
public interface UsersDao {
public boolean insert(Users users) throws Exception;
public Users select(int id) throws Exception;
public boolean update(Users users) throws Exception;
public boolean delete(int id) throws Exception;
public List selectAll() throws Exception;
public List selectAllByPage(int curPage, int lineSize) throws Exception;
public int getCount() throws Exception;
}
daoImpl:
package com.ssh.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.ssh.dao.UsersDao;
import com.ssh.orm.Users;
public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao {
public boolean delete(int id) throws Exception {
String hql = "delete from Users where id=:id";// 注意删除的写法!
Query q = this.getSession().createQuery(hql);
q.setParameter("id", id);
if (q.executeUpdate() > 0) {
return true;
}
return false;
}
public boolean insert(Users users) throws Exception {
this.getSession().save(users);
return true;
}
public Users select(int id) throws Exception {
String hql = "from Users u where u.id=:id";
Query q = this.getSession().createQuery(hql);
q.setParameter("id", id);
List l = q.list();
if (l.size() > 0) {
return (Users) l.get(0);
}
return null;
}
public boolean update(Users users) throws Exception {
this.getHibernateTemplate().update(users);//注意更新操作
return true;
}
public List selectAll() throws Exception {
List all = null;
String hql = "from Users";
Query q = this.getSession().createQuery(hql);
List l = q.list();
if (l.size() > 0) {
all = l;
}
return all;
}
public List selectAllByPage(int curPage, int lineSize) throws Exception {
List all = null;
String hql = "from Users";
Query q = this.getSession().createQuery(hql);
q.setFirstResult((curPage - 1) * lineSize);
q.setMaxResults(lineSize);
List l = q.list();
if (l.size() > 0) {
all = l;
}
return all;
}
public int getCount() throws Exception {
String hql = "select count(*) from Users";
Query q = this.getSession().createQuery(hql);
if (q.list().size() > 0) {
return (Integer) q.list().get(0);
}
return 0;
}
}
operate:
package com.ssh.text;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ssh.dao.impl.UsersDaoImpl;
import com.ssh.orm.Users;
public class Text {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UsersDaoImpl userDaoImpl = (UsersDaoImpl) context
.getBean("userDaoImpl");
// Users user = new Users();
// user.setName("zzc");
// user.setPassword("front");
// System.out.println(userDaoImpl.insert(user));
// Users u = userDaoImpl.select(2);
// System.out.println(u.getName());
// u.setName("text");
// System.out.println(userDaoImpl.update(u));
// System.out.println(userDaoImpl.delete(2));
List l = userDaoImpl.selectAllByPage(5, 10);
// for (int i = 0; i < l.size(); i++) {
// System.out.println(((Users) l.get(i)).getName());
// }
if (l != null) {
for (Object u : l) {
System.out.println(((Users) u).getId() + ((Users) u).getName());
}
} else {
System.out.println("没有记录");
}
System.out.println(userDaoImpl.getCount());
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/zzcfront</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ssh/orm/Users.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userDao" class="com.ssh.dao.UsersDao" abstract="true"></bean>
<bean id="userDaoImpl" class="com.ssh.dao.impl.UsersDaoImpl"
parent="userDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
</beans>