项目——通过自动回复机器人学Mybatis(二)

xiaoxiao2021-02-28  64

现在就要引入Mybatis了

Mybatis是github上的开源项目,下载地址:https://github.com/mybatis/mybatis-3/releases 下载下来后,将mybatis的jar包复制进项目lib目录 最好下载个mybatis的源码包,里面有mybatis的源码和演示代码(test),test里有配置文件 核心配置文件路径:源码目录\src\test\java\org\apache\ibatis\submitted\complex_property\Configurtion.xml 项目中创建com.csdn.config包,放入Configurtion.xml 配置datasource标签 <?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> <!-- <settings> <setting name="useGeneratedKeys" value="false"/> <setting name="useColumnLabel" value="true"/> </settings> <typeAliases> <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/> </typeAliases> --> <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://127.0.0.1:3306/we_chat"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--<mappers> <mapper resource="com/imooc/config/sqlxml/Message.xml"/> <mapper resource="com/imooc/config/sqlxml/Command.xml"/> <mapper resource="com/imooc/config/sqlxml/CommandContent.xml"/> </mappers> --> </configuration> 现在介绍Mybatis中负责与数据库交互且能执行sql语句的对象——sqlSession sqlSession的作用: 1.向SQL语句传入参数 2.执行SQL语句 3.获取执行SQL语句的结果 4.事务的控制 如何获得sqlSession呢? 1.通过配置文件获取数据库连接相关信息 2.通过配置文件构建sqlSessionFactory 3.通过sqlSessionFactory打开数据库会话

创建com.csdn.db包,创建DBAccess类返回sqlSession

/** * 访问数据库类 * */ public class DBAccess { public SqlSession getSqlSession() throws Exception{ //通过配置文件获取数据库连接信息 Reader reader = Resources.getResourceAsReader("com/csdn/config/Configurtion.xml"); //通过配置信息构建sqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //通过sqlSessionFactory打开一个数据库会话 SqlSession sqlSession = sessionFactory.openSession(); return sqlSession; } }

现修改MessageDao中关于连接数据库的jdbc代码

public List<Message> queryMessageList(String command,String description){ DBAccess dbAccess = new DBAccess(); SqlSession sqlSession=null; try { sqlSession=dbAccess.getSqlSession(); //通过sqlSession执行SQL语句 } catch (Exception e) { e.printStackTrace(); }finally{ if(sqlSession!=null){ sqlSession.close(); } } return null; }

加入Mapper映射文件

核心配置文件路径:源码目录\src\test\java\org\apache\ibatis\submitted\complex_property\Configurtion.xml 与Configurtion.xml一起的还有个User.xml,它就是Mapper映射文件 新建com.csdn.config.sqlxml包,放入User.xml,重命名为Message.xml,并做相应修改 <?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.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" parameterType="com.imooc.bean.Message" resultMap="MessageResult"> select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE <where> <if test="command != null and !"".equals(command.trim())"> and COMMAND=#{command} </if> <if test="description != null and !"".equals(description.trim())"> and DESCRIPTION like '%' #{description} '%' </if> </where> </select> <delete id="deleteOne" parameterType="int"> delete from MESSAGE where ID = #{_parameter} </delete> <delete id="deleteBatch" parameterType="java.util.List"> delete from MESSAGE where ID in( <foreach collection="list" item="item" separator=","> #{item} </foreach> ) </delete> </mapper> 回到MessageDao sqlSession就是如此执行sql语句的,参数"Message"为Message.xml中的namespace,"queryMessageList"为select标签的id

回到Mapper文件

如果你的查询语句的返回值是实体类,则添加resultMap属性,属性值为resultMap标签的id

resulrMap标签的子标签:

<id/>主键使用此标签,其余使用<result/> column属性为数据库中的列名,jdbcType一般为数据库中的数据类型的的大写(除了int,可通过java.sql.Types.查询),property为对应的实体类中的属性 最后在核心配置文件Configurtion中,配置该Mapper文件 <mappers> <mapper resource="com/imooc/config/sqlxml/Message.xml"/> </mappers>

动态SQL拼接

MessageDao中的queryMessageList(String command,String description)是有参数的 如何修改selectList = sqlSession.selectList("Message.queryMessageList");呢? 首先selectList只接收一个参数,因此我们可以将command和description封装进一个Message对象,再将此对象作为参数传入 如下: public List<Message> queryMessageList(String command,String description){ DBAccess dbAccess = new DBAccess(); SqlSession sqlSession=null; List<Message> selectList =new ArrayList<Message>(); try { sqlSession=dbAccess.getSqlSession(); Message message = new Message(); message.setCommand(command); message.setDescription(description); //通过sqlSession执行SQL语句 selectList = sqlSession.selectList("Message.queryMessageList",message); } catch (Exception e) { e.printStackTrace(); }finally{ if(sqlSession!=null){ sqlSession.close(); } } return selectList; } Message.xml修改: parameterType修改 <select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult">

动态拼接:

很类似与JSTL是不是,不过test中的可不是EL表达式而是OGNL表达式,OGNL功能强大不仅可以调用传进来的对象的属性值(如:command),还可以调用java方法(如:equals()和trim()) 注意!!!:test中为了防止代表空字符串的""与test=""的双引号冲突,用""替换双引号。还有在动态添加的sql语句中有一个#{},在mybatis解析到这的时候,会用"?"替换#{},再用传进来的属性给"?"赋值
转载请注明原文地址: https://www.6miu.com/read-66204.html

最新回复(0)