添加依赖pom.xml
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 提供mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
mybatis配置
<?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>
<settings>
<!-- changes from the defaults -->
<setting name="lazyLoadingEnabled" value="false"/>
</settings>
<typeAliases>
<!--这里给实体类取别名,方便在mapper配置文件中使用 -->
<typeAlias alias="user" type="com.example.entity.User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db1"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<!--这里添加的是执行CRUD操作的接口对应的配置文件(xml文件) -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
实体类User
public class User {
private Long id;
private String userId;
private String userName;
private Integer age;
private String email;
private Date addTime;
private Date modifyTime;
private Integer isDel;
...
}
mapper配置
<?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.example.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.entity.User">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="user_id" jdbcType="VARCHAR" property="userId"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
<result column="age" jdbcType="INTEGER" property="age"/>
<result column="email" jdbcType="VARCHAR" property="email"/>
<result column="add_time" jdbcType="TIMESTAMP" property="addTime"/>
<result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime"/>
<result column="is_del" jdbcType="TINYINT" property="isDel"/>
</resultMap>
<sql id="Base_Column_List">
id, user_id, user_name, age, email, add_time, modify_time, is_del
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user
where id = #{id,jdbcType=BIGINT}
</select>
</mapper>
Mapper
import com.example.entity.User;
public interface UserMapper {
User selectByPrimaryKey(Long id);
}
测试
import com.example.entity.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.Before;
import org.junit.Test;
import java.io.InputStream;
public class UserMapperTest {
// 指定MyBatis配置文件
final String RESOURCE = "mybatis-config.xml";
SqlSessionFactory sessionFactory = null;
@Before
public void before() {
try {
// 1、指定MyBaties配置文件
InputStream inputstream = Resources.getResourceAsStream(RESOURCE);
// 2、创建SqlSessionFactory()
sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test() {
// 3、获取SqlSession
SqlSession session = sessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.selectByPrimaryKey(1L);
System.out.println(String.format("userId:%s,userName:%s,age:%s", user.getId(), user.getUserName(), user.getAge()));
}
}