前期准备:
一张表(!已经连接数据库!我的数据库叫book,表名为user,有id、name、age三列)
项目结构:(log4j可以没有)
1,创建一个空的Maven项目
然后在pom.xml中导入依赖(mybatis需要3个依赖,junit测试依赖包、mybatis依赖包、SQL连接依赖包)
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
2.com包下创建3个包mapper(存放接口)、pojo(存放实体类)、utils(可以把创建工厂的前期过程放在这个包里面,目的是简化代码)
(1)pojo下创建User类
package com.pojo;
public class User {
public int id;
public String name;
public int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
(2)mapper下创建UserMapper接口
package com.mapper;
import com.pojo.User;
public interface UserMapper {
public User findUserById(int id);
}
(3)utils下创建MyBatisUtils
package com.utils;
import com.mapper.UserMapper;
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 MybatisUtils {
private static SqlSessionFactory factory;
static {
try {
InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
factory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession=factory.openSession();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession getSession(){
return factory.openSession();
}
}
3.在resource下创建数据库连接池db.properties
#mysql
db.username=root
db.password=123456
db.jdbcUrl=jdbc:mysql://127.0.0.1:3306/book?useSSL=false&characterEncoding=utf-8
db.driverClass=com.mysql.cj.jdbc.Driver
!!注意数据库密码和数据库名字
4.在resource的mapper目录下创建UserMapper.xml文件,里面存放SQL语句(如果没有mapper目录,就自己创建)!!!!名称空间一定要是接口的全路径且SQL语句id要的接口的方法名保持一致
<?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.mapper.UserMapper">
<!-- 结果集映射,一般用于select查询的结果映射-->
<resultMap id="UserInfoMap" type="com.pojo.User">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
</resultMap>
<parameterMap id="parameterMap" type="com.pojo.User">
<parameter property="id" resultMap="id"></parameter>
<parameter property="name" resultMap="name"></parameter>
</parameterMap>
<!-- sql语句 id是该语句的唯一标识-->
<select id="findUserById" resultMap="UserInfoMap">
select *
from user
where id = #{id}
</select>
</mapper>
5.在resource下创建mybatis的核心配置文件mybatis-config.xml(log4j可有可无)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--加载属性文件db.properties-->
<properties resource="db.properties"></properties>
<!--<settings></settings>-->
<settings>
<!-- 开启驼峰映射-->
<setting name="mapUnderscoreToCamelCase" value="ture"/>
</settings>
<!-- 扫描包,别名映射-->
<typeAliases>
<package name="com.pojo"/>
</typeAliases>
<!--环境配置-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${db.driverClass}"/>
<property name="url" value="${db.jdbcUrl}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<!-- sql语句的xml映射-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
6.在test中的目录java下创建com.test的包,然后在里面创建一个Test的测试类(如果@org.junit.Test报错那把它改为@Test即可)
package com.test;
import com.mapper.UserMapper;
import com.pojo.User;
import com.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
public class Test {
@org.junit.Test
public void findUserById(){
SqlSession session= MybatisUtils.getSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
User user=userMapper.findUserById(2);
System.out.println(user);
session.commit();
session.close();
}
}
最后运行