1.创建maven工程
2.配置pom.xml
<!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency>
3.创建实体类
与数据库字段一一对应
4.配置mybatis-config配置文件
<?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">
<!-- Mybatis核心配置文件 -->
<configuration>
<properties resource="database.properties"></properties>
<typeAliases>
<package name="com.smbms.pojo"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
头部声明为固定格式
configuration 配置:(可以不写其中的一部分;但顺序不能乱)
properties 可以配置在Java 属性配置文件中
settings 修改 MyBatis 在运行时的行为方式
typeAliases 为 Java 类型命名一个别名(简称)
typeHandlers 类型处理器
objectFactory 对象工厂
plugins 插件
environments 环境
environment 环境变量
transactionManager 事务管理器
dataSource 数据源
mappers 映射器
typealiases元素:作用:配置类型别名,通过与MyBatis的SQL映射文件相关联,减少输入 多余的完整类名,以简化操作。
<typeAliases>
<typeAlias alias="User" type="cn.smbms.pojo.User"/>
</typeAliases>
5.创建mapper文件夹下的 sql映射文件
<?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.smbms.mapper.UserMapper">
<select id="getUserByAll" resultType="User">
SELECT USERNAME,USERCODE USERID,USERPASSWORD,ADDRESS,USERROLE FROM SMBMS_USER
</select>
</mapper>
6.创建mapper接口
与映射文件对应
方法名与id对应(相同)
返回类型与resultType对应且相同
7.创建测试类
package com.smbms.test;
import com.smbms.mapper.UserMapper;
import com.smbms.pojo.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.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @author: zjl
* @datetime: 2024/3/23
* @desc:
*/
public class UserTest {
private static SqlSessionFactory factory;
private static InputStream is;
private UserMapper userMapper;
static {
//1.定义mybatis核心配置文件的路径
String path = "mybatis/mybatis-config.xml";
//2.转字节流接收
try {
is = Resources.getResourceAsStream(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.创建SqlSessionFactory对象
factory = new SqlSessionFactoryBuilder().build(is);
}
@Test
public void getUserByAll() throws IOException {
SqlSession sqlSession = factory.openSession();
// int count = sqlSession.select("aaaa.getl");
// System.out.println(count);
sqlSession.close();
//count = sqlSession.selectOne("aaaa.getCount");
is.close();
}
@Test
public void getCountForMapper() throws IOException {
SqlSession sqlSession = factory.openSession();
userMapper = sqlSession.getMapper(UserMapper.class);
int count = userMapper.getCount();
System.out.println(count);
sqlSession.close();
is.close();
}
}
8.