1. 工程目录结构整理
注意在Idea中所有的配置文件都必须放在Resource包中!! 不然编译器无法编译配置文件(除了web.xml和springMvc-servlet.xml外)
2. 配置mavenPOM.xml文件
<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>nfm</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- spring版本号-->
<spring.version>4.3.2.RELEASE</spring.version>
<!-- mybatis版本号-->
<mybatis.version>3.2.4</mybatis.version>
<!-- log4j日志文件管理包版本-->
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.9</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework</groupId>-->
<!--<artifactId>spring-tx</artifactId>-->
<!--<version>${spring.version}</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework</groupId>-->
<!--<artifactId>spring-aop</artifactId>-->
<!--<version>${spring.version}</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springframe end -->
<!-- mybatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<!-- junit测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--阿里巴巴数据源包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.2</version>
</dependency>
<!-- json数据-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!--日志文件管理包-->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- oracle数据源-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<!--c3p0数据源-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
</dependencies>
</project>
3. 配置web.xml文件
<?xml version="1.0"encoding="UTF-8"?>
<web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--解决工程编码过滤器-->
<filter>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring开始-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置中央控制器SpringMVC配置-->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index</welcome-file>
</welcome-file-list>
</web-app>
4 . 配置springMvc_servlet.xml文件
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<mvc:default-servlet-handler/><!--静态资源处理一般选择这个方案-->
</beans>
5 .配置applicationContext.xml文件
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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
http://mybatis.org/schema/mybatis-springhttp://mybatis.org/schema/mybatis-spring.xsd">
<!--通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置 @aspectJ切面的bean创建代理,织入切面-->
<aop:aspectj-autoproxy/>
<!--自动扫描的包名-->
<context:component-scanbase-package="com.geok"use-default-filters="true"></context:component-scan>
<!--默认的注解映射的支持-->
<mvc:annotation-driven/>
<!--定义视图解析器(action中跳转的页面配置)-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--<property name="viewClass"value="org.springframework.web.servlet.view.JstlView"/>-->
<!--<property name="prefix" value="/"/>-->
<propertyname="suffix"value=".jsp"/>
</bean>
<!--引入我们的jdbc.properties-->
<beanid="property"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="locations">
<list>
<value>classpath:jdbc.properties</value><!-- classpath代表类路径,如src下面的文件-->
</list>
</property>
</bean>
<!--配置druid数据源-->
<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"
destroy-method="close">
<propertyname="driverClassName">
<value>${jdbc.driver}</value>
</property>
<propertyname="url">
<value>${jdbc.url}</value>
</property>
<propertyname="username">
<value>${jdbc.username}</value>
</property>
<propertyname="password">
<value>${jdbc.password}</value>
</property>
<!--连接池最大使用连接数-->
<propertyname="maxActive">
<value>20</value>
</property>
<!--初始化连接大小-->
<propertyname="initialSize">
<value>1</value>
</property>
<!--获取连接最大等待时间-->
<propertyname="maxWait">
<value>60000</value>
</property>
<!--连接池最大空闲-->
<propertyname="maxIdle">
<value>20</value>
</property>
<!--连接池最小空闲-->
<propertyname="minIdle">
<value>3</value>
</property>
<!--自动清除无用连接-->
<propertyname="removeAbandoned">
<value>true</value>
</property>
<!--清除无用连接的等待时间-->
<propertyname="removeAbandonedTimeout">
<value>180</value>
</property>
<!--连接属性-->
<propertyname="connectionProperties">
<value>characterEncoding=UTF-8</value>
</property>
</bean>
<!--定义jdbcTemplate用于批量操作-->
<beanid="searchJdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--配置数据源C3P0数据库连接-->
<!-- <bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!– 用户名–>
<property name="user" value="fda" />
<!– 用户密码–>
<property name="password" value="fda" />
<property name="driverClass"value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl"value="jdbc:oracle:thin:@192.168.161.201:1521:orcl" />
<!–连接池中保留的最大连接数。默认值: 15–>
<property name="maxPoolSize" value="20" />
<!– 连接池中保留的最小连接数,默认为:3–>
<property name="minPoolSize" value="2" />
<!– 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3–>
<property name="initialPoolSize" value="2" />
<!–最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 –>
<property name="maxIdleTime" value="60"/>
</bean>-->
<!-- mybatis文件配置,扫描所有mapper文件-->
<!-- 配置SqlSessionFactoryBean -->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="classpath:mybatis.xml"/>
<!-- mapper和resultmap配置路径-->
<propertyname="mapperLocations">
<list>
<value>classpath:com/geok/mapperConfig/*.xml</value>
</list>
</property>
</bean>
<!--定义SqlSessionTemplate -->
<beanid="sqlSessionTemplate"class="org.mybatis.spring.SqlSessionTemplate">
<constructor-argname="sqlSessionFactory"ref="sqlSessionFactory"></constructor-arg>
</bean>
<mybatis:scanbase-package="com.geok.mapper"/>
<!--自动扫描mapper接口,注入sqlsessionfactory-->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.geok.mapper"/>
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</bean>
<!--开启mybatis事务-->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<tx:adviceid="transactionAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="create*"propagation="REQUIRED"/>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="modify*"propagation="REQUIRED"/>
<tx:methodname="edit*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="remove*"propagation="REQUIRED"/>
<tx:methodname="get*"propagation="SUPPORTS"/>
<tx:methodname="find*"propagation="SUPPORTS"/>
<tx:methodname="select*"propagation="SUPPORTS"/>
<tx:methodname="*"propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<aop:configproxy-target-class="false">
<aop:pointcutid="transactionPointcut"
expression="execution(* com.geok.serviceImpl.*.*(..))"/>
<aop:advisorpointcut-ref="transactionPointcut"
advice-ref="transactionAdvice"/>
</aop:config>
<!--配置声明试事务配置-->
<tx:annotation-driventransaction-manager="transactionManager"proxy-target-class="true"/>
</beans>
6 .配置jdbc.properties文件
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.161.201:1521:orcl
jdbc.username=fda
jdbc.password=fda
7 .定义一个接口存放增删改查方法
如:
8 配置映射User.xml文件
<mappernamespace="com.geok.mapper.UserMapper">
<resultMapid="UserResultMap"type="com.geok.entity.UserInfo">
<resultcolumn="NAME"property="name"jdbcType="VARCHAR"/>
<resultcolumn="PASSWORD"property="password"jdbcType="VARCHAR"/>
<resultcolumn="AGE"property="age"jdbcType="INTEGER"/>
</resultMap>
<selectid="selectUser"resultMap="UserResultMap">
SELECT*FROM USERINFO
</select>
</mapper>
9 .注入Mapper接口的映射类(注入后就可以调用mapper接口中的增删改查方法)
10.启动项目,在controller中调用方法