- 编写 MyBatis 工具类;
package com.cunyu.utils;
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;
/**
-
@author : cunyu
-
@version : 1.0
-
@className : MyBatisUtils
-
@date : 2020/7/14 11:38
-
@description : MyBatis 工具类
*/
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = “mybatis-config.xml”;
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
-
@param
-
@return
-
@description 获取 SqlSession 连接
-
@date 2020/7/14 11:46
-
@author cunyu1943
-
@version 1.0
*/
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
- 接下来创建实体类;
package com.cun.pojo;
/**
-
@author : cunyu
-
@version : 1.0
-
@className : User
-
@date : 2020/7/14 11:55
-
@description : User 实体类
*/
public class User {
p