SSH环境搭建(一)使用注解整合SSH

spring配置文件: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:aop="http://www.springframework.org/schema/aop"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">
   <!--      
    配置service     
   <bean id="customerService" class="com.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
   </bean>
   配置Dao
   <bean id="customerDao" class="com.dao.impl.CustomerDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
   </bean> -->
   <!-- 配置spring容器创建时要扫描的包 -->
    <context:component-scan base-package="com"></context:component-scan>
   <!-- 配置hibernateTemplate -->
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <!-- spring接管sessionFactory的创建 
        用spring提供的提高sessionFactory:LocalSessionFactoryBean
        创建sessionFactory有三部分信息
            就是hibernate配置文件的信息      
   -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入hibernate配置文件 -->
        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 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>
                <!-- create:根据映射关系生成表 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
        <!--  
            使用注解后,不再使用该种方式
        <property name="mappingLocations">
            <array>
                <value>classpath:com/entity/*.hbm.xml</value>
            </array>
         </property> -->
         <!-- 使用注解后,用该方式指定实体类的包 -->
         <property name="packagesToScan">
            <array>
                <value>com.entity</value>
            </array>
         </property>
   </bean>
    <!-- 数据源,与数据库连接 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 数据库驱动 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <!-- 数据库连接地址 -->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh?characterEncoding=utf8"/>
        <!-- 用户名 -->
        <property name="user" value="root"/>
        <!-- 密码 -->
        <property name="password" value="123456"/>
        <!-- 最小连接数 -->
        <property name="minPoolSize" value="3"/>
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="10"/>
        <!-- 初始化连接数 -->
        <property name="initialPoolSize" value="5"/>
    </bean>


   <!-- 配置事务管理器 -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>

   <!-- 开启spring对注解事务的支持 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
   <!-- 
   配置事务的通知
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
   </tx:advice>
   配置AOP
   <aop:config>
        配置切入点表达式
        <aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="pt1"/>
        建立切入点表达式和事务通知的关联
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
   </aop:config> -->
</beans>

业务层实现类:CustomerServiceImpl

/*
 * 业务层实现类
 */
@Service("CustomerService")
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public class CustomerServiceImpl implements CustomerService {
    @Resource(name="customerDao")
    CustomerDao customerDao;

    @Override
    @Transactional(readOnly=false,propagation=Propagation.SUPPORTS)
    public List<Customer> findAll() {
    customerDao.findAll();
    return null;
    }

    @Override
    public void save(Customer customer) {
    customerDao.save(customer);
    }

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

}

持久层实现类:CustomerDaoImpl

@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
    //spring提供的类似于session的对象
    @Resource(name="hibernateTemplate")
    private HibernateTemplate hibernateTemplate;

    @Override
    public List<Customer> findAll() {

    return (List<Customer>) hibernateTemplate.find("from Customer");
    }

    @Override
    public void save(Customer customer) {
    hibernateTemplate.save(customer);
    }  

}

实体类:Customer

@Entity
@Table(name="Customer")
public class Customer {
    @Id
    @Column(name="custId")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int custId;
    @Column(name="custName")
    private String custName; 
    @Column(name="custSource")
    private String custSource;
    @Column(name="custIndustry")
    private String custIndustry;  
    @Column(name="custLevel")
    private String custLevel;  
    @Column(name="custAddress")
    private String custAddress;  
    @Column(name="custPhone")
    private String custPhone;
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public String getCustSource() {
        return custSource;
    }
    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }
    public String getCustIndustry() {
        return custIndustry;
    }
    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }
    public String getCustLevel() {
        return custLevel;
    }
    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }
    public String getCustAddress() {
        return custAddress;
    }
    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
    @Override
    public String toString() {
    return "Customer [custid=" + custId + ", custName=" + custName
        + ", custSource=" + custSource + ", custIndustry="
        + custIndustry + ", custLevel=" + custLevel + ", custAddress="
        + custAddress + ", custPhone=" + custPhone + "]";
    }
    public int getCustId() {
        return custId;
    }
    public void setCustId(int custId) {
        this.custId = custId;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值