zoukankan      html  css  js  c++  java
  • 搭健MyBatis开发环境

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

  • 相关阅读:
    【python35小工具】b站弹幕保存
    w7 python35 输出中文乱码解决
    【python】版本35 正则-非库-爬虫-读写xlw文件
    【python小工具】我在bilibili个人资料里控制家里的电脑
    【python小工具】linux 低权限密码记录 提权小套路
    [学习交流]博客园 cnblog 添加github链接和自定义美化学习
    cp2102 驱动 win7x64 -2018
    基于Vue实现可以拖拽的树形表格(原创)
    不起眼的 z-index 却能牵扯出这么大的学问
    彻底搞懂CSS伪类选择器:is、not
  • 原文地址:https://www.cnblogs.com/package-java/p/10316536.html
Copyright © 2011-2022 走看看