使用MyBatis操作数据库连接

来源:互联网 发布:linux虚拟机home扩容 编辑:程序博客网 时间:2024/06/05 20:39


这篇文章是接我上篇文章改写的,最好结合上一篇文章一起阅读

一.MyBatis环境搭建

1.第一个问题当然是导包

下载地址:https://github.com/mybatis/mybatis-3/releases
在其中将源码包也下载了
复制其中的jar(打开第一层中的,就一个,不是lib里面的)到项目的lib下面,然后右键,add path

2.文件的载入

其次新建一个包(com.imooc.config),将源码包中的Configuration.xml复制进去(mybatis-3-mybatis-3.4.1\src\test\java\org\apache\ibatis\submitted\complex_property),这个是MyBatis的配置文件,建立数据库的连接,接着在同样的路径中找到User.xml,并复制到另一个包中(可在config下的sql中),这个是与数据库的映射文件,改为Message.xml
3.文件修改
如下,将其中的driver改成JDBC驱动,url,用户名,密码,注意将<typeAliases>注释掉,具体不知道,但是知道这个会影响到后面sqlSession的获取


<dataSource type="UNPOOLED">        <property name="driver" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://127.0.0.1:3306/imooc"/>        <property name="username" value="root"/>        <property name="password" value="root"/>      </dataSource>


然后是改写映射文件

Message.xml

因为只是查询,所以其他的代码都删除掉,就留下这些代码

解释一下:

在Java类中调用的时候使用如下

messageList=sqlSession.selectList("Message.queryMessageList");

其中的selectList就对应<select>的id,即为入口,前面的Message是指进入到mapper中的select的id,然后通过resultMap的值关联到<resultMap>

在<resultMap>的column中的ID对应的是<select>中sql语句的ID,而property对应的是type为com.imooc.bean.Message类中的属性,意思就是将从数据库中获取到的记录赋给Message对象,并返回集合(我猜测其内部的指令肯定和上一篇直接使用JDBC是一样的)

<mapper namespace="Message">  <resultMap type="com.imooc.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="VARCHAR" property="content"/>  </resultMap>  <select id="queryMessageList"  resultMap="MessageResult">    select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE where 1=1  </select>


最后别忘了在配置文件Configuration.xml中声明

<mappers>    <mapper resource="com/imooc/config/sql/Message.xml"/>  </mappers>

二.获取SqlSession会话操作数据库

public  SqlSession getSqlSession() throws Exception{//通过配置文件获取数据库连接信息,获取的是路径Reader reader=Resources.getResourceAsReader("com/imooc/config/Configuration.xml");//通过配置信息构建一个SqlSessionFactorySqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);//通过sqlSessionFactory打开一个数据库会话SqlSession sqlSession=sqlSessionFactory.openSession();return sqlSession;}

然后在控制层中去获取

List<Message> messageList=new ArrayList<>();DBAccess dbAccess=new DBAccess();SqlSession sqlSession=null;sqlSession=dbAccess.getSqlSession();//通过sqlSession执行SQL语句messageList=sqlSession.selectList("Message.queryMessageList");//关闭sessionsqlSession.close();

三.条件查询,实现JDBC原始中的拼接操作

这里控制层中传入两个参数,由于接收的限制,只能接收一个参数,这里将Message对象传进去

List<Message> messageList=new ArrayList<>();DBAccess dbAccess=new DBAccess();SqlSession sqlSession=null;sqlSession=dbAccess.getSqlSession();//因为selectList中的参数除了id外,只能再加一个,这样只能将两个参数封装起来Message message=new Message();message.setCommand(command);message.setDescription(description);//通过sqlSession执行SQL语句messageList=sqlSession.selectList("Message.queryMessageList",message);sqlSession.close();

在Message.xml中要使用到OGNL表达式,MyBaits中的xml使用的不是EL运算符,而是OGNL

其中xml中的引号用&quot

OGNL的特有操作符and相当于Java中的&&

OGNL支持Java的对象方法

<select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult">    select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE where 1=1    <if test="command!=null and !"".equals(command.trim())">      and COMMAND=#{command}    </if>    <if test="description!=null and !"".equals(description.trim())">      and DESCRIPTION like concat('%',#{description},'%')    </if>  </select>

这样就完成了


最后要提的一点是:进行模糊查询的语句如下

mysql :LIKE CONCAT('%',#{empname},'%' ) 或者 LIKE CONCAT('%',‘${empname}’,'%' )

oracle:LIKE '%'||#{empname}||'%'



0 0
原创粉丝点击