此篇在上一篇博客的基础上进行修改,实现注解开发,所需pom节点详情,还请参考上一篇博客点击打开链接
使用注解方式来开发,需要做的事如下:
先来看一下我们的注解版分层的结果目录:
从图中可以看到pojo层发生了改变,少了一个配置文件。当然,这只是表面上的,实际上需要改动的地方还是有好几处的。
我们先从实体层开始切入:
Rs:
package cn.jjz.pojo;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
* Created by lenovo on 2017/9/22.
*/
@Entity
@Table(name = "Rs")
public class Rs {
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid",strategy="uuid")
private String id;
@Column
private String name;
@Column
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我们再来看看dao层中的实现类:
package cn.jjz.dao;
import cn.jjz.pojo.Rs;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
/**
* Created by lenovo on 2017/10/29.
*/
@Repository("RsDao")
public class RsDaoImpl implements RsDao {
@Resource
private SessionFactory sessionFactory;
public void add(Rs rs) {
sessionFactory.getCurrentSession().save(rs);
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
可以发现,添加上了注解
再看看service层中的实现类:
package cn.jjz.service;
import cn.jjz.dao.RsDaoImpl;
import cn.jjz.pojo.Rs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* Created by lenovo on 2017/10/29.
*/
@Service("RsService")
public class RsServiceImpl implements RsService {
@Resource(name = "RsDao")
private RsDaoImpl rsDao;
@Transactional
public void add(Rs rs) {
rsDao.add(rs);
}
public void setRsDao(RsDaoImpl rsDao) {
this.rsDao = rsDao;
}
public RsDaoImpl getRsDao() {
return rsDao;
}
}
也是加上了注解
我们再看看applicationContext.xml中的配置,有哪些改动:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<context:component-scan base-package="cn.jjz"></context:component-scan>
<!--1.配置数据源c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<!--jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 配置session工厂 类:Local-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="cn.jjz.pojo"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!--<property name="mappingDirectoryLocations" value="classpath:cn/jjz/pojo"></property>-->
</bean>
<!-- 事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
非注解applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!--1.配置数据源c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<!--jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 配置session工厂 类:Local-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<property name="mappingDirectoryLocations" value="classpath:cn/jjz/pojo"></property>
</bean>
<!--3.dao-->
<bean id="rsDao" class="cn.jjz.dao.RsDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--4.service-->
<bean id="rsService" class="cn.jjz.service.RsServiceImpl">
<property name="rsDao" ref="rsDao"></property>
</bean>
<!--要用Spring去创建Action对象 Struts2的Action是多例的-->
<bean id="rsAction" class="cn.jjz.action.RsAction" scope="prototype">
<property name="rsService" ref="rsService"></property>
</bean>
<!-- 事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
再看看struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="one" namespace="/" extends="struts-default">
<action name="add" class="cn.jjz.action.RsAction" method="add">
<result name="success">/ok.jsp</result>
</action>
</package>
</struts>
最后看看需要改动的地方:
RsAction:
package cn.jjz.action;
import cn.jjz.pojo.Rs;
import cn.jjz.service.RsService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.core.StandardReflectionParameterNameDiscoverer;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.persistence.Column;
import java.util.List;
/**
* Created by lenovo on 2017/10/29.
*/
@Controller
public class RsAction extends ActionSupport {
private Rs rs;
@Resource(name = "RsService")
private RsService rsService;
@Action(value = "add",results = {@Result(name = "success",location = "/ok.jsp")})
public String add(){
rsService.add(rs);
System.out.println(rs.getName());
return SUCCESS;
}
public Rs getRs() {
return rs;
}
public void setRs(Rs rs) {
this.rs = rs;
}
public RsService getRsService() {
return rsService;
}
public void setRsService(RsService rsService) {
this.rsService = rsService;
}
}