此篇在上一篇博客的基礎上進行修改,實現註解開發,所需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;
- }
- }
- 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;
- }
- }