spring4 学习4 spring MVC+mybatis+Mysql

在前面搭建的基础上,引入新的jar包如下:

aopalliance-1.0.jar
aspectjweaver-1.8.8.jar
mybatis-3.3.0.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.31.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-jdbc-4.2.4.RELEASE.jar
spring-orm-4.2.4.RELEASE.jar
spring-oxm-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar

代码结构如下:下载地址 

spring4 学习4 spring MVC+mybatis+Mysql_第1张图片

 

localConfig.properties

Java代码

  1. #datasource properties  

  2. jdbc.url=jdbc:mysql://localhost:3306/world  

  3. jdbc.username=root  

  4. jdbc.password=root 

spring-dataSource.xml

 

Java代码

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

  4.     xmlns:tx="http://www.springframework.org/schema/tx"  

  5.     xmlns:aop="http://www.springframework.org/schema/aop"  

  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   

  7.     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  

  8.     http://www.springframework.org/schema/aop   

  9.     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd    

  10.     http://www.springframework.org/schema/tx   

  11.     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">  

  12.   

  13.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  

  14.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  

  15.         <property name="url" value="${jdbc.url}"></property>  

  16.         <property name="username" value="${jdbc.username}"></property>  

  17.         <property name="password" value="${jdbc.password}"></property>  

  18.     </bean>  

  19.       

  20.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

  21.         <property name="dataSource" ref="dataSource" />  

  22.     </bean>  

  23.   

  24.     <bean id="transactionManager"  

  25.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

  26.         <property name="dataSource" ref="dataSource" />  

  27.     </bean>  

  28.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  

  29.         <tx:attributes>  

  30.             <tx:method name="*" />  

  31.         </tx:attributes>  

  32.     </tx:advice>  

  33.     <aop:config>  

  34.         <aop:pointcut expression="execution(* com.xx.demo.bsh.*.*.*(..))"  

  35.             id="myPointcut" />  

  36.         <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />  

  37.     </aop:config>  

  38. </beans>  

  配置玩事务后先检查一下mysql中的表的存储引擎是否是innoDB。若是MyISAM,要改成InnoDB,因为MyISAM是事务不安全的。下载地址 

          查看命令:show create table city;

          修改命令:alter table city engine = InnoDB;

下载地址 

你可能感兴趣的:(spring4)