spring hibernate crud頭回弄這個 直接上代碼 有用給個贊

		頭回弄這個 直接上代碼 有用給個贊
```/
package wlt.tab.dao;

import java.util.List;

import wlt.tab.vo.Student;

public interface StudentDao {
	 public List<Student> findAll();
     public Student findBookById(int id);
     public void save(Student book);
     public void update(Student book);
     public void delete(Student id);
}


package wlt.tab.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import wlt.tab.vo.Student;

public class StudentDaoImp implements StudentDao{

	private SessionFactory sessionFactory;
	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	
	public void delete(Student id) {
		    Session session=this.sessionFactory.openSession();
		    session.beginTransaction();
		    session.delete(id);
			session.beginTransaction().commit();
			session.close();
	}

	public List<Student> findAll() {
		// Student 實體類名
		   String hql = "from Student";
		   Query query = this.sessionFactory.openSession().createQuery(hql);
		   return query.list();
	}

	public Student findBookById(int id) {
		// TODO Auto-generated method stub
		
		Student student=(Student)this.sessionFactory.openSession().get(Student.class, id);
		
		return student;
	}

	public void save(Student book) {
		    Session session=this.sessionFactory.openSession();
		    session.beginTransaction();
		    session.save(book);
			session.beginTransaction().commit();
			session.close();
	}

	public void update(Student book) {
		try {
			   Session session=this.sessionFactory.openSession();
			    session.beginTransaction();
			    session.update(book);
				session.beginTransaction().commit();
				session.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		  
		
	}

}


package wlt.tab.service;

import java.util.List;

import wlt.tab.vo.Student;

public interface StudentService {
	
	  public Student findStudentById(int id);
      public void saveBook(Student book);
      public List<Student> findAll();
      public boolean delete(Student id);
      public void update(Student book);
      
}
package wlt.tab.service;

import java.util.List;

import wlt.tab.dao.StudentDao;
import wlt.tab.vo.Student;

public class StudentServiceImp implements StudentService {

	
	private StudentDao dao;
	
	
	
	public StudentDao getDao() {
		return dao;
	}

	public void setDao(StudentDao dao) {
		this.dao = dao;
	}
	
	

	public List<Student> findAll() {
		// TODO Auto-generated method stub
		return dao.findAll();
	}

	public Student findStudentById(int id) {
		// TODO Auto-generated method stub
		return dao.findBookById(id);
	}

	public void saveBook(Student book) {
		// TODO Auto-generated method stub
		dao.save(book);
	}

	public boolean delete(Student id) {
		dao.delete(id);
		return true;
	}

	public void update(Student book) {
		// TODO Auto-generated method stub
		
		dao.update(book);
	}

}


package wlt.tab.vo;

import java.io.Serializable;
import java.util.Date;

@SuppressWarnings("serial")
public class Student implements Serializable {
	
	private int emp_id;
    private String emp_name;
    private String emp_der;
    private int emp_age;
    private Date emp_date;
    private String emp_address;
    
	public int getEmp_id() {
		return emp_id;
	}
	public void setEmp_id(int empId) {
		emp_id = empId;
	}
	public String getEmp_name() {
		return emp_name;
	}
	public void setEmp_name(String empName) {
		emp_name = empName;
	}
	public String getEmp_der() {
		return emp_der;
	}
	public void setEmp_der(String empDer) {
		emp_der = empDer;
	}
	public int getEmp_age() {
		return emp_age;
	}
	public void setEmp_age(int empAge) {
		emp_age = empAge;
	}
	public Date getEmp_date() {
		return emp_date;
	}
	public void setEmp_date(Date empDate) {
		emp_date = empDate;
	}
	public String getEmp_address() {
		return emp_address;
	}
	public void setEmp_address(String empAddress) {
		emp_address = empAddress;
	}
	@Override
	public String toString() {
		return "Student [emp_address=" + emp_address + ", emp_age=" + emp_age
				+ ", emp_date=" + emp_date + ", emp_der=" + emp_der
				+ ", emp_id=" + emp_id + ", emp_name=" + emp_name + "]";
	}
	public Student(int empId, String empName, String empDer, int empAge,
			Date empDate, String empAddress) {
		super();
		emp_id = empId;
		emp_name = empName;
		emp_der = empDer;
		emp_age = empAge;
		emp_date = empDate;
		emp_address = empAddress;
	}
	
	public Student(){
		
	}

	
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="wlt.tab.vo.Student" table="emp">
        <id name="emp_id" type="int">
            <column name="emp_id" />
            <generator class="native" />
        </id>
        <property name="emp_name" type="java.lang.String">
            <column name="emp_name" length="25" />
        </property>
        <property name="emp_der" type="java.lang.String">
            <column name="emp_der" length="25" />
        </property>
         <property name="emp_age" type="int">
            <column name="emp_age" length="25" />
        </property>
         <property name="emp_date" type="time">
            <column name="emp_date" length="25" />
        </property>
        
         <property name="emp_address" type="java.lang.String">
            <column name="emp_address" length="25" />
        </property>
    </class>
</hibernate-mapping>



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

	<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="username" value="system"></property>
		<property name="password" value="wltpass"></property>
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property>
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
	</bean>

	<bean id="sf"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="ds">
		</property>
		<property name="hibernateProperties">
			<props>
				 <prop key="hibernate.dialect">  
                     org.hibernate.dialect.Oracle10gDialect
                </prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>
				<prop key="current_session_context_class">thread</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>wlt/tab/vo/Student.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	
      <bean id="bookDao" class="wlt.tab.dao.StudentDaoImp">
         <property name="sessionFactory" ref="sf"></property>
      </bean>
   
      <bean id="StudentService" class="wlt.tab.service.StudentServiceImp">
         <property name="dao" ref="bookDao"></property>
      </bean><!--
      
      
      
	 配置聲明事務 創建事務管理器對象 
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sf"></property>
	</bean>


	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" read-only="false" />
			<tx:method name="insert*" propagation="REQUIRED" read-only="false" />
			<tx:method name="saveBook" propagation="REQUIRED" read-only="false" />
			<tx:method name="remove*" propagation="REQUIRED" read-only="false" />
			<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false" />
			<tx:method name="modify*" propagation="REQUIRED" read-only="false" />
			<tx:method name="getAll*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	通過aop把代碼卡在方法的前後 
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* wlt.tab.*.*(..))" />
	</aop:config>


--></beans>

package wlt.tab.action;

import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import wlt.tab.service.StudentService;
import wlt.tab.vo.Student;

public class MyText {

	public static void main(String[] args) {

		int len = MyText.finabyid();
		System.out.println(len);
	}

	public static int finall() {
		ApplicationContext ioc = new ClassPathXmlApplicationContext(
				"classpath:applicationContext.xml");

		StudentService ts = (StudentService) ioc.getBean("StudentService");

		List<Student> list = ts.findAll();
		if (list.size() > 0) {
			for (Student s : list) {
				System.out.println("--->>>" + s.toString());
			}
			return 1;
		}

		return -1;
	}

	public static int finabyid() {
		ApplicationContext ioc = new ClassPathXmlApplicationContext(
				"classpath:applicationContext.xml");
		StudentService ts = (StudentService) ioc.getBean("StudentService");
		Student list = ts.findStudentById(23);
		if (list != null) {
			System.out.println(list.toString());
			return 1;
		}
		return -1;
	}

	//添加
	public static int save() {
		ApplicationContext ioc = new ClassPathXmlApplicationContext(
				"classpath:applicationContext.xml");
		StudentService ts = (StudentService) ioc.getBean("StudentService");
		ts.saveBook(new Student(66, "ss", "ss", 28, new Date(), "藍田"));
		return 1;
	}

	
	//刪除
	public static int del() {
		ApplicationContext ioc = new ClassPathXmlApplicationContext(
				"classpath:applicationContext.xml");
		StudentService ts = (StudentService) ioc.getBean("StudentService");
		
		Student student=new Student();
		student.setEmp_id(22);
		
		boolean b = ts.delete(student);

		if (b) {
			return 1;
		}
		return -1;
	}
	public static int updt(){
		ApplicationContext ioc = new ClassPathXmlApplicationContext(
		"classpath:applicationContext.xml");
         StudentService ts = (StudentService) ioc.getBean("StudentService");
        
         ts.update(new  Student(21, "小斯", "女", 27, new Date(), "藍田"));
		return 1;
	}

}

測試

![在這裏插入圖片描述](https://img-blog.csdnimg.cn/2020021911123292.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4MjE4MzA3,size_16,color_FFFFFF,t_70)![](https://img-blog.csdnimg.cn/20200219111140959.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4MjE4MzA3,size_16,color_FFFFFF,t_70)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章