CREATE DATABASE `mybatis_study`;
USE `mybatis_study`;
CREATE TABLE `user`(
`user_id` INT(20) NOT NULL PRIMARY KEY,
`user_name` VARCHAR(30) DEFAULT NULL,
`password` VARCHAR(30) DEFAULT NULL
)ENGINE = INNODB DEFAULT CHARSET = utf8;
INSERT INTO `user` (`user_id`,`user_name`,`password`) VALUES
(1, '张三', '123456'),
(2, 'admin', 'admin'),
(3, 'root', 'root');
junit
junit
4.11
test
mysql
mysql-connector-java
5.1.47
org.mybatis
mybatis
3.5.7
在resources中创建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核心配置文件-->
<configuration>
<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/mybatis_study?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
//sqlSessionFactory-->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用MyBatis第一步:获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
* SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
*/
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
(1)实体类
//实体类
public class User {
private Integer user_id;
private String user_name;
private String password;
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"user_id=" + user_id +
", user_name='" + user_name + '\'' +
", password='" + password + '\'' +
'}';
}
}
(2)Mapper接口
public interface UserMapper {
List<User> getUserList();
}
(3)创建 UserMapper.xml 映射文件
该映射文件主要用于: 描述对象与关系模型间的映射(推荐自动映射<对象中的属性和表中的字段名一致>) 和SQL 映射(在其中编写 SQL 语句)。
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定一个对应的Mapper接口-->
<mapper namespace="com.baidu.mapper.UserMapper">
<!--select查询语句-->
<select id="getUserList" resultType="com.baidu.pojo.User">
select * from user
</select>
</mapper>
juint单元测试
public class UserTest {
@Test
public void test(){
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//执行SQL
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user:userList) {
System.out.println(user);
}
//关闭SqlSession
sqlSession.close();
}
}
User{user_id=1, user_name='张三', password='123456'}
User{user_id=2, user_name='admin', password='admin'}
User{user_id=3, user_name='root', password='root'}
进程已结束,退出代码为 0
MyBatis 使用步骤: