Mybatis笔记(一)-----Mybatis基础xml配置文件解释

1、预先的配置

(1)创建一个数据库(MySQL)mybatis 和一张 user 表,脚本如下:

#创建 mybatis 数据库
create database mybatis;
#使用数据库
use mybatis;
#创建表,有 id, name, age
create table user (
  ID INT(11) PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(18) DEFAULT NULL,
  AGE INT(11) DEFAULT NULL
)

(2)基本的 PO(持久化对象)

package com.test.mybatistest;
public
class User { private int ID; private String name; private int age;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> User(<span style="color: rgba(0, 0, 255, 1)">int</span> id, String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age){
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.ID =<span style="color: rgba(0, 0, 0, 1)"> id;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setID(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> ID) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.ID =<span style="color: rgba(0, 0, 0, 1)"> ID;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAge(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getID() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ID;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getAge() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> age;
}

}

2、xml 配置文件

(1)User.xml (位置:直接在 src 下)

<?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="com.test.springtest.User">
    <select id="GetUserByID" parameterType="int" resultType="com.test.springtest.dao.MUser">
        select * from `student` where id = #{id}
    </select>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">insert
        </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="saveUser"</span><span style="color: rgba(255, 0, 0, 1)"> parameterType</span><span style="color: rgba(0, 0, 255, 1)">="com.test.springtest.User"</span><span style="color: rgba(255, 0, 0, 1)">
        useGeneratedKeys</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    insert into student(NAME,AGE) values (#{name},#{age})
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">insert</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</mapper>

具体解释:

<mapper namespace="com.test.springtest.User"

         为这个 mapper 指定一个唯一的 namespace,它习惯上设置为:“包名 +sql 映射文件名”,这样可以保值名的唯一。

<select id="GetUserByID" parameterType="int" resultType="com.test.mybatistest.User">

       id:这个 select 语句的 id
       parameterType:指定查询是传入的参数类型
       resultType:即返回结果集的类型,这理指定为 User 类型

select * from `student` where id = #{id}   

       一条 select 语句

useGeneratedKeys="true"  

       使用数据库的自动增长策略

(2)mybatis-config.xml (位置:直接在 src 下)

<?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>
<!--环境配置,连接的数据库,这里使用的是 MySQL-->
<environments default="mysql">
<environment id="mysql">
<!--指定事务管理的类型,这里简单使用 Java 的 JDBC 的提交和回滚设置-->
<transactionManager type="JDBC"></transactionManager>
<!--dataSource 指连接源配置,POOLED 是 JDBC 连接对象的数据源连接池的实现-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybbs"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 这是告诉 Mybatis 区哪找持久化类的映射文件,对于在 src 下的文件直接写文件名,
如果在某包下,则要写明路径, 如:com/mybatistest/config/User.xml-->

<mapper resource="User.xml"></mapper>
</mappers>
</configuration>

 Mybatis 的配置文件默认命名为 mybatis-config.xml,程序运行前需要加载这个文件;

第一行是 XML 声明,指定字符集;

<configuration>Mybatis 配置文件的根元素为

<environment>:用来配置 Mybatis 的环境,即连接的数据库。

<transationManager>:配置 Mybatis 的事务管理

<dataSource>:数据源,Mybatis 推荐使用数据源(维持着一个连接池,而不用每次连接都开启一个连接)来管理数据库连接。而 dataSource 下的属性相信你已经懂了(如果你学过 JDBC 的配置)

3、运行测试

 

package com.test.springtest.test;
public class Test{
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取配置文件</span>
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化mybatis,创建SqlSessionFactory类实例</span>
    SqlSessionFactory sqlSessionFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(is);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建Session实例</span>
    SqlSession session =<span style="color: rgba(0, 0, 0, 1)"> sqlSessionFactory.openSession();
    User user </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> User(6, "张三", 33<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">插入数据</span>
    session.insert("com.test.springtest.User.saveUser"<span style="color: rgba(0, 0, 0, 1)">,user);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提交事务</span>

session.commit();
//关闭 Session
session.close();
}
}

 

说明:

         程序先读取配置文件,然后通过 SqlSession 对象来操作持久化对象。

        为了使用 Mybatis 进行持久化操作,可以把上述程序总结如下操作步骤:

        1)编写持久化类(e.g. User)和其持久化操作的 Mapper.xml(e.g. User.xml),并在 mapper.xml 中定义 SQL 语句

        2)获取 SqlSessionFactory

        3)获取 SqlSession

        4)用面向对象的方式操作数据库(session.insert()、session.update()、session.select()。。。。)

        5)提交事务,关闭 SqlSession