搭建ssh框架项目(二)

一、创建dao层

  (1)创建接口ICommonDao.java

package com.cppdy.ssh.dao;

public interface ICommonDao<T> {
    public void save(T entity);
}
ICommonDao.java

  (2)创建实现类CommonDaoImpl.java,实现ICommonDao.java接口、继承HibernateDaoSupport类(spring集成hibernate对数据库的增删改查)

package com.cppdy.ssh.dao.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cppdy.ssh.dao.ICommonDao;

public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {

    /**  
    * @Name: save
    * @Description: 保存对象的方法
    * @Parameters: T entity 对象
    * @Return: 无
    */
    public void save(T entity) {
        this.getHibernateTemplate().save(entity);
    }

}
CommonDaoImpl.java

  (3)创建接口IElecTextDao.java

package com.cppdy.ssh.dao;

import com.cppdy.ssh.domain.ElecText;

public interface IElecTextDao extends ICommonDao<ElecText> {
    
}
IElecTextDao.java

  (4)创建实现类ElecTextDaoImpl.java,实现IElecTextDao.java接口、继承实现类CommonDaoImpl.java

package com.cppdy.ssh.dao.impl;

import com.cppdy.ssh.dao.IElecTextDao;
import com.cppdy.ssh.domain.ElecText;

public class ElecTextDaoImpl extends CommonDaoImpl<ElecText> implements IElecTextDao {
    
}
ElecTextDaoImpl.java

二、配置spring的beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

<!-- 创建sessionFactory,这是spring整合hibernate的入口 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>
            classpath:hibernate.cfg.xml
        </value>
    </property>
</bean>

<bean id="elecTextDao" class="com.cppdy.ssh.dao.impl.ElecTextDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>
beans.xml

三、配置事物自动提交(在hibernate.cfg.xml加入<property name="hibernate.connection.autocommit">true</property>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 数据库用户名 -->
        <property name="hibernate.connection.username">root</property>
        <!-- 数据库密码 -->
        <property name="hibernate.connection.password">root</property>
        <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 数据库连接地址 -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh</property>
        <!-- 事物自动提交 -->
        <property name="hibernate.connection.autocommit">true</property>
        <!-- 配置方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- 无表自动创建,有表自动追加数据 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 显示sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 映射文件 -->
        <mapping resource="com/cppdy/ssh/domain/ElecText.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
hibernate.cfg.xml

四、创建测试类TestDao.java

package junit;

import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cppdy.ssh.dao.IElecTextDao;
import com.cppdy.ssh.domain.ElecText;

public class TestDao {
    @Test
    public void saveElecText(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        IElecTextDao elecTextDao = (IElecTextDao)ac.getBean("elecTextDao");
        //实例化PO对象,赋值,执行保存
        ElecText elecText = new ElecText();
        elecText.setTextName("测试DAO名称");
        elecText.setTextDate(new Date());
        elecText.setTextRemark("测试DAO备注");
        elecTextDao.save(elecText);
    }
}
TestDao.java

五、查看数据库

六、项目结构

原文地址:https://www.cnblogs.com/jiefu/p/9846941.html