1-搭建maven环境
下载maven包,进入配置文件;01-修改仓库位置;02-修改为国内镜像;
<localRepository>D:\program_install\java2\apache-maven-3.6.0\maven_jar</localRepository>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
设置系统环境变量和测试:
02-引入mybatis相关坐标
<dependencies>
<!-- mybatis相关依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
03-编写实体类
public class User implements Serializable {
//属性私有
private Integer id;
private String username;
private Date birthday;
private String address;
private String sex;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "=================User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
04-创建mapper接口
public interface UserDao {
//查询所有用户
public List<User> findAll();
//通过id查询用户
public User findById(int id);
}
05-编写mybatis映射配置 文件
<?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.qcby.dao.UserDao">
<select id="findAll" resultType="com.qcby.entity.User">
SELECT * FROM t_user
</select>
</mapper>
06-编写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="mysql">
<environment id="mysql">
<!--配置数据库事务管理 mysql对事物默认不提交 手动提交-->
<transactionManager type="JDBC"></transactionManager>
<!--数据库连接池,可以创建很多连接 放在池子里面,下次用直接拿-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_yglh?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root!"/>
</dataSource>
</environment>
</environments>
<!-- 主配置文件 将映射文件加载到主配置文件中-->
<mappers>
<mapper resource="mapper/userDao.xml"></mapper>
</mappers>
</configuration>
07-编写测试方法
package com.qcby.test;
import com.qcby.dao.UserDao;
import com.qcby.entity.User;
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 候苑博
* @date 2021/2/25 11:53
* @description
*/
public class Demo1 {
/**
* 第一种findAll方法
* @throws IOException
*/
@Test
public void run() throws IOException {//异常快捷键ctrl+alt+T
//主配置文件 搞一下 读取配置文件 加载配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//得到sqlSession的工厂对象 创建sqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
//通过sqlSessionFactory对象创建session对象
SqlSession session = factory.openSession();
//通过session对象调用findAll方法
List<Object> list = session.selectList("com.qcby.dao.UserDao.findAll");
//遍历结果
for (Object o : list) {//list.o
System.out.println(o);
}
//关闭资源
session.close();
inputStream.close();
}
@Test
public void testFindAll() throws IOException {
InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
SqlSession sqlSession = factory.openSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);//代理对象 java底层反射出来的
List<User> list = mapper.findAll();
for (User user : list) {
System.out.println(user);
}
//关闭
sqlSession.close();
stream.close();
}
}
结果显示: