MyBatis_Day01_搭建开发环境

一、新建Maven项目
1)打开IDEA开发工具,点击"Create New Project"
MyBatis_Day01_搭建开发环境_第1张图片
2)选择左侧的"Maven",点Next
MyBatis_Day01_搭建开发环境_第2张图片
3)填入"GroupId"和“ArtifactId”:
MyBatis_Day01_搭建开发环境_第3张图片

4)Finish

MyBatis_Day01_搭建开发环境_第4张图片

Mave项目新建完成后,项目结构如下:

MyBatis_Day01_搭建开发环境_第5张图片
image.png
二、导入依赖,编写实体类及其配置文件:
1)在"pom.xml"中导入需要的依赖:


    4.0.0

    com.zl
    hello_mybatis
    1.0-SNAPSHOT

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    8
                    8
                
            
        
    
    jar

    
        
            org.mybatis
            mybatis
            3.4.5
        
        
            junit
            junit
            4.12
        
        
            mysql
            mysql-connector-java
            5.1.32
        
        
            log4j
            log4j
            1.2.17
        
    

2)src.main.java包下新建com.zl.domain包,然后新建实体类User:
package com.zl.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 + '\'' +
                '}';
    }
}

3)新建com.zl.dao包,然后新建UserDao接口:

新增一个查询方法:

package com.zl.dao;

import com.zl.domain.User;

import java.util.List;

public interface UserDao {
    /**
     * 查询所有用户
     * @return
     */
    List findAll();
}
4)在src.main.resources下新建com.zl.dao包,然后新建UserDao.xml:

注意:resources下的包结构必须和java包下的路径结构一致,还需要注意的是,resources文件夹下无法一次新建com.zl.dao包,只能一级一级的新建目录。




    
    

5)在resources根目录下新建SqlMapConfig.xml主配置文件:



    
    
        
        
            
            
            
            
                
                
                
                
            
        
    

    
        
    


三、新建测试类:
1)在src.test.java包下新建UserTest测试类:
import com.zl.dao.UserDao;
import com.zl.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 org.junit.Test;

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

public class UserTest {

    @Test
    public void testFindAll(){
        try {
            //1.读取配置文件
            InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.创建session工厂
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            //3.创建session
            SqlSession sqlSession = factory.openSession();
            //4.获取dao接口的代理对象
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            //5.执行方法
            List users = userDao.findAll();
            //6.输出语句
            users.stream().forEach(System.out::println);
            //7.释放资源
            sqlSession.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
四、数据库表:

User表:

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` datetime default NULL COMMENT '生日',
`sex` char(1) default NULL COMMENT '性别',
`address` varchar(256) default NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入数据:

insert into 'user'('id','sername','birthday','sex','address'
values (41,'张三','2018-02-27 17:33:08','男','上海'),
(42,'张小宝','2019-03-02 15:09:24','女','上海徐家汇'),
(43,'张大宝','2019-03-04 11:34:12','女','上海长宁'),
(45,'杨过','2019-03-04 14:07:08','男','上海浦东'),
(46,'小龙女','2019-03-07 13:11:56','女','上海'),
(48,'哈哈哈','2018-09-08 12:33:01','女','上海徐家汇');

你可能感兴趣的:(MyBatis_Day01_搭建开发环境)