相关文献资料地址: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

最新文章

  1. 最大子段和(c++)
  2. C++_系列自学课程_第_9_课_C语言风格字符串_《C++ Primer 第四版》
  3. SQL JOIN的用法
  4. 天龙客户端的ResourceManager
  5. 【leetcode】Longest Consecutive Sequence(hard)☆
  6. decimal(a,b)
  7. FIR.im Weekly - 让炫酷 UI 为 APP 增色
  8. 基于css3的文字3D翻转特效
  9. oracle dmp数据导入
  10. &#39;weinre&#39; 不是内部或外部命令,也不是可运行的程序 或批处理文件。 解决方案
  11. java的main函数组成
  12. 对 JSON 数据进行序列化和反序列化
  13. 静默安装Oracle12.2数据库
  14. 35)django-验证码
  15. 简单的 FastDFS + Nginx 应用实例
  16. Cookiecutter: 更好的项目模板工具:(3)高级用法
  17. web基础----&gt;java邮件的发送
  18. LoadRunner学习笔记log函数
  19. 树(Tree,UVA 548)
  20. curl 命令参数

热门文章

  1. HDU 2955 Robberies【01背包】
  2. Image解码
  3. 如何使用阿里云的yum源
  4. luoguP4921 情侣?给我烧了! 组合数_容斥原理_计数问题
  5. HTML中使用 js 添加 data-toggle
  6. laravel :Call to undefined function App\Http\Controllers\success() 解决方法
  7. 洛谷1099 [NOIP2007] 树网的核
  8. JavaScript 实现留言框
  9. 使用plsql创建package
  10. React 中的 AJAX 请求:获取数据的方法