1,创建applicationContext.xml并导入头文件
<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:aop="http://www.springframework.org/schema/aop" 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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<beans>
2,扫描service的注解
<!-- 扫描service接口,将service纳入spring容器 -->
<context:component-scan base-package="com.sxt.service.impl"></context:component-scan>
3,配置数据源【这里说三种配置方法】
3.1spring自带的
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ssm" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
3.2c3p0的【注意导c3p0的两个jar包】
<!-- 使用C3P0的配置方法 但是要导入c3p0的jar包 c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${database.driverClassName}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="user" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
3.3dbcp的【注意导dbcp的两个jar包】
<!-- 使用dbcp的配置方法 但是要导dbcp的jar包 commons-dbcp.jar commons-pool-1.6.jar -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="1111"></property>
</bean>
4,实例化SqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis.cfg.xml" />
<!--扫描 XXXmapper.xml映射文件,配置扫描的路径 这个不配置也可以,但是不配置的话,下面dao和xxxMapper.xml必须放在同一个包下面 -->
<property name="mapperLocations" value="classpath:com/sxt/mapping/*.xml"></property>
</bean>
5,配置dao包的查找
<!-- DAO接口所在包名,Spring会自动查找之中的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sxt.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
6,第4点和第五点还可以这样配置【不推荐】
注意,这样配置就没有mybatis.cfg.xml了,当然,想要也可以加进来,还有就是XxxMapper.java和XxxMapper.xml的名字必须相同
<!-- 实例化SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 给SqlSessionFactory 对象注入 DataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 注入分页插件,拦截器 -->
<property name="plugins">
<list>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<!-- 底层数据库的方言 -->
<prop key="dialect">mysql</prop>
<!-- 分页最大页,最小页判断 -->
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</list>
</property>
</bean>
<!-- 通过MapperScannerConfigurer扫描,产生Mapper接口的代理对象
产生的Mapper接口的代理对象,在spring容器的id为接受名字,首字母小写 mapper.java 和 mapper.xml必须名字相同,在同一个目录 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定需要扫的package -->
<property name="basePackage" value="com.bjsxt.mapper"></property>
<!-- 注入SqlSessionFactory对象 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
7,声明式事务配置
<!-- 1 实例化事务管理器对象 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2 声明事务切面 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 进行aop的配置 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* com.sxt.service.impl.*.*(..))"id="pc" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
</aop:config>
版权声明:本文为LHH201016原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。