我们先创建一个新的数据库,名字叫MyBatis
然后在MyBatis
数据库中新建表tb1_employee
:
create table tb1_employee(
id int(11) primary key auto_increment,
last_name varchar(255),
gender char(1),
email varchar(255)
)
然后在里面添加一条数据:
UPDATE `mybatis`.`tb1_employee` SET `last_name`='tom', `gender`='0', `email`='tom@atguigu.com' WHERE `id`='1';
然后在eclipse
中新建java
工程,名字叫MyBatis_HelloWorld
:
在里面新建一个Employee
类:
可以看到,下面的
package com.atguigu.mybatis.bean;
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + "]";
}
}
然后在项目下新建一个lib
文件夹存放jar包:log4j.jar
,mybatis-3.4.1.jar
,mysql-connector-java-5.1.37-bin.jar
三个jar
包
然后还要新建一个conf
文件夹,里面放配置文件,先把log4j.xml
放进去
注意新建的conf
文件夹是源码包文件夹(Source Folder
),不是普通的文件夹:
最后记得把jar
包导一下,全选jar
包右键点Build Path
,建好后如下图:
然后呢再去新建一个xml
文件放到conf
文件夹下,名字叫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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="1230" />
</dataSource>
</environment>
</environments>
<!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
我们来新建一个EmployeeMapper.xml
sql映射文件:
<?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="com.atguigu.mybatis.EmployeeMapper">
<!--
namespace:名称空间;指定为接口的全类名(我们还没有定义接口,先随便定一个吧)
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
-->
<select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee">
select * from tb1_employee where id = #{id}
</select>
</mapper>
然后新建一个MyBatisTest
测试类:
package com.atguigu.mybatis.test;
import static org.junit.Assert.*;
import java.io.IOException;
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.atguigu.mybatis.bean.Employee;
public class MyBatisTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息
* 2、sql映射文件;配置了每一个sql,以及sql的封装规则等。
* 3、将sql映射文件注册在全局配置文件中
* 4、写代码:
* 1)、根据全局配置文件得到SqlSessionFactory;
* 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查
* 一个sqlSession就是代表和数据库的一次会话,用完关闭
* 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。
*
* @throws IOException
*/
@Test
public void test() throws IOException {
// 2、获取sqlSession实例,能直接执行已经映射的sql语句
// sql的唯一标识:statement Unique identifier matching the statement to use.
// 执行sql要用的参数:parameter A parameter object to pass to the statement.
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
//推荐使用namespace+select的id这种方式:"com.atguigu.mybatis.EmployeeMapper.selectEmp"
Employee employee = openSession.selectOne(
"com.atguigu.mybatis.EmployeeMapper.selectEmp", 1);
System.out.println(employee);
} finally {
openSession.close();
}
}
}
运行测试结果如下:
可以看到其他的都正常,就lastName
是null
.为什么呢?因为我们之前新建数据库中的是last_name
,我们的Employee
中是lastName
,所以会是null
最快的解决办法:(查询的时候,给字段名字不对应的那一列起别名)
<select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee">
select id,last_name lastName,email,gender from tb1_employee where id = #{id}
</select>
结果: