mybatis的配置

需要先去下载mybatis,网址为:点击打开链接


点击releases


把mybatis-3.4.6.zip和Source code(这个是mabatis源码)都下载下来;

解压后现在mybatis的文件夹中找到mybatis的驱动文件


就是选中的这个添加到你项目的jar包中通常是lib文件夹;

然后再去解压后的source code文件夹中进入这个目录:

            C:\mybatis\mybatis-3-mybatis-3.4.6\src\test\java\org\apache\ibatis\submitted\complex_property

找到这个文件


把他(configuration.xml)添加都你项目的配置文件夹中

最后打开这个配置文件


这是里面的基本配置,把driver、url、username、password配置完成就行了;

<property name="url" value="jdbc:mysql://127.0.0.1:3306/micro/micro_message"/>
其中micro是项目的数据库名字,micro_message是需要连接的表名;

最后是怎么使用

    1,先在项目中创建一个DBAccess类,用来访问数据库类:

    

package db;

import java.io.Reader;

/**
 * 访问数据库类,也就是数据库访问层
 */

public class dbAccess {
    public SqlSession getSqlSession(){
        //通过配置文件获取数据连接信息
        Reader reader =  Ressource.getResourceAsReader("config/Configuration.xml"); //路径是从src之后开始
        //通过配置信息构建一个SqlSessionFactory
        SqlSesionFactory sqlSesionFactory =  new SqlSessionFactoryBuider().build(reader);
        //通过sqlsessionFactory打开一个数据库会话
        SqlSession sqlSession = new sqlSessionFactory.openSession();
        return sqlSession;
    }
}

2.在Dao层里使用dbAccess类:

    

 DBAccess dbAccess = new DBAccess();
        SqlSession sqlSession = null;
        try{
            sqlSession = dbAccess.getSqlSession();
            //通过sqlSession执行SQL语句

        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if(sqlSession != null)
            sqlSession.close();
        }

这样基本架构就搭建完毕;

下面是mysql的基本配置与执行;

去Source code中的C:\mybatis\mybatis-3-mybatis-3.4.6\src\test\java\org\apache\ibatis\submitted\complex_property

文件夹中找到这个文件


然后将User.xml复制到你的配置文件夹中,进行下列修改

<?xml version="1.0" encoding="UTF-8"?>
<!--

       Copyright 2009-2016 the original author or authors.

       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.

-->
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace  相当于java中的package-->
<mapper namespace="Message">

  <resultMap type="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="content" jdbcType="BOOLEAN" property="content"/>
  </resultMap>

  <!--select语句的配置,id名唯一性-->
  <select id="queryMessageList"  resultMap="UserResult">
    SELECT id,command,description,content FROM message WHERE 1=1
  </select>

</mapper>

到这里就mybatis的基本配置就结束了!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值