引入需要的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. 【USACO 3.2.6】香甜的黄油

    [描述] 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  2. NetBeans无法使用编码GBK安全地打开该文件 解决方法

    正常安装的NetBeans在打开UTF-8编码的文件时,会提示“NetBeans无法使用编码GBK安全地打开该文件”,点击“是”强制打开后,中文会变成乱码. 上述问题可以通过如下方式解决: 用文本编辑 ...

  3. html a标签

    <a> 标签定义超链接,用于从一张页面链接到另一张页面. <a> 元素最重要的属性是 href 属性,它指示链接的目标. 在所有浏览器中,链接的默认外观是: 未被访问的链接带有 ...

  4. mysql主从复制 (超简单) 转载

    怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作:   1.1.版本一致   1.2.初始化表,并在后台启动mysql   1.3.修改root的密码 ...

  5. powerpoint2015如何调整PPT立体图形

    在powerpoint2016中,可以添加图片来丰富演说,但通常人们只会懂得插入平面图片.动态图片,立体图片就无法插入了吗?不是的,在powerpoint2016为我们准备了大量的预设,可以调整图片, ...

  6. javaWeb中一个按钮提交两个表单

    一个按钮提交两个表单,有时候会用到,一般会很容易想到使用 onclick="document.form1.submit();document.form2.submit();" 的方 ...

  7. 判断input checkbox选中状态

    $("#IsAdmin").is(':checked') 判断收否选中 返回true 或者false

  8. JavaBean与EJB的区别与应用

    JavaBean 是一种组件,它在内部有接口或有与其相关的属性,以便不同人在不同时间开发的 bean 可以询问和集成. EJB 是部署在服务器上的可执行组件或商业对象.有一个协议允许对其进行远程访问或 ...

  9. 转:ElasticSearch的安装和相关插件的安装

    原文来自于:http://blog.csdn.net/whxaing2011/article/details/18237733 本文主要介绍如下内容:          1.ElasticSearch ...

  10. swift material

    import UIKit extension UIColor { convenience init(hex:Int, alpha:CGFloat=1.0) { let red = CGFloat((h ...