SpringMVC+Mybatis整合注解

本文档详细介绍了Spring、SpringMVC及MyBatis三种框架的XML配置方法,包括数据源配置、视图解析器设置、事务管理等关键步骤,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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
/***********************基本描述**********************************/ 0、根据表可以单独生成javaBean后缀可以自定义 1、工具本身是非常简单的,每个人都能做就是使用模板替换生成相应文件 2、工具主要针对SpringMvc+Mybatis注解+Mysql生成对象,dao、sqlDao、interface、实现接口 3、根据表生成Excel 4、生成成功后倒入到自己对应的项目中,然后Ctrl+Shipt+O(Eclipse快速倒入包)实现 5、里面因为运用的是注解,所以很多包我就没有提供了因为这些都是很基础的东西,不会的同学可以去网上查看搭建Mybatis注解 6、生成了些什么,具体主要是对单表的增、删、改、查(分页) /********************************/ /********************************/ /*************完全免费***********/ /********************************/ /********************************/ 如果大家喜欢可以再给我提其他功能,有时间我加上 /*********************************************************************************/ 模板介绍: MySql.Data.dll :连接Mysql基本dl我们的的驱动。 foxjava.exe :直接运行程序 xml : Excel文件夹 ##### TemplateXml.xml 根据数据库对应表生成字段描述,生成后最好用WPS打开,然后重新另存为office认识的Excel template : 文件生成模板(非常重要的不能修改) ##### BasePojo.template 所有基础表对象都要继承,方便序列化(系统自动生成) ##### Pager.template 分页对象 (系统自动生成) ##### dao.template 数据库接口Dao(mybatis接口方式,在方法上写sql,复杂的使用sqlProvider) ##### daoSqlProvider.template 复杂sql提供者 ##### service.template 对外开放的接口 ##### serviceImpl.template 实现开放接口,基本数据操作逻辑 /*********************************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值