# ssm-eclipse-chapter06
**Repository Path**: gdkj-group6/ssm-eclipse-chapter06
## Basic Information
- **Project Name**: ssm-eclipse-chapter06
- **Description**: mybatis入门
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 9
- **Created**: 2020-10-14
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# ssm-eclipse-chapter06
#### 介绍
初识mybatis
小组成员:梁嘉铭 谢加成 谢江华 庞博文 吴昊锋
参考代码:教材P88(任务是对**account表操作**)mysql导出
```sql
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`balance` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES ('2', 'whf15', '9527');
INSERT INTO `account` VALUES ('3', 'whfo', '2345');
INSERT INTO `account` VALUES ('4', 'whfa', '1997');
```
#### 安装教程
1. 按小组配置 MySQL 数据源(修改AccountMapper.xml)主要是看sql语句的表明有没有错
```xml
```
2. 修改完AccountMapper.xml文件就去改mybatis-config.xml文件(主要是配置Mapper的位置)
```xml
```
3. 编写测试文件test.java,参考书本p95的测试代码发现(以下代码都是重复的,可以抽象为一个方法)
1、读取配置文件2、根据配置文件构建SqlSessionFactory3、通过SqlSessionFactory创建SqlSession
```java
private SqlSession sqlSession;
private SqlSession getMyBatisSession() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
```
完整测试代码如下
```java
package com.itheima.test;
import java.io.*;
import java.io.InputStream;
import java.util.List;
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.itheima.po.Account;
public class test {
private SqlSession sqlSession;
private SqlSession getMyBatisSession() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
@Test
public void findAccountByIdTest() throws Exception {
// 1、读取配置文件
sqlSession = getMyBatisSession();
// 4、SqlSession执行映射文件中定义的SQL,并返回映射结果
Account account = sqlSession.selectOne("com.itheima.mapper" + ".AccountMapper.findAccountById", 2);
// 打印输出结果
System.out.println(account.toString());
// 5、关闭SqlSession
sqlSession.close();
}
/**
* 根据用户名称来模糊查询用户信息列表
*/
@Test
public void findAccountByNameTest() throws Exception {
SqlSession sqlSession = getMyBatisSession();
// 4、SqlSession执行映射文件中定义的SQL,并返回映射结果
List accounts = sqlSession.selectList("com.itheima.mapper" + ".AccountMapper.findAccountByName",
"t");
for (Account account : accounts) {
// 打印输出结果集
System.out.println(account);
}
// 5、关闭SqlSession
sqlSession.close();
}
}
```
#### 使用说明
1. 数据库数据如下

2. 运行测试方法

3. xxxx#### 知识收获
2. 学习Mapper.xml配置文件应该怎么写(如何修改,知道属性是做什么的)
2. 如何通过eclipse上传到gitee
3. 工具Typora的简单使用