Mybatis入门
作者:互联网
MyBatis入门使用
1 环境
- jdk
- idea/eclipse
- mysql
- maven
2 步骤
2.1 在数据库创建表
create table stu
(
stu_id int not null primary key,
stu_name varchar(10) null
);
pom文件导入mybatis,mysql依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
2.2 创建实体类
2.2.1 在src/main/java下创建domain包
2.2.2 在domain包下创建Student类
package com.yhb.domain;
/**
* @author YangHaiBo
* @create 2019-09-20 18:43
*/
public class Student {
private Integer studentId;
private String studentName;
//已省略get/set方法
}
2.3 创建映射类
2.3.1 创建dao包
2.3.2 创建StudentMapper接口
package com.yhb.dao;
import com.yhb.domain.Student;
import java.util.List;
public interface StudentMapper {
List<Student> searchAll();
Student searchById(Integer id);
}
2.4 创建映射文件
2.4.1 在src/main/resources/创建mapper文件夹
2.4.2 创建StudentMapper.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">
<mapper namespace="com.yhb.dao.StudentMapper">
<resultMap type="com.yhb.domain.Student" id="BaseMap">
<!-- 以此把实体类的字段和sql的语句中的字段对应 -->
<id javaType="Integer" column="stu_id" property="studentId"/>
<result javaType="String" column="stu_name" property="studentName"/> </resultMap>
<select id="searchById" resultType="com.yhb.domain.Student" parameterType="Integer"> select * from stu where stu_id = #{id}
</select>
<select id="searchAll" resultMap="BaseMap">
select * from stu </select>
</mapper>
2.5 创建mybatis配置文件
2.5.1 在src/resources/下创建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">
<!-- 配置读取database.properties里面连接数据库信息 -->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"/>
<dataSource type="pooled">
<property name="url" value="jdbc:mysql://localhost:3306/testdata?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC"/>
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<!-- 建立与stumapper.xml映射文件的对应关系 -->
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
2.6 编写测试类
2.6.1 创建测试类
在src/main/test/下创建mybatis包
2.6.2 编写测试类
package mybatis;
public class TestMybatis {
@Test
public void testSearch() throws IOException {
//1.先定义一个变量,先保存mybatis-config.xml文件
String resource = "mybatis-config.xml";
//2.产生api对象,产生会话工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
//3.通过会话工厂得到会话
SqlSession session = sf.openSession();
//4.通过session得到需要的数据
StudentMapper studentMapper = session.getMapper(StudentMapper.class); List<Student> stuList = studentMapper.searchAll();
//5.遍历
System.out.println(stuList == null ? "stuList is null" : "stuList is not null"); for (Student stu : stuList) {
System.out.println(stu != null ? stu.toString() : "stu null");
}
}
}
2.7 结果
显示出查询出的数据库内结果
标签:stuList,入门,创建,mybatis,stu,Student,Mybatis,null 来源: https://www.cnblogs.com/guming1125/p/11560226.html