Spring09 ssh注解开发

引入需要的jar包

复制代码
@Entity
public class Teacher {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
  private   Integer  tId;  //编号
  private   Integer  tAge; //年龄
  private   String  tName;//姓名
  private   Date  tDate;
  
  
  
@Override
public String toString() {
    return "Teacher [tId=" + tId + ", tage=" + tAge + ", tName=" + tName
            + ", tDate=" + tDate + "]";
}
public Teacher() {
    super();
}
public Teacher(Integer tId, Integer tage, String tName, Date tDate) {
    super();
    this.tId = tId;
    this.tAge = tage;
    this.tName = tName;
    this.tDate = tDate;
}
public Integer gettId() {
    return tId;
}
public void settId(Integer tId) {
    this.tId = tId;
}

public Integer gettAge() {
    return tAge;
}
public void settAge(Integer tAge) {
    this.tAge = tAge;
}
public String gettName() {
    return tName;
}
public void settName(String tName) {
    this.tName = tName;
}
public Date gettDate() {
    return tDate;
}
public void settDate(Date tDate) {
    this.tDate = tDate;
}
  
}
复制代码
复制代码
public interface TeacherDao {
    //新增
    void  addTeacher(Teacher teacher);
 
    //删除
    void  deleteTeacher(Teacher teacher);
    //修改
    void  updateTeacher(Teacher teacher);
    //查询
    List<Teacher>  findTeachers();
    //根据ID查询指定的teacher
    Teacher findById(Integer id);
    
}
复制代码
复制代码
@Repository("teacherDao")
public class TeacherDaoImpl implements TeacherDao {
    @Autowired   // byType
    private SessionFactory sessionFactory;
    
    // 新增
    public void addTeacher(Teacher teacher) {
        sessionFactory.getCurrentSession().save(teacher);
    }

    // 删除
    public  void deleteTeacher(Teacher teacher){
        sessionFactory.getCurrentSession().delete(teacher);
    }

    // 修改
    public void updateTeacher(Teacher teacher){
        sessionFactory.getCurrentSession().update(teacher);
    }

    // 查询
    public List<Teacher> findTeachers(){
        return sessionFactory.getCurrentSession().createQuery("from  Teacher").list();
    }

    
    public Teacher findById(Integer id) {
        //OpenSessionInView
        //return (Teacher)sessionFactory.getCurrentSession().get(Teacher.class, id);
        return (Teacher) sessionFactory.getCurrentSession().load(Teacher.class, id);
    }
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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


    
    
}
复制代码
复制代码
public interface TeacherService {

    // 新增
    void addTeacher(Teacher teacher);

    // 删除
    void deleteTeacher(Teacher teacher);

    // 修改
    void updateTeacher(Teacher teacher);

    // 查询
    List<Teacher> findTeachers();
    
       //根据ID查询指定的teacher
        Teacher findById(Integer id);
}
复制代码
复制代码
@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {

    @Resource(name="teacherDao")   //byName
    private TeacherDao dao;
    
    //新增
    @Transactional
    public void addTeacher(Teacher teacher) {
       dao.addTeacher(teacher);
    }
    //删除
    @Transactional
    public void deleteTeacher(Teacher teacher) {
        dao.deleteTeacher(teacher);
    }
    //修改
    @Transactional
    public void updateTeacher(Teacher teacher) {
        dao.updateTeacher(teacher);
    }

    //查询所有
    @Transactional(readOnly=true)
    public List<Teacher> findTeachers() {
       return    dao.findTeachers();
    }
    
    //查询指定的教师
    @Transactional(readOnly=true)
    public Teacher findById(Integer id) {
        return dao.findById(id);
    }
    
    public TeacherDao getDao() {
        return dao;
    }
    public void setDao(TeacherDao dao) {
        this.dao = dao;
    }
    
    
}
复制代码
复制代码
@Namespace("/")
@ParentPackage("struts-default")
@Component
public class AddAction extends ActionSupport {
    private  String  name;
    private  Integer  age;
    private  Integer  id;
    @Autowired
    @Qualifier("teacherService")   //@Resource(name="teacherService") 
    private TeacherService  service;
    
     public  String add(){
         System.out.println("进入ladd");
         Teacher teacher=new Teacher();
         teacher.settAge(age);
         teacher.settName(name);
         service.addTeacher(teacher);
         return  SUCCESS;
     }
     
     @Action(value="AddServlet",results={@Result(location="/success.jsp")})
     public  String find(){
         Teacher teacher=service.findById(id);
         System.out.println(teacher);
         return  SUCCESS;
     }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public TeacherService getService() {
        return service;
    }

    public void setService(TeacherService service) {
        this.service = service;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }     

}
复制代码
复制代码
<?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:context="http://www.springframework.org/schema/context"
    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/context 
        http://www.springframework.org/schema/context/spring-context.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">

    <!-- 配置数据源 dbcp数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driverClass}" />
        <property name="url" value="${jdbcUrl}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}"/>
    </bean>

    <!-- 使用配置文件 加载 数据库需要的4要素 经常使用 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    
    <!--配置sessionFactory -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 读取hibernate配置文件<property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
         <!-- 配置数据源 -->
         <property name="dataSource" ref="dataSource"></property>
         <!-- 扫描  包下面的  类 -->
         <property name="packagesToScan" value="cn.bdqn.bean"/>
         <property name="hibernateProperties">
          <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <!-- 当前的事务线程内  使用session   -->
            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
          </props>
         </property>
     </bean>

 <!-- 开启扫描包 -->
 <context:component-scan base-package="cn.bdqn.*"/>

<!--  配置事务管理器  -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>
  <!--事务的注解  -->
  <tx:annotation-driven transaction-manager="transactionManager"/>
 

</beans>
复制代码
复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  
  
  <!-- 配置全局监听器 确保 容器 对象 只被实例化一次! -->
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
  <!--  默认xml名称 必须是 applicationContext.xml 必须在 WEB-INF的根目录下
     现在我们 设置applicationContext.xml文件的路径     我们也可以更改 xml文件的名称 -->
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  
  
   
   <!-- 设置openSessionInView  必须在struts2的核心控制器 之前  不然会起作用 -->
   <filter>
   <filter-name>open</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  
   <filter-mapping>
     <filter-name>open</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping> 
  
  
  <!--配置struts2的核心控制器  -->
     <filter>
   <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
   <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping> 
  
  

  
  
  <servlet>
    <servlet-name>AddServlet1</servlet-name>
    <servlet-class>cn.bdqn.servlet.AddServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AddServlet</servlet-name>
    <url-pattern>/AddServlet</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
原文地址:https://www.cnblogs.com/kaisadadi/p/7641025.html