4.使用接口+xml文件方式编写dao层

创建maven工程
4.使用接口+xml文件方式编写dao层_第1张图片

引入maven依赖

<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.0modelVersion>

  <groupId>com.cjqgroupId>
  <artifactId>cjq-mybatis1artifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>cjq-mybatis1name>
  <url>http://maven.apache.orgurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.2.8version>
    dependency>

    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.35version>
    dependency>

  dependencies>
project>

创建主配置文件configuration.xml



<configuration>

    <typeAliases>
        <typeAlias type="com.cjq.vo.Emp" alias="emp" />
    typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="admin"/>
            dataSource>
        environment>
    environments>
    <mappers>
        
        <mapper resource="mapper/EmpMapper.xml"/>
    mappers>
configuration>

emp表
4.使用接口+xml文件方式编写dao层_第2张图片

emp类

package com.cjq.vo;

/**
 * Created by cjq on 2017/9/16.
 */
public class Emp {
    public Emp() {
    }

    public Integer getEno() {
        return eno;

    }

    public Emp(Integer eno, String ename, String job, Double sal) {
        this.eno = eno;
        this.ename = ename;
        this.job = job;
        this.sal = sal;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "eno=" + eno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                '}';
    }

    public void setEno(Integer eno) {
        this.eno = eno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Double getSal() {
        return sal;
    }

    public void setSal(Double sal) {
        this.sal = sal;
    }

    Integer eno;
    String ename;
    String job;
    Double sal;

}

创建接口

package com.cjq.dao;

import com.cjq.vo.Emp;

/**
 * Created by cjq on 2017/9/16.
 */
public interface EmpDao {

    public void insert(Emp emp);

    public void delete(int eno);

    public Emp getEmpById(int eno);
}


<mapper namespace="com.cjq.dao.EmpDao">
     
     

    
      <delete id="delete" parameterType="int">
        delete from emp where eno=#{eno}
      delete>
    
      <insert id="insert" parameterType="com.cjq.vo.Emp">
        insert into emp(eno,ename,job,sal) values(#{eno},#{ename},#{job},#{sal})
      insert>
    
      <select id="getEmpById"  parameterType="int" resultType="com.cjq.vo.Emp">
           select * from emp where eno=#{value}
      select>
mapper>

获取SqlSession的工具类

package com.cjq.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by cjq on 2017/9/16.
 */
public class SessionFactory {
    static SqlSessionFactory factory=null;
    static{
        //测试查询
        String resource="configuration.xml";
        //获取资源的流
        InputStream r= null;
        try {
            r = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获得session工厂
         factory=new SqlSessionFactoryBuilder().build(r);
    }
    public static SqlSession getSession(){
        //openSession默认事物是手动提交的
         return factory.openSession();
    }

}

测试

package com.cjq.vo;


import com.cjq.dao.EmpDao;
import com.cjq.dao.SessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;

/**
 * Created by cjq on 2017/9/16.
 */
public class DeptTest {

    public static void main(String[] args) throws IOException {
        //获取session
        SqlSession session= SessionFactory.getSession();
        //获得Dao
        EmpDao dao=session.getMapper(EmpDao.class);

       // dao.insert(new Emp(4,"zhangsan","job1",4000.0));
       // Emp emp=dao.getEmpById(4);
       // System.out.println(emp);
         dao.delete(4);

        session.commit();
        session.close();

    }
}

你可能感兴趣的:(mybatis,mybatis)