MyBatis-Helloworld

时间:2021-09-09 04:26:42

一、依赖配置

1.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>com</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies> <build>
<plugins>
<!-- 指定jdk -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.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-mysql">
<environment id="development-mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.8.136:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> <!-- mappers:将 SQL 映射文件注册到全局配置中 -->
<mappers>
<!-- 引用类路径下的 SQL 映射文件 -->
<mapper resource="com/dao/MyUserMapper.xml"/>
</mappers>
</configuration>

二、测试

以 myuser 表为例

MyBatis-Helloworld

1.对应实体类

public class MyUser {
private Integer id;
private String name;
private Integer age; 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 Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "MyUser{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}

2.对应接口

import com.bean.MyUser;

public interface MyUserMapper {
public MyUser selectMyUser(Integer id);
}

3.接口对应的映射文件

<?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">
<!-- namespace 对应接口文件的全路径 -->
<mapper namespace="com.dao.MyUserMapper">
<!--id 与接口文件方法名对应,resultType 返回类型的全路径-->
<select id="selectMyUser" resultType="com.bean.MyUser">
select * from myuser where id = #{id}
</select>
</mapper>

4.将映射添加到 MyBatis 配置文件 mybatis-config.xml

上面已添加

5.测试方法

import com.bean.MyUser;
import com.dao.MyUserMapper; 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; public class Main {
public static void main(String[] args) {
SqlSession session = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession(); // 不用接口的方式
// MyUser myUser = session.selectOne("com.dao.MyUserMapper.selectMyUser",1); MyUserMapper mapper = session.getMapper(MyUserMapper.class);
MyUser myUser = (MyUser) mapper.selectMyUser(1);
System.out.println(myUser);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
}

MyBatis-Helloworld


官方文档

GitHub 地址

相关文章