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;
} }
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文件
ssh注解开发的更多相关文章
- ssh+注解开发 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- JAVAEE——SSH项目实战06:统计信息管理、Spring注解开发和EasyUI
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7190925.html 一.统计信息管理 二.Spring注解开发 1.service ...
- Spring笔记13--SSH--全注解开发
SSH全注解开发: (1) 在Action类中添加注解,实现Struts2的注解开发(@NameSpace.@ParentPackage.@Action...) package com.tongji. ...
- SSH整合开发时Scope为默认时现象与原理
1.前提知识 1)scope默认值 进行SSH整合开发时,Struts2的action须要用spring容器进行管理,仅仅要涉及到类以bean的形式入到spring容器中.无论是xml配置还是使用注解 ...
- SpringMVC注解开发初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC的注解开发入门
1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...
- 基于ssh框架开发的购物系统的质量属性
根据前面的博客,我们已经大致了解了ssh架构开发整体概念:Struts是一个实现了MVC模式的经典的框架:Hibernate是轻量级Java EE应用的持久层解决方案,以面向对象的方式提供了持久化类到 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- ssh注解整合
ssh注解整合 导入java包 配置struts2环境 1. 创建struts.xml配置文件 <?xml version="1.0" encoding="UTF- ...
随机推荐
- CSS3重要内容翻译
以上是废话 1.3 此处未完全确认,相较于css3和css3的选择器,区别包括: 基础定义改变(选择器.选择器组,简单选择器等),特别的,作为css2中简单选择器,如今被成为简单选择器序列,“简 ...
- 用Guava辅助Throwable异常处理
Guava的 Throwables 工具常常可以让exception处理更方便. Propagation 有时候,你会想把捕获的exception抛到上一个try/catch块.对于 RuntimeE ...
- java面试题及答案(基础题122道,代码题19道)
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
- js 得到当前季度
Date.prototype.getQuarter = function() { var month = this.getMonth(); if(month < 3) { return '第一季 ...
- 集合及特殊集合arrayList
1,运用集合 arrayList 首先复制Colections加 : 创建arrayList ar =new arrayList(); ArrayList具体提供的功能:属性 ...
- SQLSERVER一个比较不错的分页存储过程p_splitpage
CREATE procedure p_splitpage @sql nvarchar(4000), --要执行的sql语句 @page int=1, --要显示的页码 @pageSize int, - ...
- FLASK初步实践
感觉经过DJANGO,CI,RAILS之类的WEB框架之后,FLASK的思路就比较顺畅了... FLASKR.PY import sqlite3 from flask import Flask, re ...
- SSS小记
好吧 最终的normal加上去了 不过加在local 上 效果什么的比我预期的好一点 . 还有一点opengl Crack 的原因: 各种program 忘记init 造孽: 还有潜在的memory ...
- VMware 11安装Mac OS X 10.10
http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html
- Java ClassLoader基础及加载不同依赖 Jar 中的公共类(转)
http://www.iteye.com/topic/1135259 http://www.trinea.cn/android/java-loader-common-class/ http://www ...