- ♂️个人主页:杯咖啡
- 进步是今天的活动,明天的保证!
- ✨目前正在学习:SSM框架,算法刷题
- 希望大家多多支持一起进步呀!
昨天我们了解了框架相关知识,并且导入依赖配置了核心文件,今天就可以开始写代码测试了。
项目专栏Mybatis详解01里面有学习路线哦,想看的小伙伴可以看看哦。
不多说直接开始。
这里需要注意:很多小伙伴链接不成功,这个时候要修改一下自己的驱动版本,尽量与数据库版本一致
jdbc.driverclassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=*******
这里有个小细节,新版数据库驱动的类型不要写错 com.mysql.cj.jdbc.Driver
设置完这个就可以连接到数据库了。
这里直接附上,第一次用来测试成功,先跑通,后面的文章会详细将各个标签的作用和属性学习一遍
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties">properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverclassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="StudentMapper.xml">mapper>
mappers>
configuration>
package com.longlong.pojo;
import java.util.Objects;
/**
* @Author DELL longlong
* @Date 2022/7/1 14:51
* @Version 1.0
* @Function 实体类
*/
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
public Student() {
}
public Student(String name, String email, Integer age) {
this.name = name;
this.email = email;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(email, student.email) && Objects.equals(age, student.age);
}
@Override
public int hashCode() {
return Objects.hash(id, name, email, age);
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ll">
<select id="getAll" resultType="com.longlong.pojo.Student" >
select *
from student
select>
mapper>
最激动人心的时刻,要进行测试了
package com.longlong.test;
import com.longlong.pojo.Student;
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;
/**
* @Author DELL longlong
* @Date 2022/7/3 19:32
* @Version 1.0
* @Function
*/
public class Mytest {
@Test
public void testGetAll() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List<Student> list = sqlSession.selectList("ll.getAll");
// list.forEach(student -> System.out.println(student));
for (Student student : list){
System.out.println(student);
}
System.out.println("OK");
sqlSession.close();
}
}
运行结果
显示这样就表示我们的测试是成功的,祝贺一下吧!!!
今天我们完成了第一次使用mybatis框架,并且成功了。开心呢!!!
下面就要深入学习配置文件的标签和属性,运行过程,作用!!!
本次源码放在代码仓库gitee,自取链接
喜欢的小伙伴留下三连再走吧,自己也可以试试哦。
Wrong cannot afford defeat but right can.
错误经不起失败,但是真理确实不怕失败。-《泰戈尔》