2019/12/9 ~ 10:黑马Mybatis学习笔记(一)

2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第1张图片
2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第2张图片

环境搭建:

创建实体类和接口
实体类User

package com.itheima.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

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

接口IUserDao

package com.itheima.dao;

import com.itheima.domain.User;

import java.util.List;

/**
 * 用户持久层接口
 */
public interface IUserDao {

    /**
     * 查询所有
     * @return
     */
    List<User> findAll();

}

创建Mybatis主配置文件SqlMapConfig.xml(名字不限定)

创建在src.main.resources目录下





<configuration>
    
    <environments default="mysql">
        
        <environment id="mysql">
            
            <transactionManager type="JDBC">transactionManager>
            
            <dataSource type="POOLED">
                
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            dataSource>
        environment>
    environments>

    
    <mappers>
        <mapper resource="com/itheima/dao/IUserDao.xml">mapper>
    mappers>
configuration>

创建映射配置文件IUserDao.xml(也可以叫 IUserMapper.xml)


        

<mapper namespace="com.itheima.dao.IUserDao">

<select id="findAll" resultType="com.itheima.domain.User">
        select * from user
    select>
mapper>

文件目录结构:

2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第3张图片

Mybatis入门案例

resources目录下创建log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n


在src.test.java.com.itheima.test下创建测试类 MybatisTest
2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第4张图片

package com.itheima.test;


import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Mybatis入门
 */
public class MybatisTest {

    /**
     * 入门案例
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // 1.读取配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");

        // 2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);

        // 3.使用工厂生产SqlSession对象
        SqlSession session = factory.openSession();

        // 4.使用SqlSession创建Dao接口的代理对象
        IUserDao userDao = session.getMapper(IUserDao.class);

        // 5.使用代理对象执行方法
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }

        // 6.释放资源
        session.close();
        in.close();

    }
}

执行结果
2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第5张图片

注解配置

在这里插入图片描述
resouces目录下无需再有IUserDao.xml
2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第6张图片
再接口IUserDao中加入注解@Select

package com.itheima.dao;

        import com.itheima.domain.User;
        import org.apache.ibatis.annotations.Select;

        import java.util.List;

/**
 * 用户持久层接口
 */
public interface IUserDao {

    /**
     * 查询所有
     * @return
     */
    @Select("select * from user")
    List<User> findAll();

}

Mybatis核心配置文件中的配置更改

    <mappers>
        
        
        <mapper class="com.itheima.dao.IUserDao">mapper>
    mappers>

原理图
2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第7张图片


2019/12/9 ~ 10:黑马Mybatis学习笔记(一)_第8张图片

你可能感兴趣的:(Mybatis)