Spring配置SessionFactory

Spring的核心模块之一为依赖注入,普遍使用的为Set方式注入,那么下面将借助该方式配置数据源和SessionFactory

直接引用hibernate.cfg.xml配置文件

这种方式需要提供hibernate.cfg.xml配置文件,在其中构造sessionFactory,并且持久层类的映射也在改文件配置,这步操作和Spring没有任何关系。产生关系是在spring的配置文件中,通过set方式注入到org.springframework.orm.hibernate3.LocalSessionFactoryBean类中,其对应属性为:configLocation。配置如下:

<!-- 寻找hibernate.cfg.xml方式配置sessionfactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean>


使用数据源配置SessionFactory

1、使用jdbc数据源

<!-- 配置jdbc数据源 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="url" value="jdbc:mysql://localhost:3306/iess?useUnicode=true&characterEncoding=UTF8"/> </bean> -->


2、使用c3p0数据源

<!-- 配置c3p0数据源 --> <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/iess?useUnicode=true&characterEncoding=UTF8"/> <property name="maxIdleTime" value="25000"/> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">root</prop> <prop key="c3p0.min_size">10</prop> <prop key="c3p0.max_size">100</prop> <prop key="c3p0.timeout">1800</prop> <prop key="c3p0.idle_test_period">3600</prop> </props> </property> </bean>

3、结合数据源配置SessionFactory

<!-- 根据数据源创建sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/indigopacific/iessportal/persistent/Seal.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealModel.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/BranchInfo.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealData.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.fetch_size">30</prop> <prop key="hibernate.jdbc.batch_size">0</prop> <prop key="hibernate.max_fetch_depth">10</prop> </props> </property> </bean>

spring配置事务

spring配置事务借助其AOP核心模块,其包括如下步骤:

1、配置事务管理器

2、配置Pointcut,定义事务作用范围

3、配置Advice,定义事务传播范围

其配置如下:

<!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置哪些类开启事务 --> <aop:config> <aop:pointcut id="allMethod" expression="execution(* com.indigopacific.iessportal.Manager.*.*(..))"/> <aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice>


整个配置文件

<?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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 寻找hibernate.cfg.xml方式配置sessionfactory --> <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> --> <!-- 配置数据源方式获取sessionfactory --> <!-- 配置jdbc数据源 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="url" value="jdbc:mysql://localhost:3306/iess?useUnicode=true&characterEncoding=UTF8"/> </bean> --> <!-- 配置c3p0数据源 --> <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/iess?useUnicode=true&characterEncoding=UTF8"/> <property name="maxIdleTime" value="25000"/> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">root</prop> <prop key="c3p0.min_size">10</prop> <prop key="c3p0.max_size">100</prop> <prop key="c3p0.timeout">1800</prop> <prop key="c3p0.idle_test_period">3600</prop> </props> </property> </bean> <!-- 根据数据源创建sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/indigopacific/iessportal/persistent/Seal.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealModel.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/BranchInfo.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealData.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.fetch_size">30</prop> <prop key="hibernate.jdbc.batch_size">0</prop> <prop key="hibernate.max_fetch_depth">10</prop> </props> </property> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置哪些类开启事务 --> <aop:config> <aop:pointcut id="allMethod" expression="execution(* com.indigopacific.iessportal.Manager.*.*(..))"/> <aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> </beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring配置MyBatis可以通过以下步骤实现: 1. 添加MyBatis和Spring Boot MyBatis Starter的依赖项到项目的pom.xml文件中: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> ``` 2. 创建一个MyBatis的配置类,用于配置MyBatis的相关属性和设置: ```java @Configuration @MapperScan("com.example.mapper") // 指定Mapper接口所在的包 public class MyBatisConfig { @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); return sessionFactory.getObject(); } } ``` 3. 创建Mapper接口和对应的Mapper XML文件,用于定义数据库操作的SQL语句和映射关系。例如,创建一个UserMapper接口和对应的UserMapper.xml文件: ```java @Mapper public interface UserMapper { User getUserById(int id); void insertUser(User user); void updateUser(User user); void deleteUser(int id); } ``` ```xml <!-- UserMapper.xml --> <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" resultType="com.example.model.User"> SELECT * FROM users WHERE id = #{id} </select> <insert id="insertUser" parameterType="com.example.model.User"> INSERT INTO users (id, name, age) VALUES (#{id}, #{name}, #{age}) </insert> <update id="updateUser" parameterType="com.example.model.User"> UPDATE users SET name = #{name}, age = #{age} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM users WHERE id = #{id} </delete> </mapper> ``` 4. 在Spring Boot的配置文件application.properties或application.yml中配置数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 5. 在需要使用Mapper的地方,通过@Autowired注解注入Mapper,并调用相应的方法进行数据库操作: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(int id) { return userMapper.getUserById(id); } public void insertUser(User user) { userMapper.insertUser(user); } public void updateUser(User user) { userMapper.updateUser(user); } public void deleteUser(int id) { userMapper.deleteUser(id); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值