在这里插入代码片
javabean
package wlt.net.vo;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
@Entity
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(){
}
}
xxx.hbm.xml
<property name="emp_address" type="java.lang.String">
<column name="emp_address" length="25" />
</property>
</class>
applicationContext.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”
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:context=“http://www.springframework.org/schema/context”
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-2.5.xsd
">
<!-- 自动扫描 -->
<context:component-scan base-package="wlt.net" />
<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.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>wlt/net/vo/Student.hbm.xml</value>
</list>
</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="*" />
</tx:attributes>
</tx:advice>
<!--通过aop把代码卡在方法的前后 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* wlt.net.*.*(..))" />
</aop:config>
<!-- 配置 HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>
</bean>
StudentDao
package wlt.net.dao;
import java.util.List;
import wlt.net.vo.Student;
public interface StudentDao {
boolean insertstud(Student student);
boolean updatestud(Student student);
boolean deletestud(Student student);
Student seletefinbyid(int empid);
List selectall();
//根据模糊查询查询
List selectbyname(Object name,Object object);
//根据name查询
List selectbyname2(String sql,Object name);
//根据name模糊查询
List selectbyname3(Object name);
}
StudentDaoimpl
package wlt.net.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import wlt.net.vo.Student;
@Repository(“studentDaoimpl”)
public class StudentDaoimpl implements StudentDao{
@Autowired
private HibernateTemplate template;
@Transactional
public boolean deletestud(Student student) {
template.delete(student);
return true;
}
@Transactional
public boolean insertstud(Student student) {
// TODO Auto-generated method stub
template.save(student);
return true;
}
public Student seletefinbyid(int empid) {
// TODO Auto-generated method stub
Student student=template.get(Student.class, empid);
return student;
}
@Transactional
public boolean updatestud(Student student) {
// TODO Auto-generated method stub
template.update(student);
return true;
}
public List<Student> selectall() {
// TODO Auto-generated method stub
return template.find("from Student");
}
public List<Student> selectbyname(Object name,Object object) {
// TODO Auto-generated method stub
List<Student> list=template.find("from Student b where b.emp_name like'%"+name+"%' and b.emp_id like '%"+object+"%'");
return list;
}
@SuppressWarnings("unchecked")
public List<Student> selectbyname2(String sql,Object name) {
List<Student> list=template.find(sql,name);
return list;
}
public List<Student> selectbyname3(Object name) {
List<Student> s = template.find("from Student where emp_name like '%"+name+"%'");
return s;
}
}
MyText
package wlt.net.text;
import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import wlt.net.dao.StudentDaoimpl;
import wlt.net.vo.Student;
@ContextConfiguration(“classpath:applicationContext.xml”)
public class MyText {
public static void main(String[] args) {
/*
* ApplicationContext ioc = new ClassPathXmlApplicationContext(
* "classpath:applicationContext.xml");
*
* StudDao ts = (StudDao) ioc.getBean("studDao"); ts.select();
*/
new MyText().finbyname2();
}
public void finall() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
List<Student> student = daoimpl.selectall();
for (Student s : student) {
System.out.println(s.toString());
}
}
public void finbyname3() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
List<Student> student = daoimpl.selectbyname3("李");
for (Student s : student) {
System.out.println(s.toString());
}
}
public void finbyname2() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
//List<Student> student = daoimpl.selectbyname2("from Student u where u.emp_name like ?", "%李%");
List<Student> student = daoimpl.selectbyname2("from Student u where u.emp_name = ?", "小期");
for (Student s : student) {
System.out.println(s.toString());
}
}
public void finbyname() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
List<Student> student = daoimpl.selectbyname("李",4);
for (Student s : student) {
System.out.println(s.toString());
}
}
public void finbyid() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
Student student = daoimpl.seletefinbyid(7);
System.out.println(student.toString());
}
public void save() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
boolean b = daoimpl.insertstud(new Student(17, "小七", "女", 27,
new Date(), "蓝田"));
if (b) {
System.out.println("ok");
} else {
System.out.println("no");
}
}
public void updt() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
boolean b = daoimpl.updatestud(new Student(17, "小期", "男", 27,
new Date(), "蓝田"));
if (b) {
System.out.println("ok");
} else {
System.out.println("no");
}
}
public void del() {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
StudentDaoimpl daoimpl = (StudentDaoimpl) ioc.getBean("studentDaoimpl");
Student student = new Student();
student.setEmp_id(23);
boolean b = daoimpl.deletestud(student);
if (b) {
System.out.println("ok");
} else {
System.out.println("no");
}
}
}
结果: