
1、applicationContext.xml(有时包版本不匹配,就把包版本后缀去掉,例如:
http://www.springframework.org/schema/context/spring-context-4.3.xsd
变成http://www.springframework.org/schema/context/spring-context.xsd
把"-4.3"去掉
)
<?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: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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--spring 和Mybatis 的整合-->
<!--1、配置数据库相关参数-->
<context:property-placeholder location="classpath:db.properties"/>
<!--2、配置数据源,这里注意我这value值在db.properties中配置,也可以直接配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>
<!--3、配置sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--扫描bean层包 使用别名,扫描可别出错,我这扫的是根目录下的bean-->
<property name="typeAliasesPackage" value="com.gm.bean"/>
<!--配合加载映射文件 *mapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--分页配置-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect"></prop>
<prop key="reasonable"></prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--自动生成dao,mapper-->
<!--自动生成dao接口包,将dao策藏注入spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置给出需要扫描的dao层路径,扫描可别出错,我这扫的是根目录下的dao-->
<property name="basePackage" value="com.gm.dao"/>
<!--注入Spring容器中-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--自动扫描全部包,扫描可别出错,我这扫的是根目录下的所有包-->
<context:component-scan base-package="com.gm"/>
<!--配置事务注解-->
<bean id="transctionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven></tx:annotation-driven>
</beans>
此处蓝色为根目录

2、db.properties
//使用Driver驱动
jdbc.driver=com.mysql.jdbc.Driver
//其中“db_gm”是我mysql中的数据库,可更换
jdbc.url=jdbc:mysql://localhost:3306/db_gm?useSSL=true&characterEncoding=utf-8
//mysql账号
jdbc.username=
//mysql密码
jdbc.password=
3、log4j.properties定义一些标准,照抄不用改
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4、spring-mvc.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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--扫描位置注解,扫描根目录下controller层-->
<context:component-scan base-package="com.gm.controller"/>
<!--配置映射处理器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--3、视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>//前端页面文件后缀,我的都是.jsp,例如:index.jsp
<property name="prefix" value="/pages/"/>//webapp中pages包中的jsp文件
</bean>
</beans>

2304

被折叠的 条评论
为什么被折叠?



