系列文章目录
本章将部署简单的mybatis示例。
文章目录
一、环境说明
该示例都是在idea软件下搭建maven完成的。
需要入门的同学知道以下学习工具
mybatis官方中文文档
建立一个Mysql新表
create table stuEmp
(
id int null,
name varchar(255) null,
password varchar(255) null,
constraint stuEmp_pk
unique (id)
);
我的数据建立在demo数据库下的stuEmp表中。
不同的位置记得修改mybatis-config.xml文件中的url地址。
新建idea项目
1. 新建一个maven项目
2. 下一步,给当前项目命名
3. 点击finish,完成构建。
二、文档结构
三、Mybatis原理简述
四、文件内容
StuDao.java
package com.lqz.jdbc.dao;
import com.lqz.jdbc.entity.Student;
import java.util.List;
public interface StuDao {
List<Student> query();
}
StuDaoImpl.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">
<mapper namespace="com.lqz.jdbc.dao.StuDao">
<select id="query" resultType="com.lqz.jdbc.entity.Student">
select id,name,password from stuEmp
</select>
</mapper>
Student.java
package com.lqz.jdbc.entity;
public class Student {
private Integer id;
private String name;
private String password;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
mybatis-config.xml
<?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="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/demo?useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/StuDaoImpl.xml"/>
</mappers>
</configuration>
TestMybatis.java
package com.lqz.jdbc;
import com.lqz.jdbc.dao.StuDao;
import com.lqz.jdbc.entity.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 javax.xml.stream.FactoryConfigurationError;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestMybatis {
@Test
public void test1(){
InputStream resourceAsStream = null;
try {
resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
System.out.println("没有找到mybatis文件");
}
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
StuDao mapper = sqlSession.getMapper(StuDao.class);
List<Student> query = mapper.query();
query.forEach(System.out::println);
SqlSession.close();
}
}
pom.xml
<?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>org.example</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java/com/lqz/jdbc</directory>
<includes>
<include>*/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
写在后面
这里只介绍对mybatis还不是很了解的同学们,如果有什么不懂的,请私信,欢迎大家一起讨论。