zoukankan      html  css  js  c++  java
  • mybatis开发流程及测试

    1.创建maven工程(jar包),添加相关依赖:mybatis、mysql驱动、junit、log4j

    <dependencies>
    
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.1.1</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
        </dependencies>
    2.添加相关配置文件:
    2.1核心配置文件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>
    
     <properties resource="config.properties"></properties>
     
     <typeAliases>
        <package name="com.curry.beans"/>
     </typeAliases>
    
     
      <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
          </dataSource>
        </environment>
      </environments>
     
      <mappers>
        <package name="com.curry.dao"/>
      </mappers>
      
      
    </configuration>
    2.2log4j
    2.3数据库连接信息配置文件config.properties
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/kkb
    jdbc.username=root
    jdbc.password=123456
    3.添加实体映射类
    package com.curry.beans;
    
    public class Dept {
    
        private Integer deptNo;
    
        private String dname;
    
        private String loc;
    
        public Integer getDeptNo() {
            return deptNo;
        }
    
        public void setDeptNo(Integer deptNo) {
            this.deptNo = deptNo;
        }
    
        public String getDname() {
            return dname;
        }
    
        public void setDname(String dname) {
            this.dname = dname;
        }
    
        public String getLoc() {
            return loc;
        }
    
        public void setLoc(String loc) {
            this.loc = loc;
        }
    
    }
    4.添加sql映射文件
    <?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="com.curry.dao.DeptMapper">
    
        <select id="selectDeptByDeptNo" resultType="Dept">
            select * from dept where DEPTNO = #{deptNo}
        </select>
       
    </mapper>
    5.测试类
    public class TestMybatis {
        private SqlSession session;
    
        @Before
        public void start() {
            try {
                InputStream inputStream = Resources.getResourceAsStream("myBatis-config.xml");
                SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
                session = factory.openSession();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    
        @Test
        public void test01() {
            DeptMapper dao = session.getMapper(DeptMapper.class);
            Dept dept = dao.selectDeptByDeptNo(10);
            System.out.println(dept.getDname() + "======" + dept.getLoc());
        }
    
    }
     

  • 相关阅读:
    如何使用KeyChain保存和获取UDID
    ios 使用keychain来存储token
    关于button中设置文字不显示的问题
    实现uitable cell中点击button设置当前cell为选中状态
    setImageEdgeInsets 和 setImage配合使用达到button区域大并可调节其上图片显示区域大小的效果
    C#之Action的实际应用例子
    ConcurrentQueue队列的基本使用方式
    【转】C#中base关键字的几种用法
    C# Activator和new的区别
    C#回调浅析(委托、接口回调)
  • 原文地址:https://www.cnblogs.com/currystar/p/10882209.html
Copyright © 2011-2022 走看看