spring,hibernate,dao操作数据库模…

来源:互联网 发布:淘宝站内营销手段 编辑:程序博客网 时间:2024/05/21 11:17

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 fromusers;

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) throwsException;

public int getCount() throws Exception;

}

daoImpl:

package com.ssh.dao.impl;

import java.util.List;

import org.hibernate.Query;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ssh.dao.UsersDao;
import com.ssh.orm.Users;

public class UsersDaoImpl extends HibernateDaoSupport implementsUsersDao {
public boolean delete(int id) throws Exception {
   String hql = "delete fromUsers where id=:id";// 注意删除的写法!
   Query q =this.getSession().createQuery(hql);
   q.setParameter("id",id);
   if (q.executeUpdate()> 0) {
    returntrue;
   }
   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 uwhere 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 = "fromUsers";
   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) throwsException {
   List all = null;
   String hql = "fromUsers";
   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;
importorg.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 = newUsers();
   // 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 (Objectu : 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">
   <propertyname="driverClassName">
   <value>com.mysql.jdbc.Driver</value>
  </property>
   <propertyname="url">
   <value>jdbc:mysql://127.0.0.1:3306/zzcfront</value>
  </property>
   <propertyname="username">
   <value>root</value>
  </property>
   <propertyname="password">
   <value>root</value>
  </property>
</bean>

<beanid="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <propertyname="dataSource">
   <ref bean="dataSource" />
  </property>
   <propertyname="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <propkey="hibernate.show_sql">true</prop>
   </props>
  </property>
   <propertyname="mappingResources">
   <list>
    <value>com/ssh/orm/Users.hbm.xml</value>
   </list>
  </property>
</bean>
<bean id="hibernateTemplate"
  class="org.springframework.orm.hibernate3.HibernateTemplate">
   <propertyname="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">
   <propertyname="hibernateTemplate">
   <ref bean="hibernateTemplate"/>
  </property>
</bean>

</beans>

Hibernate映射文件和Users 类不在写出。

0 0
原创粉丝点击