您的位置:首页 > 编程语言 > Java开发

spring+springMVC+mybatis框架搭建

2014-11-04 18:56 393 查看
一.web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>test</display-name>

<listener>

<description>spring监听</description>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<listener>

<description>配置内存泄露监听</description>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

<filter>

<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>

<url-pattern>/</url-pattern>

</filter-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:application*.xml</param-value>

</context-param>

<servlet>

<description>springMVC核心分发器</description>

<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.jsp</welcome-file>

</welcome-file-list>

</web-app>

二.springMVC 配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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">
<mvc:annotation-driven/>

<mvc:default-servlet-handler/>

<context:component-scan base-package="com.test">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<!-- 视图解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/view/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

三.application 配置文件

<?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:context="http://www.springframework.org/schema/context"

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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

<!-- 加载配置文件 -->

<context:property-placeholder location="classpath:config.properties"/>

<context:component-scan base-package="com.test">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

<property name="url" value="${jdbc_url}" />

<property name="username" value="${jdbc_username}" />

<property name="password" value="${jdbc_password}" />

</bean>

<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="mapperLocations" value="classpath*:com/test/mapping/*Mapper.xml"></property>

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.test.dao"></property>

</bean>

</beans>

四.log4j 配置文件

# Output pattern : date [thread] priority category - message FATAL 0 ERROR 3 WARN 4 INFO 6 DEBUG 7

log4j.rootLogger=all, Console, RollingFile

#Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

#RollingFile

log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender

#log4j.appender.RollingFile.File=${catalina.base}/wtpwebapps/test/document/logs/jeesite.log

log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout

log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

#Springframework level

#log4j.logger.org.springframework=ERROR

#Hibernate level

#log4j.logger.org.hibernate=ERROR

### \u8bbe\u7f6e\u5f53\u524d\u9879\u76ee\u4e2d\u5177\u4f53\u6a21\u7248\u7684\u65e5\u5fd7

log4j.logger.com.longruan.zmsys.dao=debug

五.config.properties

#validationQuery=SELECT 1

#jdbc_url=jdbc:mysql://localhost:3306/bkytest?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

##jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

#jdbc_username=root

#jdbc_password=admin

jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

jdbc_username=zm08

jdbc_password=zm08

六.mapper.xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.test.dao.UserMapper">

<resultMap type="com.test.model.User" id="resultMap">

<result column="id" property="id"/>

<result column="name" property="name"/>

<result column="password" property="password"/>

</resultMap>

<select id="getAll" resultMap="resultMap">

select * from myuser

</select>

<select id="findUser" parameterType="com.test.model.User" resultType="int">

select count(*) from myuser where name=#{name} and password=#{password}

</select>

</mapper>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: