本次分享mybatis入门案例
环境:maven3.5,IDE:idea
1.导入相关依赖,pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.hncj</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<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.18</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2.在resources目录下创建mybatis配置文件,名字可自定义,本案例为mybatis.xml
数据库的信息根据个人的不同进行修改即可
mapper文件的位置根据自身项目结构进行修改
<?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>
<!-- 配置环境 -->
<environments default="mysql">
<!-- 配置mysql的环境 -->
<environment id="mysql">
<!-- 配置事务的类型 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池) -->
<dataSource type="POOLED">
<!-- 配置连接数据库的四个基本信息 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test_mybatis?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="czs0704"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
<mappers>
<mapper resource="com/hncj/mapper/UserDAOMapper.xml" />
</mappers>
</configuration>
3.设计数据库,数据库语句如下
CREATE DATABASE test_mybatis;
USE test_mybatis;
CREATE TABLE `t_user`(
t_id INT(10) PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(30) NOT NULL,
`address` VARCHAR(30) NOT NULL,
`sex` VARCHAR(30) NOT NULL
)ENGINE = INNODB DEFAULT CHARSET=utf8;
4.创建javaBean
package com.hncj.enity;
public class User {
private Integer id;
private String name;
private String address;
private String sex;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
'}';
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
5.写dao层接口,本次演示查询所有的方法
package com.hncj.dao;
import com.hncj.enity.User;
import java.util.List;
public interface Userdao {
List<User> findAll();
void save(User user);
}
6.写mapper配置文件
注意mapper文件的位置与mybatis配置文件中mapper对应的resource路径要相同
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hncj.dao.Userdao">
<select id="findAll" resultType="com.hncj.enity.User">
select * from t_user;
</select>
</mapper>
7.写测试类
package com.hncj.test;
import com.hncj.dao.Userdao;
import com.hncj.enity.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;
public class TestMybatis {
//junit
@Test
public void test() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = factory.openSession();
Userdao mapper = sqlSession.getMapper(Userdao.class);
List<User> users = mapper.findAll();
// for (User user : users) {
// System.out.println(user);
// }
users.forEach(user -> {
System.out.println(user);
});
sqlSession.close();
resourceAsStream.close();
}
}
测试结果如下
项目结构如下: