欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis总结(1)

程序员文章站 2024-01-30 10:47:04
...

一、domain对象

package com.person.test.mybatis.domain;

/**
 * Copyright: Copyright (c) 2017/10/20
 * Description:
 * CLASS_NAME: User
 * PACKAGE_NAME: com.person.test.mybatis
 * version: v1.0.0
 * author: BooKING
 * date: 2017/10/20 9:04
 */
public class User {
    private String id;
    private String username;
    private String password;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

二、配置文件
1.con.xml配置文件
MyBatis总结(1)

<?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>
    <!--引用properties中数据库的信息,这里是连接数据库的信息放在db.properties文件中-->
    <properties resource="com/person/test/mybatis/db.properties"/>

    <!--设置别名,这样这个包下面的domain对象就可以直接使用类名,不需要写出完整的路径-->
    <typeAliases>
        <package name="com.person.test.mybatis.domain"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 注册userMapper的信息,这里定义的是你的domain对象,分别与你的表相对应-->
    <mappers>
        <mapper resource="com/person/test/mybatis/mapping/userMapper.xml"/>
        <mapper resource="com/person/test/mybatis/mapping/classMapper.xml"/>
        <mapper resource="com/person/test/mybatis/mapping/teacherMapper.xml"/>
    </mappers>

</configuration>

2.domain的映射文件

<?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">
<!--namespce 为了唯一标识这个映射文件,习惯上是包名加文件名-->
<mapper namespace="mybatis.domain.userMapper">
<!--id 为了标识每个语句,进行引用;parametereType 是传入的入参类型,reultType 返回类型,如果没用使用别名,就要写全User的路径-->
    <select id="getUser" parameterType="String" resultType="User">
        select * from mybatis where id = #{id}
    </select>

    <insert id="addUser" parameterType="User">
        insert into mybatis values(#{id},#{username},#{password})
    </insert>

三、代码实现功能

public class TestMybatis {
    public static void main(String args[]) throws Exception {
        String resource = "/com/person/test/mybatis/conf.xml";
        //使用类加载器加载mybatis的配置文件

        InputStream inputStream = new FileInputStream("src/com/person/test/mybatis/conf.xml");
        //构建session工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 创建能执行映射文件中sql的sqlSession,true是默认提交事务,false需要手动提交事务
        SqlSession session  = sessionFactory.openSession(true);

        String statement = "mybatis.domain.userMapper.getUser";
        User user = session.selectOne(statement,"1010");
        session.close();
        System.out.println(user);
}