思路:搭建环境---导入MyBatis--编写代码---测试!
一、搭建环境
1、搭建数据库环境:
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user` (
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
engine=innodb和engine=myisam区别:
1、myisam,默认类型。是存储记录和文件的标准类型,支持全文搜索,可以被压缩,与其他引擎相比,有检查和修复表格的大部分工具,不支持事务,不支持外键。强调的是性能,执行速度比较快,可以在不同的系统中迁移。如果执行大量 的SELECT,MyISAM是更好的选择。
2、innodb,支持事务处理等高级处理,支持外键,如果执行大量的select和update语句,出于性能考虑,使用innodb是最好的选择。
INSERT INTO `user` (`id`,`name`,`pwd`) VALUES
(1,'刘能','123'),
(2,'赵四','456'),
(3,'谢广坤','789')
2、idea创建Maven项目:
Snipaste_2021-01-30_13-36-21.png3、idea连接MySQL
Snipaste_2021-01-30_13-39-10.png Snipaste_2021-01-30_13-42-11.png Snipaste_2021-01-30_13-43-59.png二、导入MyBatis
1、导入相关依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
2、防止配置文件不导出或不生效的解决方案:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
三、编写代码
1、项目结构:
Snipaste_2021-01-30_14-49-42.png2、在resources中创建mybatis-config.xml(MyBatis的配置文件)
<?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">
<!-- Mybatis默认的事务管理器就是JDBC,连接池:POOLED-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&
useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper. XML都需要在Mybatis核心配置文件中注册!-->
<mappers>
<package name="com.why.mapper"/>
</mappers>
</configuration>
3、编写工具类(java.com.why.utils.MyUtils.class)
package com.why.utils;
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 java.io.IOException;
import java.io.InputStream;
//工具类
//SqlSessionFactory --》SqlSession
public class MyUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用MyBatis第一步:获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
4、编写实体类(java.com.why.pojo.User.class)
package com.why.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data//生成set,get方法,
@AllArgsConstructor//有参构造
@NoArgsConstructor//无参构造
public class User {
private int id;
private String name;
private String pwd;
}
5、编写映射接口(java.com.why.mapper.UserMapper.interface)
package com.why.mapper;
import com.why.pojo.User;
import java.util.List;
public interface UserMapper {
//查询所有数据
List<User> selectUser();
}
6、编写映射文件(java.com.why.mapper.UserMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.why.mapper.UserMapper">
<select id="selectUser" resultType="com.why.pojo.User">
select *
from mybatis.user;
</select>
</mapper>
四、测试
1、测试(MyTest.class):
package com.why.dao;
import com.why.mapper.UserMapper;
import com.why.pojo.User;
import com.why.utils.MyUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MyTest {
@Test
public void test() {
SqlSession sqlSession = MyUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectUser();
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
}
2、查询结果
Snipaste_2021-01-30_14-48-28.png3、报错utf-8:可以把mybatis-config.xml中的注释删除,也可以把idea的编码统一
网友评论