1. 程式人生 > >Mybatis的下載並搭建核心架構

Mybatis的下載並搭建核心架構

import arc 配置信息 Coding src namespace clas lease oid

下載網站:

   https://github.com/mybatis/mybatis-3/releases

1.引入web項目下引入mybatis.jar

  引入到WebRoot/WEB-INF/lib下

2.配置 mybatis 核心配置文件(這個在mybatis源碼包中)

  mybatis-3-mybatis-3.4.5\src\test\java\org\apache\ibatis\submitted\complex_property\Configuration.xml

  將Configuration.xml復制到web項目中

   配置核心配置文件



<configuration
> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"
/> <property name="url" value="jdbc:mysql://localhost:3306/micro_message"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper
resource="com/daoan/config/sqlxml/Message.xml"/> </mappers> </configuration>

 

3.得到sqlSession對象

  

import java.io.IOException;
import java.io.Reader;

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

/**
 * 訪問數據庫類
 */
public class DBAccess {
    public SqlSession getSqlSession() throws IOException {
        //通過配置文件獲取數據庫連接信息
        Reader reader = Resources.getResourceAsReader("com/daoan/config/Configuration.xml");
        //通過配置信息構建SqlSessionFactoy
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //通過SqlSessionFactoy打開一個數據庫會話
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}

4.使用Mybatis框架來連接連接sql以及執行sql語句

/**
 * 和Message表相關的數據庫操作
 */
public class MessageDao {
    /**
     *  根據查詢條件,查詢消息列表
     */
    public List<Message> queryMessageList(String command,String description) {
        DBAccess dbAccess = new DBAccess();
        List <Message> messageList = new ArrayList<Message>();
        SqlSession sqlSession = null;
        try {
            sqlSession = dbAccess.getSqlSession();
            //通過sqlSession執行SQL語句
            messageList = sqlSession.selectList("Message.queryMessageList");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(sqlSession !=null){
                sqlSession.close();
                sqlSession = null;
            }
        }
        return messageList;
    }
    
    public static void main(String[] args) {
        MessageDao messageDao = new MessageDao();
        messageDao.queryMessageList("", "");
    }
}

5.Message.xml配置文件 (取出數據庫某個字段的值放在java的對象的屬性裏面)

<?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="Message">

  <resultMap type="com.daoan.bean.Message" id="MessageResult">
    <id column="Id" jdbcType="INTEGER" property="id"/>
    <result column="Command" jdbcType="VARCHAR" property="command"/>
    <result column="Description" jdbcType="VARCHAR" property="description"/>
    <result column="Context" jdbcType="VARCHAR" property="context"/>
  </resultMap>

  <select id="queryMessageList" resultMap="MessageResult">
    select Id,Command,Description,Context from message where 1=1
  </select>
</mapper>

Mybatis的下載並搭建核心架構