| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
fmms
13年前发布

MyBatis配置及实现CURD操作

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%"));   }    }



 本文由用户 fmms 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1333250718843.html
MyBatis MyBatis3 持久层框架