springmvc+mybatis配置

环境:

Jdk 1.8.0_40

Eclipse 4.4.2

Tomcat 8.0.20

Maven 3.2.5

Mybatis 3.2.8

Spring 4.1.5

主要步骤:(以下能够正常运行)

1. 新建工程

Maven Project

-> New Maven Project 保持默认,直接下一步

-> 选择org.apache.maven.archetypes,点击下一步

-> 输入Group id 和Artifact id,完成

2. 配置pom.xml

<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>cn.sdut.ming</groupId>

<artifactId>web-demo</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>web-demo Maven Webapp</name>

<url>http://maven.apache.org</url>

<properties>

<!-- spring版本号 -->

<spring.version>4.1.5.RELEASE</spring.version>

<!-- mybatis版本号 -->

<mybatis.version>3.2.8</mybatis.version>

<!-- log4j日志文件管理包版本 -->

<slf4j.version>1.7.10</slf4j.version>

<log4j.version>1.2.17</log4j.version>

</properties>

<dependencies>

<!-- spring核心包 -->

<!-- springframe start -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</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-oxm</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-webmvc</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-context-support</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 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<!-- 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.34</version>

</dependency>

<!-- 阿里巴巴数据源包 -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.0.13</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>

<!-- log end -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.5.1</version>

</dependency>

</dependencies>

<build>

<finalName>web-demo</finalName>

</build>

</project>

3. 建立目录结构(如下)

4. 相关配置文件

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>Archetype Created Web Application</display-name>

<!-- 读取spring配置文件 -->

<context-param>

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

<param-value>classpath:conf/spring.xml;

classpath:conf/spring-mybatis.xml

</param-value>

</context-param>

<!-- 设计路径变量值 -->

<context-param>

<param-name>webAppRootKey</param-name>

<param-value>springmvc.root</param-value>

</context-param>

<!-- Spring字符集过滤器 -->

<filter>

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

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

</filter-mapping>

<!-- 日志记录 -->

<context-param>

<!-- 日志配置文件路径 -->

<param-name>log4jConfigLocation</param-name>

<param-value>classpath:conf/log4j.properties</param-value>

</context-param>

<context-param>

<!-- 日志页面的刷新间隔 -->

<param-name>log4jRefreshInterval</param-name>

<param-value>6000</param-value>

</context-param>

<listener>

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

</listener>

<listener>

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

</listener>

<!-- springMVC核心配置 -->

<servlet>

<servlet-name>spring</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

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

<param-value>classpath:conf/spring-mvc.xml</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- 错误跳转页面 -->

<error-page>

<!-- 路径不正确 -->

<error-code>404</error-code>

<location>/WEB-INF/errorpage/404.jsp</location>

</error-page>

<error-page>

<!-- 没有访问权限,访问被禁止 -->

<error-code>405</error-code>

<location>/WEB-INF/errorpage/405.jsp</location>

</error-page>

<error-page>

<!-- 内部错误 -->

<error-code>500</error-code>

<location>/WEB-INF/errorpage/500.jsp</location>

</error-page>

</web-app>

jdbc.properties

jdbc_driverClassName=com.mysql.jdbc.Driver

jdbc_url=jdbc:mysql://localhost:3306/spring?useUnicode=true&amp;characterEncoding=utf-8

jdbc_username=root

jdbc_password=XXXXX

log4j.properties

### set log levels ###

#log4j.rootLogger = debug , stdout , D , E

log4j.rootLogger = debug , stdout , D

### output to the console ###

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target = System.out

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

#log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n

log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

### Output to the log file ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/log.log

log4j.appender.D.Append = true

log4j.appender.D.Threshold = DEBUG

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

log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

### Save exception information to separate file ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/error.log

log4j.appender.D.Append = true

log4j.appender.D.Threshold = ERROR

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

log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

mybatis-config.xml

<?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>

<!-- 命名空间 -->

<typeAliases>

<typeAlias alias="User" type="cn.ming.model.User"/>

</typeAliases>

<!-- 映射map -->

<mappers>

</mappers>

</configuration>

spring-mvc.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

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"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-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/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 扫描controller(controller层注入) -->

<context:component-scan base-package="cn.ming.controller"/>

<!-- 避免IE在ajax请求时,返回json出现下载 -->

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

<!-- 对模型视图添加前后缀 -->

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

p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

</beans>

spring-mybatis.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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

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.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-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/util

http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"

destroy-method="close" >

<property name="driverClassName">

<value>${jdbc_driverClassName}</value>

</property>

<property name="url">

<value>${jdbc_url}</value>

</property>

<property name="username">

<value>${jdbc_username}</value>

</property>

<property name="password">

<value>${jdbc_password}</value>

</property>

<!-- 连接池最大使用连接数 -->

<property name="maxActive">

<value>20</value>

</property>

<!-- 初始化连接大小 -->

<property name="initialSize">

<value>1</value>

</property>

<!-- 获取连接最大等待时间 -->

<property name="maxWait">

<value>60000</value>

</property>

<!-- 连接池最大空闲 -->

<property name="maxIdle">

<value>20</value>

</property>

<!-- 连接池最小空闲 -->

<property name="minIdle">

<value>3</value>

</property>

<!-- 自动清除无用连接 -->

<property name="removeAbandoned">

<value>true</value>

</property>

<!-- 清除无用连接的等待时间 -->

<property name="removeAbandonedTimeout">

<value>180</value>

</property>

<!-- 连接属性 -->

<property name="connectionProperties">

<value>clientEncoding=UTF-8</value>

</property>

</bean>

<!-- mybatis文件配置,扫描所有mapper文件 -->

<bean id="sqlSessionFactory"

class="org.mybatis.spring.SqlSessionFactoryBean"

p:dataSource-ref="dataSource"

p:configLocation="classpath:conf/mybatis-config.xml"

p:mapperLocations="classpath:mapper/*.xml"/><!-- configLocation为mybatis属性 mapperLocations为所有mapper-->

<!-- spring与mybatis整合配置,扫描所有dao -->

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

p:basePackage="cn.ming.dao"

p:sqlSessionFactoryBeanName="sqlSessionFactory"/>

<!-- 对数据源进行事务管理 -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource"/>

</beans>

spring.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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.xsd">

<!-- 引入jdbc配置文件 -->

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

<!-- 扫描文件(自动将servicec层注入) -->

<context:component-scan base-package="cn.springmvc.service"/>

</beans>

userMapper.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="cn.springmvc.dao.UserDAO">

<insert id="insertUser" parameterType="User" keyProperty="id">

insert into days_user(

state,

nickname)

values

(

#{state},

#{nickname})

</insert>

</mapper>

源代码

链接: http://pan.baidu.com/s/1sjC0laP 密码: h4bv

时间: 2024-10-05 21:30:29

springmvc+mybatis配置的相关文章

【转】一次SpringMVC+ Mybatis 配置多数据源经历

需求 现在在维护的是学校的一款信息服务APP的后台,最近要开发一些新功能,其中一个就是加入学校电影院的在线购票.在线购票实际上已经有一套系统了,但是是外包给别人开发的,我们拿不到代码只能拿到数据库,并且也不一定能很好的兼容之前的代码,所以需要基于这个数据库来进行新的开发. 现在用的后台是SpringMVC+Mybatis+MySQL开发的,购票用的是SQL Server 2008(好古老的东西了),因为要用一套用户体系所以不可能再去单独为了这个功能弄一个系统出来,因此要在原项目中兼容这个数据库.

[转]SpringMVC+ Mybatis 配置多数据源 + 手动切换数据源

正确可行的解决方法:使用Spring提供的AbstractRoutingDataSource类来根据请求路由到不同的数据源.具体做法是先设置两个不同的dataSource代表不同的数据源,再建一个总的dynamicDataSource,根据不同的请求去设置dynamicDataSource.代码如下: 配置文件spring-mybatis.xml <!--统一的dataSource--> <bean id="dynamicDataSource" class="

springMVC,mybatis配置事务

首先应该对context:include-filter和context:exclude-filter有一个了解,include是包含这个,而不是只包含,下面会有例子看看: 这是我最开始配置的MVC的配置文件,我以为是我这么配置以后,这个扫描就会只扫描Controller注解,但是不是这样的,按照我的测试 这个注解是包含了Controller,但是也应该也是对Service注解进行了扫描的,否则事务不应该不好使: 下面来看我的成功的配置文件: 在这个配置文件中仅仅是将Service剔除掉了而已,这

Springmvc+mybatis配置前台页面传递JSON串给后台接收。

前台页面js方法: $(document).ready(function(){ var saveDataAry=[]; var data1={"id":5,"name":"益生元","province":"西藏"}; var data2={"id":6,"name":"好爸爸","province":"拉萨"

如何玩转最新的项目的搭配springmvc+mybatis+Redis+Nginx+tomcat+mysql

上一次完成nginx+tomcat组合搭配,今天我们就说说,这几个软件在项目中充当的角色: 要想完成这几个软件的组合,我们必须知道和熟悉应用这个框架, 一: Nginx:在项目中大多数作为反向代理服务器.其目的处理http静态页面.和分发请求给tomcat.是目前处理大量请求的解决方案. tomcat:作为处理动态页面的服务器.由Ngxin 均衡分给的请求来处理. redis:在这个里redis 处理两个重要的功能:第一,nginx分发请求给tomcat.如何保持session的同步.就是利用r

ehcache配置:使用Spring+SpringMVC+Mybatis或者有shiro【转】

SSM框架的搭建就不在叙述了 本文主要是讲解在SSM基础上再加上ehcache 1:首先:pom.xml需要的jar <dependency>   <groupId>org.mybatis</groupId>   <artifactId>mybatis-ehcache</artifactId>   <version>1.0.0</version>  </dependency>  <dependency&g

springmvc,mybatis,shiro综合配置示例

<?xml version="1.0" encoding="UTF-8"?> <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 htt

spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)

spring,springmvc,mybatis基本整合(一)–xml文件配置方式(2)之mapper接口 一,整合结构 二,所需jar包 如上图. 三,整合配置 1,web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://j

用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试

这一部分的主要目的是 配置spring-service.xml  也就是配置spring  并测试service层 是否配置成功 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)在这个基础上面 继续进行spring的配置. 回顾上面  我们已经成功测试通过了Mybatis的配置. 这时候的目录结构是: 一:下面我们继续补充目录结构,在com.peakfortake的文件目录项目