Maven Web项目配置Mybatis
一、添加Mybatis和数据库相关的包
1 pom.xml中添加的包有mybatis,mybatis-spring,druid,MySQL-connector-Java,commons-io,reflectasm
完整的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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>jade_admin</groupId>
- <artifactId>jade_admin</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>jade_admin</name>
- <description/>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <org.springframework.security.version>3.1.6.RELEASE</org.springframework.security.version>
- <org.springframework.version>3.2.9.RELEASE</org.springframework.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-api</artifactId>
- <version>7.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${org.springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${org.springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${org.springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${org.springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${org.springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-core</artifactId>
- <version>${org.springframework.security.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-config</artifactId>
- <version>${org.springframework.security.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-web</artifactId>
- <version>${org.springframework.security.version}</version>
- </dependency>
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.23</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.2.8</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.14</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.34</version>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>com.esotericsoftware.reflectasm</groupId>
- <artifactId>reflectasm</artifactId>
- <version>1.09</version>
- </dependency>
- <dependency>
- <groupId>org.tuckey</groupId>
- <artifactId>urlrewritefilter</artifactId>
- <version>4.0.3</version>
- </dependency>
- </dependencies>
- </project>
2 /src/main/webapp/WEB-INF/lib/中添加cglib-3.1.jar
二、配置web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
- <display-name>jade_admin</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/config/spring/applicationContext-config.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <filter>
- <filter-name>UrlRewriteFilter</filter-name>
- <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>UrlRewriteFilter</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>FORWARD</dispatcher>
- </filter-mapping>
- <!-- 添加Spring-Security过滤器 -->
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/service/*</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/config/spring/applicationContext-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/service/*</url-pattern>
- </servlet-mapping>
- </web-app>
三、配置applicationContext-config.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: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">
- <import resource="classpath*:config/spring/applicationContext-security.xml" />
- <import resource="classpath*:config/spring/applicationContext-resources.xml" />
- </beans>
四、配置applicationContext-resource.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:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- 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
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
- <!-- 阿里 druid数据库连接池 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
- <!-- 数据库基本信息配置 -->
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/jade?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull" />
- <property name="username" value="root" />
- <property name="password" value="123456" />
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="filters" value="stat" />
- <!-- 最大并发连接数 -->
- <property name="maxActive" value="20" />
- <!-- 初始化连接数量 -->
- <property name="initialSize" value="1" />
- <!-- 配置获取连接等待超时的时间 -->
- <property name="maxWait" value="60000" />
- <!-- 最小空闲连接数 -->
- <property name="minIdle" value="10" />
- <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="300000" />
- <property name="validationQuery" value="SELECT 'x'" />
- <property name="testWhileIdle" value="true" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <property name="maxOpenPreparedStatements" value="20" />
- <!-- 打开removeAbandoned功能 -->
- <property name="removeAbandoned" value="true" />
- <!-- 1800秒,也就是30分钟 -->
- <property name="removeAbandonedTimeout" value="1800" />
- <!-- 关闭abanded连接时输出错误日志 -->
- <property name="logAbandoned" value="true" />
- </bean>
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <bean id="txTemplate" class="org.springframework.transaction.support.TransactionTemplate">
- <property name="transactionManager" ref="txManager" />
- </bean>
- <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:config/mybatis/configuration.xml"/>
- <property name="mapperLocations" value="classpath*:config/mybatis/mapper/*Mapper.xml" />
- <property name="typeAliasesPackage" value="com.zheng.bean" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.zheng.dao"/>
- <property name="sqlSessionFactory" ref="SqlSessionFactory" />
- </bean>
- <tx:annotation-driven transaction-manager="txManager" />
- </beans>
五、配置configuration.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
- <!-- "http://ibatis.apache.org/dtd/ibatis-3-config.dtd" -->
- <!--
- plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
- properties?, settings?,
- typeAliases?, typeHandlers?,
- objectFactory?,objectWrapperFactory?,
- plugins?,
- environments?, databaseIdProvider?, mappers?
- -->
- <configuration>
- <properties>
- </properties>
- <!-- mybatis的内部配置 -->
- <settings>
- <!--使全局的映射器启用缓存 -->
- <setting name="cacheEnabled" value="true" />
- <!--启用延迟加载 ,当禁用时,所有关联对象都会即时加载 -->
- <setting name="lazyLoadingEnabled" value="false" />
- <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载 -->
- <setting name="aggressiveLazyLoading" value="true" />
- <!--允许多结果集返回, 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动)-->
- <setting name="multipleResultSetsEnabled" value="true" />
- <!--使用列标签代替列名,不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动 -->
- <setting name="useColumnLabel" value="true" />
- <!--不能用生成键 -->
- <setting name="useGeneratedKeys" value="false" />
- <!-- 指定MyBatis如何自动映射列到字段/属性。PARTIAL只会自动映射简单,没有嵌套的结果。FULL会自动映射任意复杂的结果(嵌套的或其他情况) -->
- <setting name="autoMappingBehavior" value="PARTIAL" />
- <!--默认执行器为SIMPLE,SIMPLE执行器没有什么特别之处。REUSE执行器重用预处理语句。BATCH执行器重用语句和批量更新 -->
- <setting name="defaultExecutorType" value="SIMPLE" />
- <!--超时时间2500毫妙,它决定驱动等待一个数据库响应的时间 -->
- <setting name="defaultStatementTimeout" value="25000" />
- <setting name="proxyFactory" value="CGLIB"/>
- </settings>
- </configuration>
Maven Web项目配置Mybatis的更多相关文章
- Maven Web项目配置Mybatis出现SqlSessionFactory错误的解决方案
一.错误现象 严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Er ...
- myeclipse maven web项目配置
启用maven:window-->preference-->MyEclipse-->Maven4MyEclipse, 勾选复选框(Enable Mave4MyEclipse feat ...
- maven web项目配置log4j,及log4j参数设置
本文为博主原创,转载须注明转载地址: 1.在maven项目中引入相关的依赖: 需要依赖的jar为: <!-- 配置日志 --> <dependency> <groupId ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一:建立MAVEN Web项目
一:创建maven web项目er
- Intellij IDEA创建的Web项目配置Tomcat并启动Maven项目
本篇博客讲解IDEA如何配置Tomcat. 大部分是直接上图哦. 点击如图所示的地方,进行添加Tomcat配置页面 弹出页面后,按照如图顺序找到,点击+号 tomcat Service -> L ...
- 搭建maven web项目并配置quartz定时任务【业务:对比数据变化内容】 历程
搭建maven web项目并配置quartz定时任务[业务:对比数据变化内容] 历程2018年03月03日 10:51:10 守望dfdfdf 阅读数:100更多个人分类: 工作 问题编辑版权声明:本 ...
- myeclipse 2014新建maven web 项目步骤
首先在myeclipse中配置maven的安装路径: 在pom.xml中加上: 1 2 3 4 5 6 7 8 9 10 <plugins> <plugin&g ...
- Maven WEB 项目使用ProGuard进行混淆,最佳解决方案
Maven WEB 项目使用ProGuard进行混淆,最佳解决方案 近期公司的Android项目做了混淆,虽说对于保护代码并不是100%的,但混淆后的代码可以使那些不法份子难以阅读,这样也能对代码的保 ...
随机推荐
- centos 7 防火墙设置
一.介绍 centos 7 的防火墙是以firewalld daemon的形式存在,区别于iptables 二.使用方法 centos7 主要通过firewall-cmd命令来管理firewall, ...
- 手动安装gcc 4.8.5
# 下载gcc wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.5/gcc-4.8.5.tar.gz # 解压并进入目录 tar -zxvf gcc-.tar.gz cd ...
- 最长上升子序列:2016 Pacific Northwest Region Programming Contest—Division 2 Problem M
Description A string of lowercase letters is calledalphabeticalif deleting zero or more of its lette ...
- luogu P1353 [USACO08JAN]跑步Running
题目描述 The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ...
- [Atcoder Grand Contest 004] Tutorial
Link: AGC004 传送门 A: …… #include <bits/stdc++.h> using namespace std; long long a,b,c; int main ...
- 【主席树】Gym - 101237A - MEX-Query
主席树里每个值的位置存当前该值出现的最右位置. 如果root[r]的前缀主席树中,某值最右位置大于等于l,说明该值出现在了l,r中. 所以主席树维护区间最小值,如果左半值域的最小值<l,则说明左 ...
- Problem G: 零起点学算法86——Fibonacc
#include<stdio.h> int main(){ ]={,,}; ;i<=;i++) { a[i]=a[i-]+a[i-]; } scanf("%d", ...
- Problem A: 零起点学算法80——逆序输出(数组练习)
#include<stdio.h> int main() { ]; scanf("%d",&n); ;i<=n;i++){ scanf("%d& ...
- [百度之星2014资格赛] Disk Schedule 报告
Disk Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- codevs 1962 马棚问题--序列型DP
1962 马棚问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 每天,小明和他的马外出,然后他们一边跑一边玩耍.当他们结束 ...