首页 > 代码库 > maven+ spring+ spring mvc + mybatis
maven+ spring+ spring mvc + mybatis
maven 配置
<project xmlns="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.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ks</groupId>
<artifactId>mis</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mis Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.0.RELEASE</spring.version>
</properties>
<dependencies>
<!-- offer by chenney -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</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-aspects</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-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.2</version>
</dependency>
<!-- offer by mike -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.16</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.2.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>mis</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<configuration>
<port>8083</port>
<path>/mis</path>
<uriEncoding>utf-8</uriEncoding>
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<junitArtifactName>junit:junit</junitArtifactName>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 容器监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring默认的Servlet -->
<servlet>
<servlet-name>dispatcherservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-MVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Servlet映射 -->
<servlet-mapping>
<servlet-name>dispatcherservlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcherservlet</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<!-- 编码格式 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 日志配置文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<!-- <param-value>classpath:log4j.xml</param-value> -->
<param-value>classpath:config/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- jstl标签 -->
<!-- <jsp-config>
<taglib>
<taglib-uri>Taglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<jsp-property-group>
<url-pattern>/jsp/* </url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/include/prelude.jspf</include-prelude>
<include-coda>/include/coda.jspf</include-coda>
</jsp-property-group>
</jsp-config> -->
<!-- <jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/app-c-rt.tld</taglib-location>
</taglib>
</jsp-config> -->
<!-- 欢迎页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring 配置
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-autowire="byName">
<context:property-placeholder location="classpath:config/jdbc.properties"
ignore-unresolvable="true" />
<!-- 自动扫描Bean -->
<context:component-scan base-package="com.ks.mis" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 配置数据源 -->
<!-- <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${DBDriver}</value>
</property>
<property name="url">
<value>${DBUrl}</value>
</property>
<property name="username">
<value>${userName}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean> -->
<!-- 配置数据源 -->
<bean id="ds"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/mydb</value>
</property>
</bean>
<!-- 配置SqlSessionFactory对象,注意这里的名字 -->
<bean id="sqlSessionFactoryBeanName" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds" /><!--数据源 -->
<property name="configLocation" value="http://www.mamicode.com/classpath:mybatis.xml" />
</bean>
<!-- 配置SqlSession对象 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactoryBeanName" />
</bean>
<!-- Mapper的扫描文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="http://www.mamicode.com/com.ks.mis.dao.mapper" />
</bean>
<!-- Local Transaction Management (txManager) -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds" />
</bean>
</beans>
spring mvc:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-autowire="byName">
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<!-- 自动扫描Bean -->
<context:component-scan base-package="com.ks.mis">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- <context:component-scan base-package="com.ks.mis">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan> -->
<!-- 添加注解驱动 -->
<!-- <mvc:annotation-driven /> -->
<!-- 允许对静态资源文件的访问 -->
<!-- <mvc:default-servlet-handler /> -->
<!-- 处理在类级别上的@RequestMapping注解,增加了拦截器-->
<!-- <bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
<property name="interceptors">
多个拦截器,顺序执行
<list>
<ref bean="loginInterceptor"/>
</list>
</property>
</bean> -->
<!-- 处理方法级别上的@RequestMapping注解-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<util:list id="beanList">
<!-- 将返回对象转换为json -->
<ref bean="mappingJacksonHttpMessageConverter"/>
</util:list>
</property>
</bean>
<!-- mvc的视图扭转 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<!-- 为了处理返回的JSON数据的编码,默认是ISO-88859-1的,这里把它设置为UTF-8,解决有乱码的情况 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
</bean>
<!-- 文件上传-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size2MB(2097152) 5MB(5242880) -->
<property name="maxUploadSize">
<value>2097152</value>
</property>
<property name="maxInMemorySize">
<value>1024</value>
</property>
</bean>
</beans>
<?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>
<settings>
<!-- changes from the defaults -->
<setting name="lazyLoadingEnabled" value="http://www.mamicode.com/false" />
<!-- JdbcType enumeration. Most common are: NULL, VARCHAR and OTHER -->
<setting name="jdbcTypeForNull" value="http://www.mamicode.com/NULL" />
</settings>
<!-- 当自定义的配置文件和自动生成的文件在同一个文件夹时,这里可以不配置,自定义的mapper文件建议以Dao结尾-->
<mappers>
<mapper resource="mybatis/UserModelMapper.xml" />
</mappers>
</configuration>
jdbd.properties:
DBDriver="oracle.jdbc.driver.OracleDriver"
DBUrl="jdbc:oracle:thin:@localhost :1521:orcl"
username="C##XU"
password="C##XU"
log.properties:
log4j.rootLogger=INFO,CONSOLE,FILEOUT,errorlogfile
log4j.addivity.org.apache=true
#??§?????°è????o???
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=ERROR
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%d]-%-5p [%t] (%F:%L)|%m%n
#??′??¥è????o??????è??è??????-?
log4j.appender.CONSOLE.ImmediateFlush=true
# é??è?¤??ˉSystem.out??1???è????o
log4j.appender.CONSOLE.Target=System.err
# è????o??¥??????INFO?????ˉ
log4j.appender.FILEOUT=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILEOUT.Threshold=INFO
#????-?log??????è·ˉ???
#log4j.appender.FILEOUT.File=E:\\upload-web.log
#log4j.appender.FILEOUT.File=/mnt/ntfs/LG_FOLDER/logs/cmc-lg/info/upload-web.log
log4j.appender.FILEOUT.File=/home/tomcatuser/lg_export/logs/upload-web.log
# é??è?¤??otrue????·??? ??°????°????false??¨?ˉ??????ˉ??¨???è??è??è|????
log4j.appender.FILEOUT.Append=true
# ?????alog??????????¤§?°????è??è??è????a?¤§?°??°±????????????1??a??¥??? # KB ???MB???GB
#log4j.appender.FILEOUT.MaxFileSize=10MB
# ????¤?????-?3??a???????¤????
#log4j.appender.FILEOUT.MaxBackupIndex=3
log4j.appender.FILEOUT.DatePattern=‘.‘yyyy-MM-dd
# è????o?????????? ????
log4j.appender.FILEOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.FILEOUT.layout.ConversionPattern=[%d]-%-5p [%t] (%F:%L)|%m%n
# è????o??¥??????ERROR?????ˉ
log4j.appender.errorlogfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorlogfile.Threshold=ERROR
#log4j.appender.errorlogfile.File=E:\\upload-web-error.log
#log4j.appender.errorlogfile.File=/mnt/ntfs/LG_FOLDER/logs/cmc-lg/error/upload-web-error.log
log4j.appender.errorlogfile.File=/home/tomcatuser/lg_export/logs/upload-web-error.log
#é??è?¤??otrue????·??? ??°????°????false??¨?ˉ??????ˉ??¨???è??è??è|????
log4j.appender.errorlogfile.Append=true
#??′??¥è????o??????è??è??????-?
log4j.appender.errorlogfile.ImmediateFlush=true
log4j.appender.errorlogfile.DatePattern=‘.‘yyyy-MM-dd
# è????o?????????? ????
log4j.appender.errorlogfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorlogfile.layout.ConversionPattern =[%d]-%-5p [%t] (%F:%L)|%m%n
xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ks.mis.dao.mapper.UserModelMapper" >
<resultMap id="BaseResultMap" type="com.ks.mis.dao.model.UserModel" >
<id column="userid" property="userId" jdbcType="INTEGER" />
<result column="username" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="userrole" property="userRole" jdbcType="VARCHAR" />
<result column="lastLoginTime" property="lastLoginTime" jdbcType="TIMESTAMP" />
<result column="usertype" property="userType" jdbcType="VARCHAR" />
<result column="telephone" property="telephone" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
userid, username, password, userrole, lastlogintime, telephone, email
</sql>
<select id="selectUserInfoCount" parameterType="com.ks.mis.dao.model.UserModel" resultType="java.lang.Integer">
SELECT COUNT(1) FROM userinfo A
<if test="userName != null and ‘‘ != userName">
where username like ‘%‘+#{userName,jdbcType=VARCHAR}+‘%‘
</if>
</select>
<select id="selectUserInfo" parameterType="com.ks.mis.dao.model.UserModel" resultMap="BaseResultMap">
SELECT userid, username, password, userrole, lastlogintime, telephone, email
FROM userinfo where rownum between 0 and 10
</select>
</mapper>
maven+ spring+ spring mvc + mybatis