Eclipse:Luna Service Release 1 (4.4.1)
MyBatis:org.mybatis.mybatis-3.2.8
JDK:1.7.0_79
4.0.0
com.queen.mybatis
MyBatisDemo
0.0.1-SNAPSHOT
war
junit
junit
4.10
test
org.mybatis
mybatis
3.2.8
mysql
mysql-connector-java
5.1.32
org.slf4j
slf4j-log4j12
1.6.4
注意:该文件中主要是连接数据库的信息,驱动,url,用户名,密码以及映射文件,将我们写好的sql映射文件(UserMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 。
log4j.rootLogger=DEBUG,A1
log4j.logger.com.queen = DEBUG
log4j.logger.org.mybatis = DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
主要记录MyBatis运行过程中产生的一些日志信息。
/**
* @since 2017-07-15
* @author queen
* 定义一个UserMapper接口
*
*/
public interface UserMapper {
/**
* 根据ID查找用户
* @param id
* @return
*/
public User findUserById(int id);
}
/**
* @since 2017-07-15
* @author queen
* 定义一个Java类
*
*/
public class User {
// ID,唯一性
private String id;
// 登录ID
private String loginId;
// 用户名
private String userName;
// 角色
private String role;
// 备注
private String note;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public String toString() {
return "User [id=" + id + ", loginId=" + loginId + ", userName="
+ userName + ", role=" + role + ", note=" + note + "]";
}
}
/**
* @since 2017-07-15
* @author queen
* 编写测试Java类
*
*/
public class MyBatisTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
User user = openSession.selectOne(
"com.queen.mybatis.mapper.UserMapper.findUserById", "1");
System.out.println(user);
} finally {
openSession.close();
}
}
}
2017-07-15 16:52:43,862 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Opening JDBC Connection
2017-07-15 16:52:44,305 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] Created connection 706757729.
2017-07-15 16:52:44,305 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a204461]
2017-07-15 16:52:44,309 [main] [com.queen.mybatis.mapper.UserMapper.findUserById]-[DEBUG] ==> Preparing: select id, loginId, userName, role, note from t_user where id = ?
2017-07-15 16:52:44,372 [main] [com.queen.mybatis.mapper.UserMapper.findUserById]-[DEBUG] ==> Parameters: 1(String)
2017-07-15 16:52:44,419 [main] [com.queen.mybatis.mapper.UserMapper.findUserById]-[DEBUG] <== Total: 1
User [id=1, loginId=Sabot, userName=萨博, role=革命军, note=革命军的二号人物,同艾斯一样,为蒙奇·D·路飞的哥哥(义兄)。]
2017-07-15 16:52:44,420 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a204461]
2017-07-15 16:52:44,421 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a204461]
2017-07-15 16:52:44,421 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] Returned connection 706757729 to pool.
我们会很明显的发现这是一个完整的JDBC执行过程,打开连接,创建连接,将事务设置成false,准备SQL,SQL赋参数,执行SQL,事务提交,关闭连接,返回连接。其实MyBatis就是将这些动作封装,对于开发人员只需要关注SQL。
MyBatis整个调用过程:
根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象有数据源一些运行环境信息
sql映射文件;配置了每一个sql,以及sql的封装规则等。
将sql映射文件注册在全局配置文件中
写代码:
①根据全局配置文件,SqlSessionFactoryBuilder得到SqlSessionFactory
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
②使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
③一个sqlSession就是代表和数据库的一次会话,用完关闭
④使用sql的唯一标志来告诉MyBatis执行哪个sql,sql都是保存在sql映射文件中的。
至此,我们关于MyBatis教程之简单的入门介绍完毕。只做了简单的查询动作。