MyBatis狂神版

在之前学习MyBatis后,发现在使用中有更加方便,维护起来更加方便的方法,因此进行学习

第一步: POM

 

4.0.0

com.west
MyBatis20190930
pom
1.0-SNAPSHOT

    MybatisDemo1



    
    
        mysql
        mysql-connector-java
        5.1.47
    
    
    
        org.mybatis
        mybatis
        3.5.2
    

    
    
        junit
        junit
        4.12
    





    
        
            src/main/resources
            
                **/*.properties
                **/*.xml
            
            true
        
        
            src/main/java
            
                **/*.properties
                **/*.xml
            
            true
        
    




区别:

增加了过滤资源resource,使得我们在实际使用中不用像前两篇介绍的那要要在resource下,一层层的建对应的文件夹


    
        
            src/main/resources
            
                **/*.properties
                **/*.xml
            
            true
        
        
            src/main/java
            
                **/*.properties
                **/*.xml
            
            true
        
    

在Moudle中的子pom最好也这样弄一下

第二步:编写mybatis的核心配置文件

public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;

//放到静态代码块中,一初始就加载
static {
    //获取sqlSessionFactory对象
    try {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。
// SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession() {
    return sqlSessionFactory.openSession();
}

}

第三步:编写实体类

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 + '\'' +
            '}';
}
}

第四步:编写Dao接口

	public interface UserDao {
//查询所有用户
List getUserList();
}

第五步:在dao包下挨着Dao接口编写UerMapper.xml配置文件










第六步测试:

编写测试类

public class Mytest {

private SqlSession sqlSession;

@Before//在@Test注解执行之前调用
public void before() {
    //第一步:获得SqlSession对象
    sqlSession = MybatisUtils.getSqlSession();
}

@Test
public void test() {
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    List userList = userDao.getUserList();

    for (User user : userList) {
        System.out.println(user);
    }
}
@After//在@Test执行完之后调用
public void after() {
    //关闭SqlSession
    sqlSession.close();
}
}

和之前的区别:

1.增加了过滤资源resource
2.提前编写了Util工具类
3.因为加了过滤资源,所以不用像之前那样层层按名字建包

你可能感兴趣的:(学习)