SpringMVC+Mybatis整合注解
2016-04-28 13:13
429 查看
Spring.xml配置 <?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <mvc:annotation-driven/> <!--引入属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- service和dao包(自动注入) --> <context:component-scan base-package="com.meng.service,com.meng.dao"/> </beans>
SpringMVC.xml配置 <?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <mvc:annotation-driven /> <!-- 支持spring3.0新的mvc注解 --> <context:component-scan base-package="com.meng"/> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动Spring MVC 的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <!-- json转换器 --> </list> </property> </bean> <!-- 页面View层基本信息设定 --> <bean id="viewResolver"p:prefix="/WEB-INF/jsp/"p:suffix=".jsp"class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean> <bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding"value="UTF-8"/> <property name="maxUploadSize"value="32505856"/><!-- 上传文件大小限制为31M,31*1024*1024 --> <property name="maxInMemorySize"value="4096"/> </bean> </beans>
Spring-Mybatis.xml配置 <?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!--开启注解支持 --> <mvc:annotation-driven/> <!--创建jdbc数据源 --> <bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"value="${driver}"/> <property name="url"value="${url}"/> <property name="username"value="${username}"/> <property name="password"value="${password}"/> <!-- 初始化连接大小 --> <property name="initialSize"value="0"/> <!-- 连接池最大使用连接数量 --> <property name="maxActive"value="20"/> <!-- 连接池最大空闲 --> <property name="maxIdle"value="20"/> <!-- 连接池最小空闲 --> <property name="minIdle"value="0"/> <!-- 获取连接最大等待时间 --> <property name="maxWait"value="60000"/> </bean> <!-- mybatis文件 --> <bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource"ref="dataSource"/> <!-- 自动扫描entity目录,省略Configuration.xml里手工配置 --> <property name="mapperLocations"value="classpath:com/meng/po/*.xml"/> </bean> <bean id="sqlSession"class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0"ref="sqlSessionFactory"/> </bean> <!-- 配置事务管理器 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"ref="dataSource"/> </bean> <!-- 注解方式配置事物 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice"transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*"propagation="REQUIRED"/> <tx:method name="update*"propagation="REQUIRED"/> <tx:method name="insert*"propagation="REQUIRED"/> <tx:method name="delete*"propagation="REQUIRED"/> <tx:method name="find*"propagation="REQUIRED"read-only="true"/> <tx:method name="select*"propagation="REQUIRED"read-only="true"/> <tx:method name="search*"propagation="REQUIRED"read-only="true"/> <tx:method name="datagrid*"propagation="REQUIRED" read-only="true"/> <tx:method name="*"propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <!--定义在service包和所有子包里的任意类的任意方法的执行 --> <aop:pointcut id="transactionPointcut" expression="execution(* com.meng.service..*Impl.*(..))"/> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/> </aop:config> </beans>
运行环境:windows7+myeclipse 8.5+Tomcat 6.x
数据库:mysql
DEMO经过测试运行没问题,功能比较简单,只是进行整合练习,哪些地方不好大家可以提提意见
下载链接:http://pan.baidu.com/s/1mgkLABI
http://www.thinksaas.cn/topics/0/307/307579.html
相关文章推荐
- stringBuffer、StringBuilder、排序、Arrays、Jdk1.5新特性(java基础知识十三)
- Spring MVC 通过@Value注解读取.properties配置内容
- 解决SpringMVC传输json数据时的乱码
- Java---IO加强(3)-IO流的操作规律
- Java---IO加强(3)-IO流的操作规律
- spring mvc 无法扫描jar中controller和Service
- java keylistener 问题
- jar打包.class文件
- Scanner、String(java基础知识十二)
- 初学java集合-map—API
- 一个有意思的 Java HashSet 问题
- java中关于容器的描述
- Eclipse断点调试
- [1.1]第一个Spark应用程序之Java & Scala版 Word Count
- java学习第一步 sublime环境配置
- office转pdf(在线预览)思路(java示例)
- Spring入门之bean的属性
- java GC机制的一些总结
- Java报文或者同步的数据有个别乱码情况的处理.
- eclipse远程连接hbase错误