开发流程:
**1. 创建Java Project
2. 导入jar包(依赖包,核心包,数据驱动包,日志包)
3. 创建sqlMapConfig.xml,创建log4j.properties(日志)
4. 创建PO对象
5. 创建映射文件
6. 编写 测试类**
1.创建Java Project
2.导入jar包(依赖包,核心包,数据驱动包,日志包)
3.创建sqlMapConfig.xml,创建log4j.properties(日志)
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
</configuration>
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4. 创建PO对象
User.java
package com.sise.domain;
import java.sql.Date;
public class User {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
5. 创建映射文件
User.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:做sql隔离 -->
<mapper namespace="test">
<!-- 根据id获取用户信息 -->
<select id="findUserById" parameterType="int"
resultType="com.sise.domain.User">
select * from user where id = #{id}
</select>
</mapper>
*id:sql语句的唯一标识
parameterType:传入参数的类型
resultType:返回结果集类型
#{}
:占位符*
在核心配置文件中(SqlMapConfig.xml)加载User.xml
<!-- 引入User.xml -->
<mappers>
<mapper resource="User.xml"/>
</mappers>
6. 编写测试类
package com.sise.test;
import java.io.InputStream;
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 com.sise.domain.User;
public class UserTest {
@Test
public void testFindById()throws Exception{
String resource ="SqlMapConfig.xml";
//通过流的方法将核心配置文件加载进来
InputStream inputStream = Resources.getResourceAsStream(resource);
//通过核心配置输入流创建会话工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
//通过会话工厂创建会话
SqlSession openSession = factory.openSession();
//所调用的sql语句:namespace.ID
User user = openSession.selectOne("test.findUserById",1);
System.out.println(user);
//释放资源
openSession.close();
}
}
搞定收工!!!