mybatis入门

基本配置

配置文件的详细路径:src/test/java/org/apache/ibatis/submiited/complex_property/Configuration.xml
//通过配置文件获取数据库连接信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
//通过配置信息构建一个SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessioFactoryBuilder.build(reader);
//通过sqlSessionFactory打开一个数据库会话
SqlSession sqlSession = sqlSeesionFactory.openSession();

实体类XML

Ps1:
<mapper namespace="">//namespace属性必须存在不可省略。
<select id="">
如果namespace名不同,则id可以一样,调用的时候,namespace名.id名;若namespace名相同,则id不能一样。
Ps2:
<mapper namespace="">
<resultMap id="">
//同上...
但是注意:resultMap——id可以与select——id一样(即使namespace相同),只要它们的id在各自领域(resultMap或select)范围内唯一即可。
Ps3:<resultMap>的子标签中,如果是主键栏,用<id>子标签,如果是普通栏,用<result>子标签。
Ps4:<select resultMap="resultMap中的id名">。
Ps5:<mappers>中子标签<mapper>的resource属性也是从src根目录开始算起,“.”改为“/”;如果有多个<mapper>可以继续写,个人理解相当于注册。

:附上源码

Configuration.xml

 

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 
  3 <!--
  4 
  5  
  6 
  7        Copyright 2009-2012 the original author or authors.
  8 
  9  
 10 
 11        Licensed under the Apache License, Version 2.0 (the "License");
 12 
 13        you may not use this file except in compliance with the License.
 14 
 15        You may obtain a copy of the License at
 16 
 17  
 18 
 19           http://www.apache.org/licenses/LICENSE-2.0
 20 
 21  
 22 
 23        Unless required by applicable law or agreed to in writing, software
 24 
 25        distributed under the License is distributed on an "AS IS" BASIS,
 26 
 27        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 28 
 29        See the License for the specific language governing permissions and
 30 
 31        limitations under the License.
 32 
 33  
 34 
 35 -->
 36 
 37 <!DOCTYPE configuration
 38 
 39     PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 40 
 41     "http://mybatis.org/dtd/mybatis-3-config.dtd">
 42 
 43  
 44 
 45 <configuration>
 46 
 47 <!--
 48 
 49   <settings>
 50 
 51     <setting name="useGeneratedKeys" value="false"/>
 52 
 53     <setting name="useColumnLabel" value="true"/>
 54 
 55   </settings>
 56 
 57  
 58 
 59   <typeAliases>
 60 
 61     <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/>
 62 
 63   </typeAliases>
 64 
 65 -->
 66 
 67   <environments default="development">
 68 
 69     <environment id="development">
 70 
 71       <transactionManager type="JDBC">
 72 
 73         <property name="" value=""/>
 74 
 75       </transactionManager>
 76 
 77       <dataSource type="UNPOOLED">
 78 
 79         <property name="driver" value="com.mysql.jdbc.Driver"/>
 80 
 81         <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatisdemo"/>
 82 
 83         <property name="username" value="root"/>
 84 
 85         <property name="password" value="root"/>
 86 
 87       </dataSource>
 88 
 89     </environment>
 90 
 91   </environments>
 92 
 93  
 94 
 95   <mappers>
 96 
 97     <mapper resource="com/hp/config/Message.xml"/>
 98 
 99   </mappers>
100 
101  
102 
103 </configuration>
View Code

Message.xml

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

<!--

 

       Copyright 2009-2012 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">

 

<mapper namespace="Message">

 

  <resultMap type="com.hp.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="long" resultMap="MessageResult">

    select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1

  </select>

 

</mapper>
View Code
原文地址:https://www.cnblogs.com/thehugo/p/5597879.html