• 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());
        }
    
    }
     

  • 相关阅读:
    并查集
    树状数组及二维树状数组
    maven工程编译成jar包
    The Salt Master has rejected this minion's public key!
    salt-minion dead but pid file exists 正确解决方法
    mysql 查找表的auto_increment和修改
    fastjson --JSONObject 和JSONArray 转换
    git 获取当前版本的commitid
    fastjson 使用笔记
    spring IOC 注解@Resource
  • 原文地址:https://www.cnblogs.com/currystar/p/10882209.html
Copyright © 2020-2023  润新知