前言
因为接触配置文件比较多,所以对于我来说更喜欢配置文件,把需要配置的内容全部整到一个文件中,对于注解就显得很陌生。《Java注解基本知识》
Annotation(注解)就是Java提供了一种元程序中的元素关联任何信息和着任何元数据(metadata)的途径和方法。Annotion(注解)是一个接口,程序可以通过反射来获取指定程序元素的Annotion对象,然后通过Annotion对象来获取注解里面的元数据。
Entity注解
//定义实体类
@Entity
//定义数据库中表名
@Table(name="cst_customer")
public class Customer implements Serializable {
//定义id主键字段名称
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="cust_id")
private Long custId;
@Column(name="cust_name")
private String custName;
@Column(name="cust_source")
private String custSource;
/*get set方法*/
}
Spring配置文件注解:将Hibernate的功能:连接数据库配置数据源接管过来。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
<!-- 配置spring运行要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 开启spring对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 连接数据库信息 -->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate基本配置 -->
<property name="hibernateProperties">
<props>
<!-- 数据库方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 是否显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 是否格式化sql语句 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 采用何种方式生成数据库表结构 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 把session绑定到当前线程上 -->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!-- 映射文件位置 -->
<property name="packagesToScan">
<array>
<value>com.itheima.domain</value>
</array>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day47_ee48_ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
Struts配置文件注解:原来配置在此的内容,现在分布在Action Service Dao层
<!-- 开启开发者模式 -->
<constant name="struts.devMode" value="true"></constant>
Action注解
/*
* 客户动作类
*/
@Controller("customerAction") //控制层value
@Scope("prototype") //多例
@ParentPackage("struts-default")//默认父包
@Namespace("/customer") //命名空间
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer();
@Autowired //对类成员变量、方法及构造函数进行标注,完成自动装配的工作,消除 set,get方法
private ICustomerService customerService;
/*
* 模型驱动
*/
@Override
public Customer getModel() {
return customer;
}
/*
* 获取添加客户页面
* value:action名称 results:配置返回结果集属性
* @Result:配置返回具体结果 name:return中的String对应的 type:路径跳转方式 location:跳转路径
*/
@Action(value="addUICustomer",results={
@Result(name="addUICustomer",type="dispatcher",location="/jsp/customer/add.jsp")
})
public String addUICustomer(){
return "addUICustomer";
}
}
Service层
/**
* 客户的业务层实现类
*/
@Service("customerService") //Service层
//配置事务,如果没有事务则开启一个事务
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public class CustomerServiceImpl implements ICustomerService {
/*与Action一致,调用dao层*/
}
Dao层
/**
* 客户的持久层实现类
*/
@Repository("customerDao")
public class CustomerDaoImpl implements ICustomerDao {
/*操作数据库*/
}
小结:开始觉得注解特别难配,也不愿意多写,后来发现其实注解也挺好配的,就是把我们原来的xml中写的,分散在对应的类中。本来没有打算写,后来发现虽然代码不多,但是需要自己理清思路,真的很重要。