基于spring+springMVC+mybaitis的maven项目搭建

首先创建一个maven项目选择web类型的创建成功后,首先在pom文件中引入架包,再进行配置文件的配置

创建applicationContext.xml(spring)文件以及springmvc-servlet.xml(springmvc)和SqlMapConfig.xml在对其进行如下配置

根据个人要求进行配置

首先我是applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	
	<!--扫描  -->
	<context:component-scan base-package="com.crabverfication"/>
	
	<!--加载Java配置文件  -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!--配置数据源  -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${db.driver}"></property>
		<property name="jdbcUrl" value="${db.url}"/>
		<property name="user" value="${db.username}"/>
		<property name="password" value="${db.password}"/>
		<property name="acquireIncrement" value="10"/>
		<property name="initialPoolSize" value="5"/>
	</bean>
	
	<!-- 4. SessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- 整合mybatis,包扫描 mapper文件 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
		<property name="mapperLocations" value="classpath:com/crabverfication/mapper/*.xml"/>
	</bean>

	<!-- JDBC事务管理 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 启用支持annotation注解方式事物管理 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
没错使用注解开发配置文件就是这么简洁

springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-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/context 
	http://www.springframework.org/schema/context/spring-context-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/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
	
	<!-- 1自动扫描包,SpringMVC 会将包下用了@Controller注解的类注册为spring的Controller -->
	<context:component-scan base-package="com.crabverfication.controller"/>
	<!--设置默认配置方案  -->
	<mvc:annotation-driven/>
	
	<!-- 2. 内部资源视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	<!--文件上传解析器  -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="104857600"></property>
		<property name="defaultEncoding" value="UTF-8"></property>
	</bean>
</beans>
springMVC的配置很简单都是使用注解关于那些适配器使用注解驱动这些就可以了  默认包含在内的

下面是mybatis的配置信息

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
	    <!-- com.github.pagehelper为PageHelper类所在包名 -->
	    <plugin interceptor="com.github.pagehelper.PageHelper">
	        <!-- 4.0.0以后版本可以不设置该参数 -->
	        <property name="dialect" value="mysql"/>
	        <!-- 该参数默认为false -->
	        <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
	        <!-- 和startPage中的pageNum效果一样-->
	        <property name="offsetAsPageNum" value="true"/>
	        <!-- 该参数默认为false -->
	        <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
	        <property name="rowBoundsWithCount" value="true"/>
	        <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
	        <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
	        <property name="pageSizeZero" value="true"/>
	        <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
	        <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
	        <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
	        <property name="reasonable" value="true"/>
	        <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
	        <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
	        <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->
	        <!-- 不理解该含义的前提下,不要随便复制该配置 -->
	        <property name="params" value="pageNum=start;pageSize=limit;"/>
	        <!-- 支持通过Mapper接口参数来传递分页参数 -->
	        <property name="supportMethodsArguments" value="true"/>
	        <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
	        <property name="returnPageInfo" value="check"/>
	    </plugin>
	</plugins>

</configuration>
由于个人使用pagehelper插件所以有这些文件,其实该文件里面什么都没有。

这些文件配置好后我们需要对web.xml文件进行配置具体配置也很简单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值