mysql数据库中建表
drop table if exists tb_user;
create table tb_user(
id int primary key auto_increment,
username varchar(20),
password varchar(20),
gender char(1),
addr varchar(30)
);
INSERT INTO tb_user VALUES (1, 'zhangsan', '123', '男', '北京');
INSERT INTO tb_user VALUES (2, '李四', '234', '女', '天津');
INSERT INTO tb_user VALUES (3, '王五', '11', '男', '西安');
IDEA中新建项目模块
pom文件中导入依赖
<!--mybatis 依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
在resource目录下添加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>
<typeAliases>
<package name="com.itheima.pojo"/>
</typeAliases>
<!--
environments:配置数据库连接环境信息。可以配置多个environment,通过default属性切换不同的environment
-->
<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:///mybatis?useSSL=false"/> <!-- 连接路径为本地mysql-->
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载sql映射文件-->
<mapper resource=""/>
<!--Mapper代理方式-->
<!-- <package name="UserMapper.xml"/>-->
</mappers>
</configuration>
创建 pojo类
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String addr;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() { //重写toString()方法
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
创建sql表映射的xml文件,通常命名由表名开头。
<?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">
<!--
namespace:名称空间
-->
<mapper namespace="UserMapper"> <!-- 此处自定义命名空间 -->
<select id="selectAll" resultType="com.mybatis.User"> <!-- 此处id为该表的唯一标识 resultType为查询返回类型包下目录形式的pojo类 -->
select *
from tb_user;
</select>
<!-- <select id="selectById" resultType="user"> 官方原版结构
select *
from tb_user where id = #{id};
</select>-->
</mapper>
在mybatis-config.xml文件中加载刚创建的sql表映射的xml文件
<!--加载sql映射文件-->
<mapper resource="UserMapper.xml"/>
写核心测试代码
public static void main(String[] args) throws IOException {
//1. 获取SqlSessionFactory 注意此处的代码配置不需要记忆和理解,官方编写好直接复制使用即可
String resource = "mybatis-config.xml"; //写为mybatis-config.xml配置文件的的路径
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
List<User> users = sqlSession.selectList("UserMapper.selectAll"); //此处传sql命名空间下的唯一标识
//4.打印输出查看
System.out.println(users);
//5. 释放资源
sqlSession.close();
}
运行查看输出结果