相关文献资料地址:http://www.mybatis.org/mybatis-3/zh/getting-started.html

关于如何创建一个项目,添加Tomcat运行环境和生成`web.xml`文件就不说了,我这边的Tomcat运行环境是8.5的

一、需要在`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>cn.tedu.mybatis</groupId>
<artifactId>MYBATIS_TEST_02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 用来测试的依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 添加SpringMVC依赖,视情况添加 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- 添加MyBatis的依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 添加MyBatis整合Spring的依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 其底层是基于JDBC的,所以,还需要添加Spring-jdbc的依赖,需要注意的是: 此依赖版本必须与Spring-webmvc的保持一致,也可以理解成带Spring-的版本都
保持一致 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- 根据使用的数据库,添加数据库连接驱动的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- 添加数据源的依赖 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>

二、在`src/main/resources`下创建`db.properties`文件(文件名可以自己取),用于配置数据库连接的相关信息:

url=jdbc:mysql://localhost:3306/tedu_ums?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai   #(这边连接的数据库名字似情况而定,我这边是tedu_ums)
    driver=com.mysql.cj.jdbc.Driver
    username=root   #(数据库账号)
    password=root   #(数据库密码)
    initialSize=2   #(池启动时创建的连接数量
    maxActive=50   #(同一时间可以从池分配的最多连接数量。设置为0时表示无限制

#这是一个注释
url=jdbc:mysql://localhost:3306/tedu_ums?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver=com.mysql.cj.jdbc.Driver
username=root
password=
initialSize=2
maxActive=50

三、在项目中准备名为`spring-dao.xml`的Spring配置文件,放在`src/main/resources`下并加载以上数据库的配置文件:

<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 加载数据库的配置文件 -->
<util:properties id="dbConfig" location="classpath:db.properties"></util:properties>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="#{dbConfig.url}"></property>
<property name="driverClassName" value="#{dbConfig.driver}"></property>
<property name="username" value="#{dbConfig.username}"></property>
<property name="password" value="#{dbConfig.password}"></property>
<property name="initialSize" value="#{dbConfig.initialSize}"></property>
<property name="maxActive" value="#{dbConfig.maxActive}"></property>
</bean>
</beans>

四、完成后在测试类那边测试看看是否可以正确的获取到数据库的连接:

package web;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo {
MapperScannerConfigurer ks;
SqlSessionFactoryBean bs;
@Test
public void getConnection() {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("spring-dao.xml");
DataSource data = ac.getBean("dataSource",DataSource.class);
System.out.println(data);
ac.close(); } }

我这边输出:jdbc:mysql://localhost:3306/tedu_ums?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai, UserName=root@localhost, MySQL Connector/J

搭健MyBatis开发环境的更多相关文章

  1. 使用IDEA和gradle搭建Spring MVC和MyBatis开发环境

    1. 概述 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化建构工具. 它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐 ...

  2. 搭建MyBatis开发环境及基本的CURD

    目录 一.MyBatis概述 1. MyBatis 解决的主要问题 二.快速开始一个 MyBatis 1. 创建mysql数据库和表 2. 创建maven工程 3. 在pom.xml文件中添加信息 4 ...

  3. 搭建mybatis开发环境

    1.创建工程 <groupId>com.hope</groupId>     <artifactId>day01_eesy_01mybatis</artifa ...

  4. Spring MVC+Maven+Freemarker+Mybatis开发环境搭建

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 创建一个Spring MVC项目 集成Freemarker 集成Mybatis Mybatis自动生成工具   利用STS( ...

  5. asp.net搭建mybatis开发环境

    mybatis其实就是ibatis的升级版本不仅能在java上使用,asp.net照样可以使用mybatis来开发程序.mybatis是一个比较小巧的ORM框架,类似hibernate.自己试了一下用 ...

  6. MyBatis01 Idea中搭建MyBatis开发环境

    项目结构 POM模板 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  7. mybatis 开发环境搭建

    不说废话直接上代码,首先看下我的目录机构: 红色部分,表明你所需的jar包,已经配置文件. 创建用户表,以及插入数据. create table books(id int (11) not null ...

  8. Maven+SpringMVC+Mybatis 开发环境整合

    1.maven build遇到了如下问题:  [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:rede ...

  9. 高速建成Android开发环境ADT-Bundle和Hello World

    ----下载JDK(Java Dev Kit) 官方下载:http://www.oracle.com/technetwork/java/javase/downloads/index.html 兴许步骤 ...

随机推荐

  1. 微信小程序和App的UI设计有什么异同吗?

    大家总是把小程序和App放在一起比,因此我也花时间看了一下小程序的开发指南,尤其是UI部分的设计和原则,今天就拿它和苹果的HIG(Human Interface Guidelines)做个比较,其实两 ...

  2. 防范CSRF(二)

    在防范CSRF(一)中使用的是微软默认的设置.在信息安全中默认的往往是最危险的.因此可以考虑更改cookie中默认的名字. 更改默认操作在Global.asax中的Application_Start使 ...

  3. Appstore排名前十的程序员应用软件

    程序员又名程序猿,苦逼劳累的代名词,曾经一个朋友这么开玩笑说,如果你是富二代,你当程序员就是脑残,如果你是穷二代,当程序员的话,死的时候一定是趴键盘. 程序员 哦,可怜的程序员.在那山的这边海的那边有 ...

  4. Day01-04学习内容总结

    学习内容小结 1.什么是编程,编程有什么用,什么是编程语言 2.计算的组成原理及组成部分 3.机械硬盘的工作原理 4.什么是操作系统,操作系统做了什么,为什么要有操作系统,操作系统有什么用 5.应用程 ...

  5. JS 日历

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  6. 算法22-----托普利茨矩阵leetcode766

    1.题目 如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵. 给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True. 示例 1: 输入: matr ...

  7. MySQL的读写分离的几种选择

    MySQL的读写分离的几种选择 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 原址如下: http://heylinux.com/archives/1004. ...

  8. Cygwin(类UNIX模拟环境)&CURL(强大的http命令行工具)

    前言: 需要我用curl试下能否发送post请求调起公司的仿真系统(目前) 跟着大佬的脚步,亲测一把~ 感谢大佬的提供的博客和指导 @咩神 个人博客园及来源地址 Cygwin(类UNIX模拟环境) 一 ...

  9. 20130912.Windows下常用命令的使用(不断更新)

    Win+R================================ cmd => 命令行 lpksetup => 弹出安装或者卸载Windows显示语言 ipconfig => ...

  10. 使用PoolingHttpClientConnectionManager解决httpclient的多线程请求问题

    直接上代码 1.主程序 public class TestMain { public static void main(String[] args) throws NSQException, Time ...