首页 > 代码库 > Hibernate、Spring、myBatis下增删改查的Dao与DaoImpI
Hibernate、Spring、myBatis下增删改查的Dao与DaoImpI
Hibernate
Dao
//Dao.java import java.util.List; /** * @author Administrator * */ public interface Dao { void saveObject(Object object); void updateObject(Object object); void deleteObject(Object object); Object getObject(String HQL); List<?> findWithPage(int page, int rows, String HQL); List<?> findAllInfo(String HQL); }
DaoImpI
import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import com.demo.utils.HibernateUtils; /** * @author Administrator * */ @SuppressWarnings("rawtypes") public class DaoImpI implements Dao { /* * (non-Javadoc) * * @see com.demo.dao.Dao#saveObject(java.lang.Object) */ @Override public void saveObject(Object object) { Session session = HibernateUtils.getSession(); try { session.getTransaction().begin(); session.save(object); session.getTransaction().commit(); } catch (Exception e) { e.getStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.getClosed(session); } } /* * (non-Javadoc) * * @see com.demo.dao.Dao#updateObject(java.lang.Object) */ @Override public void updateObject(Object object) { Session session = HibernateUtils.getSession(); try { session.getTransaction().begin(); session.update(object); session.getTransaction().commit(); } catch (Exception e) { e.getStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.getClosed(session); } } /* * (non-Javadoc) * * @see com.demo.dao.Dao#deleteObject(java.lang.Object) */ @Override public void deleteObject(Object object) { Session session = HibernateUtils.getSession(); try { session.getTransaction().begin(); session.delete(object); session.getTransaction().commit(); } catch (Exception e) { e.getStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.getClosed(session); } } /* * (non-Javadoc) * * @see com.demo.dao.Dao#findWithPage(int, int, java.lang.String) */ @Override public List<?> findWithPage(int page, int rows, String HQL) { List<?> list = new ArrayList(); Session session = HibernateUtils.getSession(); try { list=session.createQuery(HQL).setFirstResult((page - 1) * rows) .setMaxResults(rows).list(); } catch (Exception e) { e.getStackTrace(); } finally { HibernateUtils.getClosed(session); } return list; } /* * (non-Javadoc) * * @see com.demo.dao.Dao#findAllInfo(java.lang.String) */ @Override public List<?> findAllInfo(String HQL) { Session session = HibernateUtils.getSession(); List<?> list = new ArrayList(); try { list=session.createQuery(HQL).list(); } catch (Exception e) { e.getStackTrace(); } finally { HibernateUtils.getClosed(session); } return list; } @Override public Object getObject(String HQL) { Session session = HibernateUtils.getSession(); Object object = null; try { object=session.createQuery(HQL).uniqueResult(); } catch (Exception e) { } return object; } }
Spring
Dao
//AssetsManagerDao.java import java.util.List; /** * 固定资产管理包括如下: * 资产管理资,产移动管理,资产报废管理,维护维修管理,库存内容,使用内容 * */ public interface AssetsManagerDao { /** * 添加或更新一个对象 * @param obj */ void saveOrUpdateObject(Object obj); /** * 删除一个对象 * @param obj */ void deleteObject(Object obj); /** * 通过HQL语言查询一个对象 * @param HQL * @return */ Object getObject(String HQL); /** * 分页查询数据 * @param page * @param rows * @param HQL * @return */ List<?>findWithPage(int page,int rows,String HQL); /** * 查询全部数据 * @param HQL * @return */ List<?>findAllInfo(String HQL); }
DaoImpI
//AssetsManagerDaoImpI.java import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.cissst.dao.AssetsManagerDao; public class AssetsManagerDaoImpI extends HibernateDaoSupport implements AssetsManagerDao{ public void saveOrUpdateObject(Object obj) { super.getHibernateTemplate().saveOrUpdate(obj); super.getHibernateTemplate().flush(); super.getHibernateTemplate().clear(); } public void deleteObject(Object obj) { super.getHibernateTemplate().delete(obj); super.getHibernateTemplate().flush(); super.getHibernateTemplate().clear(); } public Object getObject(final String HQL) { return super.getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException, SQLException { return session.createQuery(HQL).uniqueResult(); } }); } public List<?> findWithPage(final int page, final int rows, final String HQL) { return super.getHibernateTemplate().executeFind(new HibernateCallback<List<?>>() { public List<?> doInHibernate(Session session) throws HibernateException, SQLException { return session.createQuery(HQL).setFirstResult((page-1)*rows).setMaxResults(rows).list(); } }); } public List<?> findAllInfo(final String HQL) { return super.getHibernateTemplate().executeFind(new HibernateCallback<List<?>>() { public List<?> doInHibernate(Session session) throws HibernateException, SQLException { return session.createQuery(HQL).list(); } }); } }
myBatis
Dao
//UsersMapper.java import java.util.List; import com.cissst.bean.Users; /** * 定义数据访问层DAO接口 * @author JSZX * */ public interface UsersMapper { /** * 查询用户全部信息 * @return */ List<Users>findAllUserInfo(Users users); void insertUserInfo(Users users); void updateUserInfo(Users users); void deleteUserInfoById(int userId); }
<!--由于myBatis用的是XML因此给出项目的配置文件--> <!-- UserMapper.xml 与Dao同一文件夹 --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 该配置文件就是前面所说的 配置sql --> <mapper namespace="com.cissst.dao.data.UsersMapper"> <resultMap id="userMapper" type="com.cissst.bean.Users"> <id property="id" column="id" /> <result property="username" column="username" /> <result property="password" column="password" /> <result property="creatTime" column="creatTime" /> <result property="endTime" column="endTime" /> <association property="dept" javaType="com.cissst.bean.Dept"> <id property="id" column="deptId" /> <result property="deptName" column="deptName" /> </association> </resultMap> <insert id="insertUserInfo" parameterType="com.cissst.bean.Users" useGeneratedKeys="true" keyProperty="id" keyColumn="id"> <!-- <selectKey keyProperty="" keyColumn="" order="BEFORE"> --> <!-- select max(customer_id)+1 from dual --> <!-- </selectKey> --> insert into tbusers(username,password,creatTime,endTime,deptId) values(#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{creatTime,jdbcType=VARCHAR}, #{endTime,jdbcType=VARCHAR}, #{dept.id,jdbcType=INTEGER}) </insert> <select id="findAllUserInfo" resultMap="userMapper" statementType="PREPARED"> select u.id as id, u.username as username ,u.password as password,u.creatTime as creatTime ,u.endTime as endTime ,d.deptName as deptName from tbusers u,tbdept d <where> u.deptId=d.deptId <!-- <if test="id >0"> --> <!-- <![CDATA[id>#{id}]]> --> <!-- </if> --> <if test="username !=null and username !=‘‘"> and u.username like ‘%${username}%‘ </if> <if test="password !=null and password !=‘‘"> and u.password like ‘%${password}%‘ </if> <if test="dept.deptName!=‘‘ and dept.deptName !=null"> and d.deptName like ‘%${dept.deptName}%‘ </if> </where> </select> <update id="updateUserInfo" parameterType="com.cissst.bean.Users" statementType="PREPARED"> update tbusers <set> <if test="username!=null and username!=‘‘"> username=#{username,jdbcType=VARCHAR}, </if> <if test="password!=null and password!=‘‘"> password=#{password,jdbcType=VARCHAR}, </if> <if test="creatTime!=null and creatTime!=‘‘"> creatTime=#{creatTime,jdbcType=VARCHAR}, </if> <if test="endTime!=null and endTime!=‘‘"> endTime=#{endTime,jdbcType=VARCHAR}, </if> <if test="dept.id>0"> deptId=#{dept.id,jdbcType=INTEGER} </if> </set> where id=#{id} </update> <delete id="delUserInfo" parameterType="_int"> delete from tbusers <where> <if test="id>0"> id=#{id} </if> </where> </delete> </mapper>
<!--mapping下的配置文件一块给出 --> <!-- mybatis-configuraction.xml--> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 该配置文件主要配置的是连接数据库的信息 --> <configuration> <!-- <settings> --> <!-- <setting name="cacheEnabled" value="http://www.mamicode.com/true"/> --> <!-- <setting name="lazyLoadingEnabled" value="http://www.mamicode.com/true"/> --> <!-- <setting name="defaultStatementTimeout" value="http://www.mamicode.com/25000"/> --> <!-- </settings> --> <typeAliases> <typeAlias alias="usersAlias" type="com.cissst.bean.Users" /> <typeAlias alias="deptAlias" type="com.cissst.bean.Dept" /> </typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="username" value="http://www.mamicode.com/root"/> <property name="password" value="http://www.mamicode.com/root"/> <property name="url" value="http://www.mamicode.com/jdbc:mysql://127.0.0.1/mydatabase"/> <property name="driver" value="http://www.mamicode.com/com.mysql.jdbc.Driver"/> </dataSource> </environment> </environments> <mappers> <mapper resource="/com/cissst/dao/data/UserMapper.xml"/> </mappers> </configuration>
本文出自 “可爱的xiaopp” 博客,转载请与作者联系!
Hibernate、Spring、myBatis下增删改查的Dao与DaoImpI
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。