目录结构
UserDao.java
package com.xxgc.mybatis.dao;
import com.xxgc.mybatis.po.User;
import java.util.List;
//用户表Dao层接口
public interface UserDao {
//查询所有用户
List<User> selectAllUsers();
}
UserDaoMapper.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">
<!--映射一个/绑定一个 dao层接口-->
<mapper namespace="com.xxgc.mybatis.dao.UserDao">
<!--id 对应接口里的方法名 -->
<!--resultType 返回结果类型-->
<select id="selectAllUsers" resultType="com.xxgc.mybatis.po.User">
select * from users;
</select>
</mapper>
User.java
package com.xxgc.mybatis.po;
/* alt + insert 生成构造函数 */
public class User {
private long uId;
private String uName;
private String uPass;
@Override
public String toString() {
return "User{" +
"uId=" + uId +
", uName='" + uName + '\'' +
", uPass='" + uPass + '\'' +
'}';
}
public long getuId() {
return uId;
}
public void setuId(long uId) {
this.uId = uId;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getuPass() {
return uPass;
}
public void setuPass(String uPass) {
this.uPass = uPass;
}
}
MyBatisUtils.java
package com.xxgc.mybatis.util;
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 java.io.IOException;
import java.io.InputStream;
//Mybaits工具类,用来获取sqlSession
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//以下三步通过配置文件来获取sqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
/* ctrl + alt + f 提取/抽取 全局变量 */
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
/* ctrl + alt + v 自动生成返回结果 */
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
mybatis-config.xml
<?xml version="1.0" encoding="utf8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--核心配置文件-->
<configuration>
<!--开启驼峰命名-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--environments 环境 -->
<environments default="development">
<environment id="development">
<!--事务 JDBC的事务-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--serverTimezone=GMT 时区-->
<!--useSSL=true 通道加密-->
<!--useUnicode=true 自定义编码-->
<!--characterEncoding=UTF-8 设置编码为UTF-8-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT&
useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个持久层接口都对应一个mapper文件 mapper文件需要手动注入-->
<mappers>
<mapper resource="com\xxgc\mybatis\dao\UserDaoMapper.xml"/>
</mappers>
</configuration>
UserDaoTest.java
package com.xxgc.mybatis.dao;
import com.xxgc.mybatis.po.User;
import com.xxgc.mybatis.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
//单元测试
//不需要启动整个工程,只需要加载测试所依赖的模块
@Test
public void test(){
//调用工具类 得到sqlSession
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//加载dao层映射
UserDao mapper = sqlSession.getMapper(UserDao.class);
//通过调用接口执行语句
List<User> users = mapper.selectAllUsers();
//循环打印内容
for (User user : users) {
System.out.println("user = " + user);
}
//手动关闭sqlsession(学到了spring过后就不需要了)
sqlSession.close();
}
}
pom.xml
<?xml version="1.0" encoding="utf8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxgc</groupId>
<artifactId>MyBatisDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入依赖-->
<dependencies>
<!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!--junit单元测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--配置文件UTF-8错误-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--配置Maven过滤器,过滤掉xml,让xml也进行编译-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
Sql语句
/*
SQLyog Ultimate v12.09 (64 bit)
MySQL - 5.5.54 : Database - mybatis
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `mybatis`;
/*Table structure for table `users` */
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`u_id` BIGINT(18) NOT NULL AUTO_INCREMENT,
`u_name` VARCHAR(30) NOT NULL COMMENT '用户名',
`u_pass` VARCHAR(100) NOT NULL COMMENT '密码',
PRIMARY KEY (`u_id`)
) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*Data for the table `users` */
insert into `users`(`u_id`,`u_name`,`u_pass`) values (1,'张三','9033241'),(2,'李四','3333241'),(3,'王五','12156241'),(4,'赵六','1111'),(5,'铁蛋','23233241');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
最后修改于 2021-10-11 15:14:02
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付