# 第七章:MyBatis核心配置
## 7.1 核心对象
### 1.复制第6章的代码
#### (1)导包 mybatis mysql
```java
org.mybatis
mybatis
3.4.2
mysql
mysql-connector-java
8.0.28
```
#### (2)准备数据
沿用上一章节数据
#### (3)复制实体类
```java
package com.gzh.po;
public class Customer {
private int id;
private String username;
private String jobs;
private String phone;
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 getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", username='" + username + '\'' +
", jobs='" + jobs + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
```
#### (4)复制两个xml文件
mybatis-config.xml:
```java
```
CustomerMapper.xml:
```java
insert into t_customer values (#{id},#{username},#{jobs},#{phone})
update t_customer set username = #{username},jobs = #{jobs},phone = #{phone} where id = #{id}
delete from t_customer where id = #{id}
```
#### (5)测试类
```java
package com.gzh.po;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
public MybatisTest() throws IOException {
}
@Test
public void findCustomerById() {
Customer o = sqlSession.selectOne("CustomerMapper.findCustomerById", 2);
System.out.println(o);
}
```
运行结果如下:
![image-20220412211705869](https://lsky.hhdxw.top/imghub/img/image-20220412211705869.png)
### 2.创建工具类,对外提供SqlSession对象
在 main 下创建一个新的包 com.gzh.utils (通常为工具包),并创建类 MyBatisUtil
```java
package com.gzh;
import com.gzh.po.Customer;
import com.gzh.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class MyBatisTest {
SqlSession sqlSession=MyBatisUtil.getSession();
@Test
public void findCustomerByIdTest(){
Customer o = sqlSession.selectOne("CustomerMapper.findCustomerById", 2);
System.out.println(o);
}
}
```
将 Test 测试类转移到 com.gzh 包下,代码如下:
```java
public class MyBatisTest {
SqlSession sqlSession=MyBatisUtil.getSession();
@Test
public void findCustomerByIdTest(){
Customer o = sqlSession.selectOne("CustomerMapper.findCustomerById", 2);
System.out.println(o);
}
}
```
运行截图:
![image-20220412213246062](https://lsky.hhdxw.top/imghub/img/image-20220412213246062.png)
## 7.2 配置文件
### 1.properties 元素
连接数据库,通过该元素进行内部配置外在化。
(1) 在resources下创建db.properties
```java
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=8520
```
(2) 修改配置文件 2处
1.在文件下添加
```java
```
2.在标签中的 value 属性修改如下:
```java
```
(3)测试运行截图
![image-20220413084844679](https://lsky.hhdxw.top/imghub/img/image-20220413084844679.png)
### 2.typeAliases元素 给实体类起别名
(1)修改CustomerMapper.xml中的:(修改resultType属性)
```java
```
(2)在mybatis-config.xml文件中起别名(添加)
```java
```
(3)测试截图如下:
![image-20220413090235983](https://lsky.hhdxw.top/imghub/img/image-20220413090235983.png)
## 7.3 映射文件
### 1.resultMap元素:将表中字段和实体类成员变量进行对应
(1)修改实体类 username->name
重写getter and setter 以及 toString()
(2)测试结果:当实体类中的成员变量名和表中字段名字不一致时,导致字段值不能被赋值给成员变量
Customer{id=2, name='null', jobs='teacher', phone='13521210112'}
![image-20220413091252095](https://lsky.hhdxw.top/imghub/img/image-20220413091252095.png)
(3)如何解决当实体类中的成员变量名和表中字段名字不一致时,还可以将字段值赋值给成员变量?
resultMap
(4)修改CustomerMapper.xml
添加 resultMap 和 修改 select 中主键的 resultType 属性改为 resultMap
```java
```
---如何将sql语句显示到控制台?
(1)log4j.properties
```java
#Global logging configuration
log4j.rootLogger=ERROR,stdout
#MyBatis logging configuration...
log4j.logger.com=DEBUG
#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
```
(2)导包
```java
log4j
log4j
1.2.17
```
(3)resources 下 创建com,把CustomerMapper.xml文件移动到com下
![image-20220413093052137](https://lsky.hhdxw.top/imghub/img/image-20220413093052137.png)
(4)修改两个xml文件
CustomerMapper.xml
```java
namespace="com.CustomerMapper"
```
mybatis-config.xml
```java
resource="com/CustomerMapper.xml"
```
(5)修改测试方法
```java
Customer o = sqlSession.selectOne("com.CustomerMapper.findCustomerById", 2);
```
(6)运行截图
![image-20220413092737690](https://lsky.hhdxw.top/imghub/img/image-20220413092737690.png)