引入需要的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. TalkingData游戏版本在Cocos2d-x 3.0使用

    Cocos2dx在3.0的版本中改动确实不少啊,所以导致原来可以在Cocos2.x版本上的demo都不能直接用,所以不得不重要写一个新的demo 但是TalkingData的库一直都是可以用的,只是之 ...

  2. CentOS6.5安装LAMP环境的前期准备

    首先需要按照前一篇<CentOS 6.5下安装MySql 5.7>的安装步骤配置好防火墙.关闭 SELINUX 1.编译安装libxml2注:libxml2是一个xml的c语言版的解析器, ...

  3. svn更新

    下载配置文件 pwd cd /home/www/xxxx/protected/config/ get main.php 上传配置文件 put main.php svn更新 svn co svn://s ...

  4. C 编译器错误信息中文翻译

    Ambiguous operators need parentheses 不 明确的运算需要用括号括起 Ambiguous symbol ``xxx`` 不明确的符号 Argument list sy ...

  5. COJ 0016 20603矩阵链乘

    传送门:http://oj.cnuschool.org.cn/oj/home/solution.htm?solutionID=35454 20603矩阵链乘 难度级别:B: 运行时间限制:1000ms ...

  6. HDU Sky数 2079 简单易懂的代码

    题目 http://acm.hdu.edu.cn/showproblem.php?pid=2097 思路 既然要求和 十进制数字各个位数上的和是相同的, 那么16,12进制转换完之后也是10进制表示的 ...

  7. Notepad++去除代码行号的几种方法

    Notepad++去除代码行号的几种方法 (转自:http://hi.baidu.com/beer_zh/item/e70119309ee587f2a8842892)问:在网页中复制代码时,常常遇到高 ...

  8. 【最大流】XMU 1595 机器调度

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1595 题目大意: T组数据,n个任务,m个机器,对于每个任务:有一个处理时间p(表示这 ...

  9. ASPNET登陆总结

    昨天晚上看了视频,今天早上起来就凭着记忆与视频里的代码试着做了一个登陆,基本功能是实现了. 0x0:首先,第一步是做一个界面....直接扒别人做好的页面.....各种改改路径啥的,用浏览器打开,恩,发 ...

  10. Java习惯用法总结

    在微博中看到的一个不错的帖子,总结的很详细,拷贝过来,一是为了方便自己查阅,也能和大家一起共享,后面有原文的链接地址: 在Java编程中,有些知识 并不能仅通过语言规范或者标准API文档就能学到的.在 ...