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
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 <environments default="development"> 5 <environment id="development"> 6 <transactionManager type="JDBC" /> 7 <!-- 配置数据源:链接池 --> 8 <dataSource type="POOLED"> 9 <property name="driver" value="com.mysql.jdbc.Driver" /> 10 <property name="url" value="jdbc:mysql://localhost:3306/mybatis_01" /> 11 <property name="username" value="root" /> 12 <property name="password" value="root" /> 13 </dataSource> 14 </environment> 15 </environments> 16 <!-- 注册映射文件 --> 17 <mappers> 18 <mapper resource="cn/com/zfc/model/UserMapper.xml" /> 19 </mappers> 20 </configuration>
3.创建实体类:User.java
1 package cn.com.zfc.model; 2 3 /** 4 * 5 * @title User 6 * @describe User 模型类 Model 层 7 * @author 张富昌 8 * @date 2017年2月25日上午11:42:11 9 */ 10 public class User { 11 private Integer id; 12 private String name; 13 private String sex; 14 15 public User(Integer id, String name, String sex) { 16 super(); 17 this.id = id; 18 this.name = name; 19 this.sex = sex; 20 } 21 22 public User() { 23 super(); 24 } 25 26 public Integer getId() { 27 return id; 28 } 29 30 public void setId(Integer id) { 31 this.id = id; 32 } 33 34 public String getName() { 35 return name; 36 } 37 38 public void setName(String name) { 39 this.name = name; 40 } 41 42 public String getSex() { 43 return sex; 44 } 45 46 public void setSex(String sex) { 47 this.sex = sex; 48 } 49 50 @Override 51 public String toString() { 52 return "User [id=" + id + ", name=" + name + ", sex=" + sex + "]"; 53 } 54 55 }
4.创建映射文件:UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!-- mapper:映射文件,namespace:命名空间 --> 4 <mapper namespace="cn.com.zfc.model.UserMapper"> 5 <!-- 查询一个 User 对象 --> 6 <!-- parameterType:参数类型,resultType:返回结果类型 --> 7 <select id="getOne" parameterType="java.lang.Integer" resultType="cn.com.zfc.model.User"> 8 select * 9 from user 10 where 11 id=#{id} 12 </select> 13 </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
1 package cn.com.zfc.test; 2 3 import java.io.InputStream; 4 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 9 import cn.com.zfc.model.User; 10 11 /** 12 * 13 * @title TestMyBatis 14 * @describe 测试 Mybatis 15 * @author 张富昌 16 * @date 2017年2月25日下午4:18:15 17 */ 18 public class TestMyBatis { 19 public static void main(String[] args) { 20 /* 1、获取 InputStream 对象,加载 Mybatis 配置文件 */ 21 InputStream inputStream = TestMyBatis.class.getClassLoader().getResourceAsStream("mybatis_config.xml"); 22 /* 2、创建 SqlSessionFactoryBuilder,用来获取 SqlSessionFactory 对象 */ 23 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); 24 /* 3、获取 SqlSessionFactory 对象 */ 25 SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); 26 /* 4、获取 SqlSession 对象 */ 27 SqlSession sqlSession = sqlSessionFactory.openSession(); 28 /* 5、定义 statement */ 29 String statement = "cn.com.zfc.model.UserMapper.getOne"; 30 /* 6、执行查询语句 */ 31 User user = sqlSession.selectOne(statement, 1); 32 System.out.println("User:" + user); 33 /* 7、提交事务 */ 34 sqlSession.commit(); 35 /* 8、关闭 Session */ 36 sqlSession.close(); 37 } 38 }