二、MyBatis的基本使用
2.1、创建项目
??2.1.1、首先自己先创建一个数据库
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');
??2.2.2、创建一个maven项目
??2.2.3、删除src目录(为之后创建新模块做准备)
??2.2.4、在 maven 的 pom.xml 中配置 mybatis 的开发包
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--mysql 驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--MyBatis 的核心库-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
??2.2.5、创建一个模块
??1、编写mybatis的核心配置文件
? ????在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>
??2、编写mybatis工具类
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
??3、编写代码
??(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 mybatis_study.user
</select>
</mapper>
??4、测试
??juint单元测试
public class UserTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user:userList) {
System.out.println(user);
}
sqlSession.close();
}
}
??5、运行结果
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
2.2、总结
? ?MyBatis 使用步骤:
- 在 pom.xml 中加入 mybatis 依赖;
- 创建 mybatis 的核心配置文件(文件名任意);
- 根据表创建对应的实体对象;
- 创建操作接口;
- 根据操作接口创建 Mapper 映射文件;
- 编写测试类。
|