首页 > 代码库 > MyBatis之MyBatis环境搭建
MyBatis之MyBatis环境搭建
MyBatis之MyBatis环境搭建
一、MyBatis开发环境搭建
1.引入Jar包
①MyBatis
mybatis-3.4.1.jar
ant-1.9.6.jar
ant-launcher-1.9.6.jar
asm-5.1.jar
cglib-3.2.4.jar
commons-logging-1.2.jar
javassist-3.21.0-GA.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
ognl-3.1.12.jar
slf4j-api-1.7.22.jar
slf4j-log4j12-1.7.22.jar
②MySQL:
mysql-connector-java-5.1.7-bin.jar
2.创建配置文件:mybatis_config.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据源:链接池 -->
<dataSource type="POOLED">
<property name="driver" value="http://www.mamicode.com/com.mysql.jdbc.Driver" />
<property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/mybatis_01" />
<property name="username" value="http://www.mamicode.com/root" />
<property name="password" value="http://www.mamicode.com/root" />
</dataSource>
</environment>
</environments>
<!-- 注册映射文件 -->
<mappers>
<mapper resource="cn/com/zfc/model/UserMapper.xml" />
</mappers>
</configuration>
3.创建实体类:User.java
package cn.com.zfc.model;
/**
*
* @title User
* @describe User 模型类 Model 层
* @author 张富昌
* @date 2017年2月25日上午11:42:11
*/
public class User {
private Integer id;
private String name;
private String sex;
public User(Integer id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
}
4.创建映射文件:UserMapper.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">
<!-- mapper:映射文件,namespace:命名空间 -->
<mapper namespace="cn.com.zfc.model.UserMapper">
<!-- 查询一个 User 对象 -->
<!-- parameterType:参数类型,resultType:返回结果类型 -->
<select id="getOne" parameterType="java.lang.Integer" resultType="cn.com.zfc.model.User">
select *
from user
where
id=#{id}
</select>
</mapper>
二、基本使用流程
1、获取 InputStream 对象,加载 Mybatis 配置文件
InputStream inputStream = Test01.class.getClassLoader().getResourceAsStream("mybatis_config.xml");
2、创建 SqlSessionFactoryBuilder,用来获取 SqlSessionFactory 对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
3、获取 SqlSessionFactory 对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
4、获取 SqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
5、定义 statement
String statement = "cn.com.zfc.model.UserMapper.getOne";
6、执行查询语句
User user = sqlSession.selectOne(statement, 1);
System.out.println("User:" + user);
7、提交事务
sqlSession.commit();
8、关闭 Session
sqlSession.close();
三、测试MyBatis环境:TestMyBatis.java
package cn.com.zfc.test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.com.zfc.model.User;
/**
*
* @title TestMyBatis
* @describe 测试 Mybatis
* @author 张富昌
* @date 2017年2月25日下午4:18:15
*/
public class TestMyBatis {
public static void main(String[] args) {
/* 1、获取 InputStream 对象,加载 Mybatis 配置文件 */
InputStream inputStream = TestMyBatis.class.getClassLoader().getResourceAsStream("mybatis_config.xml");
/* 2、创建 SqlSessionFactoryBuilder,用来获取 SqlSessionFactory 对象 */
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
/* 3、获取 SqlSessionFactory 对象 */
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
/* 4、获取 SqlSession 对象 */
SqlSession sqlSession = sqlSessionFactory.openSession();
/* 5、定义 statement */
String statement = "cn.com.zfc.model.UserMapper.getOne";
/* 6、执行查询语句 */
User user = sqlSession.selectOne(statement, 1);
System.out.println("User:" + user);
/* 7、提交事务 */
sqlSession.commit();
/* 8、关闭 Session */
sqlSession.close();
}
}
MyBatis之MyBatis环境搭建