MyBatis配置及实现CURD操作

fmms 13年前

MyBatis 的前身就是 iBatis 。是一个数据持久层(ORM)框架。

每个MyBatis应用程序主要都是使用SqlSessionFactory实例的,一个SqlSessionFactory实例可以通过SqlSessionFactoryBuilder获得。SqlSessionFactoryBuilder可以从一个xml配置文件或者一个预定义的配置类的实例获得。


一、前提条件

建立表:admin_login_id

数据库结构

int log_id 主键

int admin_id

varchar login_time

domain:

package com;    import java.util.Date;    /**   * @function :    * @author   :jy   * @company  :万里网   * @date     :2011-7-31   */  public class Logs {   public int log_id;   public int admin_id;   public Date login_time;      public int getLog_id() {    return log_id;   }   public void setLog_id(int log_id) {    this.log_id = log_id;   }   public int getAdmin_id() {    return admin_id;   }   public void setAdmin_id(int admin_id) {    this.admin_id = admin_id;   }   public Date getLogin_time() {    return login_time;   }   public void setLogin_time(Date login_time) {    this.login_time = login_time;   }   @Override   public String toString() {    return "Logs [log_id=" + log_id + ", admin_id=" + admin_id + ", login_time=" + login_time + "]";   }     }

二、下面开始配置MyBatis

1.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="Logs" type="com.Logs"/>   </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/manage?characterEncoding=UTF-8"/>      <property name="username" value="root"/>      <property name="password" value=""/>     </dataSource>    </environment>   </environments>      <mappers>    <mapper resource="com/LogsMapper.xml" />   </mappers>  </configuration>

2.实体映射文件

<?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.LogsDAO" >      <select id="queryAllLogs" resultType="com.Logs">      SELECT        log_id,        admin_id,        login_time      FROM admin_login_log    </select>        <select id="queryStudentById" resultType="Logs" parameterType="int">     SELECT        log_id,       admin_id,       login_time     FROM admin_login_log     WHERE       log_id=#{log_id}    </select>        <insert id="addLog" parameterType="Logs">     INSERT INTO admin_login_log       (log_id, admin_id, login_time)   VALUES    (log_id, #{admin_id}, #{login_time})    </insert>        <delete id="deleteLogById" parameterType="int">     DELETE FROM      admin_login_log     WHERE      log_id=#{log_id}    </delete>        <select id="queryLogsByAdminId" parameterType="string" resultType="Logs">     SELECT       log_id,      admin_id,      login_time     FROM       admin_login_log     WHERE      admin_id like #{admin_id}    </select>  </mapper>

 

三、建立DAO接口

package com;    import java.util.List;    /**   * @function :    * @author   :jy   * @company  :万里网   * @date     :2011-7-31   */  public interface LogsDAO {   public void addLog(Logs log);      public void deleteLogById(int id);      public void updateStudentById(Logs log);      public List<Logs> queryAllLogs();      public Logs queryStudentById(int id);      public List<Logs> queryLogsByAdminId(String admin_id);  }


四、建立SqlSessionFactory实例,并实现LogsDAO接口

package com;    import java.io.IOException;  import java.io.Reader;  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;    /**   * @function :    * @author   :jy   * @company  :万里网   * @date     :2011-7-31   */  public class LogsDAOImpl implements LogsDAO {   private static SqlSession session = null;   private static LogsDAO mapper = null;      static{    String resoure = "com/ibatisConfiguration.xml";    Reader reader = null;    try {     reader = Resources.getResourceAsReader(resoure);    } catch (IOException e) {     e.printStackTrace();    }        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);    session = sqlSessionFactory.openSession();    mapper = session.getMapper(LogsDAO.class);        try {     reader.close();    } catch (IOException e) {     e.printStackTrace();    }   }      /* (non-Javadoc)    * @see com.LogsDAO#addLog(com.Logs)    */   @Override   public void addLog(Logs log) {    mapper.addLog(log);        session.commit();   }     /* (non-Javadoc)    * @see com.LogsDAO#deleteLogById(int)    */   @Override   public void deleteLogById(int id) {    mapper.deleteLogById(id);        session.commit();   }     /* (non-Javadoc)    * @see com.LogsDAO#updateStudentById(com.Logs)    */   @Override   public void updateStudentById(Logs log) {       }     /* (non-Javadoc)    * @see com.LogsDAO#queryAllLogs()    */   @Override   public List<Logs> queryAllLogs() {    List<Logs> logs = mapper.queryAllLogs();        return logs;   }     /* (non-Javadoc)    * @see com.LogsDAO#queryStudentById(int)    */   @Override   public Logs queryStudentById(int id) {    Logs logs = mapper.queryStudentById(id);        return logs;   }     /* (non-Javadoc)    * @see com.LogsDAO#queryLogsByAdminId(java.lang.String)    */   @Override   public List<Logs> queryLogsByAdminId(String admin_id) {    return mapper.queryLogsByAdminId(admin_id);   }    }

 

五、测试

package test;    import java.io.IOException;    import com.Logs;  import com.LogsDAOImpl;    /**   * @function :    * @author   :jy   * @company  :万里网   * @date     :2011-7-31   */  public class Test {     /**    * @param args    * @throws IOException     */   public static void main(String[] args) throws IOException {    // TODO Auto-generated method stub    LogsDAOImpl logs = new LogsDAOImpl();      //  System.out.println(logs.queryAllLogs());      //  Logs log = logs.queryStudentById(2);      //  System.out.println(log);            //addlog    Logs log = new Logs();        log.setAdmin_id(1);        log.setLog_id(10);        log.setLogin_time(new java.util.Date());        logs.addLog(log);        //deleteLog    logs.deleteLogById(5);        System.out.println(logs.queryLogsByAdminId("%1%"));   }    }