mybatis的简单使用

时间:2021-10-25 17:00:05

使用mybatis数据库时,需要添加一下jar包:

 

asm-3.3.1.jar
cglib-2.2.2.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
mybatis-3.2.2.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar

 

当然了,数据库驱动jar包不能忘记添加,mybatis jar包可以从官网上下载。

下面是mybatis的使用:

Dept.java

mybatis的简单使用mybatis的简单使用
package com.entity;



public class Dept implements java.io.Serializable {

    

    private Integer id;
    private String name;
    private String location;


    
    public Dept() {
    }

    

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return this.location;
    }

    public void setLocation(String location) {
        this.location = location;
    }


}
View Code

Dao包中的接口DeptDao.java

mybatis的简单使用mybatis的简单使用
package com.dao;

import java.util.List;

import com.entity.Dept;

/**
 * 全新环境下的老代码
 * @author 
 * @date 2015年12月3日 下午4:27:36
 */
public interface DeptDao {

    public void insertDept(Dept dept);
    
    public void updateDept(Dept dept);
    
    public void deleteDept(int id);
    
    public List<Dept> selAll();
    
}
View Code

DeptDaoMapper.xml

mybatis的简单使用mybatis的简单使用
<?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.dao.DeptDao">
    <resultMap id="deptResultMap" type="com.entity.Dept">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="location" column="location" />
        <result property="job" column="job" />
    </resultMap>
    <insert id="insertDept" parameterType="Dept">
        insert into dept(id,name,location) values(seq_dept_id.nextval,#{name},#{location})
    </insert>
    <update id="updateDept" parameterType="Dept">
        update dept set name=#{name},location=#{location} where id=#{id}
    </update>
    <delete id="deleteDept" parameterType="Dept">
        delete dept where id=#{id}
    </delete>
    <select id="selectAll" resultType="Dept">
        select * from dept
    </select>
</mapper>
View Code

DeptDao接口的实现类DeptDaoImpl.java

mybatis的简单使用mybatis的简单使用
package com.dao.impl;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

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 com.dao.DeptDao;
import com.entity.Dept;

public class DeptDaoImpl implements DeptDao {

    @Override
    //增加
    public void insertDept(Dept dept) {
        //1.指定数据源
        String resource="mybatis-config.xml";
        //构建sqlsf对象
        SqlSessionFactory factory=null;
        //sqlsession
        SqlSession session=null;
        //Reader对象
        Reader reader=null;
        try {
            //获取reader对象
            reader=Resources.getResourceAsReader(resource);
            //工厂生成器
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //得到工厂
            factory=builder.build(reader);
            //得到session
            session=factory.openSession();
            session.insert("insertDept", dept);
            //增删改需提交事务
            session.commit();
            System.out.println("添加成功了");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                reader.close();
                session.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    @Override
    //修改
    public void updateDept(Dept dept) {
        //1.指定数据源
        String resource="mybatis-config.xml";
        //构建sqlsf对象
        SqlSessionFactory factory=null;
        //sqlsession
        SqlSession session=null;
        //Reader对象
        Reader reader=null;
        try {
            //获取reader对象
            reader=Resources.getResourceAsReader(resource);
            //工厂生成器
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //得到工厂
            factory=builder.build(reader);
            //得到session
            session=factory.openSession();
            session.update("updateDept", dept);
            //增删改需提交事务
            session.commit();
            System.out.println("修改成功了");
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    reader.close();
                    session.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

    }

    @Override
    //删除
    public void deleteDept(int id) {
        //1.指定数据源
        String resource="mybatis-config.xml";
        //构建sqlsf对象
        SqlSessionFactory factory=null;
        //sqlsession
        SqlSession session=null;
        //Reader对象
        Reader reader=null;
        try {
            //获取reader对象
            reader=Resources.getResourceAsReader(resource);
            //工厂生成器
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //得到工厂
            factory=builder.build(reader);
            //得到session
            session=factory.openSession();
            session.delete("deleteDept", id);
            //增删改需提交事务
            session.commit();
            System.out.println("删除成功了");
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    reader.close();
                    session.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

    }

    @Override
    //查询所有
    public List<Dept> selAll() {
        List<Dept> list=new ArrayList<Dept>();
        //1.指定数据源
                String resource="mybatis-config.xml";
                //构建sqlsf对象
                SqlSessionFactory factory=null;
                //sqlsession
                SqlSession session=null;
                //Reader对象
                Reader reader=null;
                try {
                    //获取reader对象
                    reader=Resources.getResourceAsReader(resource);
                    //工厂生成器
                    SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
                    //得到工厂
                    factory=builder.build(reader);
                    //得到session
                    session=factory.openSession();
                    list=session.selectList("selectAll");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally{
                        try {
                            reader.close();
                            session.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                return list;
    }

}
View Code

mybatis-config.xml,配置文件,放在src根目录下

mybatis的简单使用mybatis的简单使用
<?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>
    <typeAliases>
        <typeAlias alias="Dept" type="com.entity.Dept"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
        <transactionManager type="JDBC"/>
            <dataSource type="UNPOOLED">
            <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@192.168.40.128:1521:orcl"/>
            <property name="username" value="thunder"/>
            <property name="password" value="admin"/>
            </dataSource>
        </environment>
    </environments>
    
    
    <mappers>
        <mapper resource="com/dao/DeptDaoMapper.xml"/>
    </mappers>
</configuration>
View Code

测试方法:

public static void main(String[] args) {
        DeptDao dd=new DeptDaoImpl();
        Dept dept=new Dept();
        //添加
//        dept.setName("就业部");
//        dept.setLocation("柒角旮旯");
//        dd.insertDept(dept);
        //修改
//        dept.setId(20);
//        dept.setName("城管部");
//        dept.setLocation("你猜,你猜,猜对了我就告诉你");
//        dd.updateDept(dept);
        
        //删除
//        dd.deleteDept(20);
        //查询所有
        List<Dept> list=dd.selAll();
        for (Dept d : list) {
            System.out.println(d.getName()+"---------"+d.getLocation());
        }
    }

以上就是简单的mybatis使用小例子,当然,项目中的使用会复杂很多,重点是DeptDaoMapper.xml文件中sql语句的设计,以及实现类中传值,其他的应该不是很难。