引入需要的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;
} }

Teacher实体类

public interface TeacherDao {
//新增
void addTeacher(Teacher teacher); //删除
void deleteTeacher(Teacher teacher);
//修改
void updateTeacher(Teacher teacher);
//查询
List<Teacher> findTeachers();
//根据ID查询指定的teacher
Teacher findById(Integer id); }

TeacherDao

@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;
} }

TeacherDaoImpl

public interface TeacherService {

    // 新增
void addTeacher(Teacher teacher); // 删除
void deleteTeacher(Teacher teacher); // 修改
void updateTeacher(Teacher teacher); // 查询
List<Teacher> findTeachers(); //根据ID查询指定的teacher
Teacher findById(Integer id);
}

TeacherService

@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;
} }

TeacherServiceImpl

@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;
} }

AddAction

<?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>

applicationContext.xml文件

<?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>

web.xml文件

jdbc.properties文件自己定义即可

ssh注解开发的更多相关文章

  1. ssh+注解开发 pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  2. JAVAEE——SSH项目实战06:统计信息管理、Spring注解开发和EasyUI

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7190925.html 一.统计信息管理   二.Spring注解开发 1.service ...

  3. Spring笔记13--SSH--全注解开发

    SSH全注解开发: (1) 在Action类中添加注解,实现Struts2的注解开发(@NameSpace.@ParentPackage.@Action...) package com.tongji. ...

  4. SSH整合开发时Scope为默认时现象与原理

    1.前提知识 1)scope默认值 进行SSH整合开发时,Struts2的action须要用spring容器进行管理,仅仅要涉及到类以bean的形式入到spring容器中.无论是xml配置还是使用注解 ...

  5. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  6. SpringMVC的注解开发入门

    1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...

  7. 基于ssh框架开发的购物系统的质量属性

    根据前面的博客,我们已经大致了解了ssh架构开发整体概念:Struts是一个实现了MVC模式的经典的框架:Hibernate是轻量级Java EE应用的持久层解决方案,以面向对象的方式提供了持久化类到 ...

  8. Struts2框架之-注解开发

    Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...

  9. ssh注解整合

    ssh注解整合 导入java包 配置struts2环境 1. 创建struts.xml配置文件 <?xml version="1.0" encoding="UTF- ...

随机推荐

  1. Xshell下漂亮的开发环境配置

    今天折腾了一天Xshell配置Linux命令行开发环境. 总结几点: 1.Xshell配色方案,这是我自己调的个人使用版,网上比较好的版本有Solarized Dark,可以下载到. [ColorFo ...

  2. (转)ligerUI 使用教程之Tip介绍与使用

    概述:   ligertip是ligerUI系列插件中的tooltip类插件,作用是弹一个浮动层,起提示作用   阅读本文要求具备jQuery的基本知识,不然文中的javascript代码不易理解 截 ...

  3. 关闭iOS的自动更新

    Safari打开网址https://oldcat.me/web/NOOTA9.mobileconfig,安装描述文件,就不会自动下载和提示更新最新的iOS了

  4. 超轻型响应jQuery旋转木马幻灯片插件anoSlide

    http://www.jqcool.net/demo/201409/jquery-anoslide/

  5. windows下Django 部署到Apache24的配置

    1.首先下载最新版Apachehttp://httpd.apache.org/download.cgi#apache24,目前官方以不提供windows msi安装包,下载好的直接解压至C盘即可,ap ...

  6. uva 580 危险的组合(排列组合)

    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Descript ...

  7. [Ioi2005]River

    设f[i][j][k]表示i上游最近的一个伐木场为j且在i所在的子树里共建了k个伐木场(不包含在i的)的最小运费和 设v为u的儿子,dist[u]为u到0号点的距离. 则当i>=j时 f[u][ ...

  8. 从Profile中窥探Unity的内存管理

    刨根问底U3D---从Profile中窥探Unity的内存管理 这篇文章包含哪些内容 这篇文章从Unity的Profile组件入手,来探讨一下Unity在开发环境和正式环境中的内存使用发面的一些区别, ...

  9. Develop a Packet Sniffer with libpcap

    Develop a Packet Sniffer with libpcap: http://vichargrave.com/develop-a-packet-sniffer-with-libpcap/

  10. 【HDOJ】4985 Little Pony and Permutation

    水题. #include <cstdio> #define MAXN 100005 int buf[MAXN], n; int main() { int i, j, k; while (s ...