这个项目就是一个例子,只有添加图书的功能:
项目架构:
resource:
整合流程:
1.pom文件节点,这两个是整合用的,其他节点不再赘述:
1 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> 2 <dependency> 3 <groupId>org.mybatis</groupId> 4 <artifactId>mybatis-spring</artifactId> 5 <version>1.2.0</version> 6 </dependency> 7 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 8 <dependency> 9 <groupId>org.mybatis</groupId> 10 <artifactId>mybatis</artifactId> 11 <version>3.2.2</version> 12 </dependency>
如果mybatis用的是3.4.1,那么mybatis-spring版本要用1.3.0
2.applicationContextDay06.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 13 "> 14 <!--01.识别jdbc.properties文件--> 15 <context:property-placeholder location="classpath:jdbc.properties"/> 16 <!--02.数据源--> 17 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 18 <property name="driverClassName" value="${jdbc.driver}"/> 19 <property name="url" value="${jdbc.url}"/> 20 <property name="username" value="${jdbc.username}"/> 21 <property name="password" value="${jdbc.password}"/> 22 <!--<property name="maxActive" value="20"/>--> 23 </bean> 24 <!--03.工厂配置 datasource+mybatis-config--> 25 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml"/> 26 <!--04.Dao层Mapper注入--> 27 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="cn.happy.day06zhenghe.dao"/> 28 <!--05.service--> 29 <bean id="bookService" class="cn.happy.day06zhenghe.service.impl.BookServiceImpl" p:bookDAO-ref="IBookDAO"/> 30 <!--06.transactionManager--> 31 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> 32 <!--07.AspectJ AOP 配置事务--> 33 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 34 <tx:attributes> 35 <tx:method name="addBook" isolation="DEFAULT" propagation="REQUIRED"/> 36 </tx:attributes> 37 </tx:advice> 38 <aop:config> 39 <!--切点--> 40 <aop:pointcut id="pointcut" expression="execution(* *..day06zhenghe.service.*.*(..))"/> 41 <!--顾问--> 42 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> 43 </aop:config> 44 </beans>
3.jdbc.properties,这里把四个串配上就行了。
4.mybatis,不用配置environments和mappers了,可以setting和alias,也可以直接干掉这个文件。
5.测试类:
1 public class Test20171021 { 2 @Test 3 public void test01(){ 4 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextDay06.xml"); 5 IBookService bookService = (IBookService)ctx.getBean("bookService"); 6 Book book = new Book(); 7 book.setBookname("放学后"); 8 book.setBookprice((double) 20); 9 bookService.addBook(book); 10 } 11 }