具体操作如下
准备工作 :
新建一个普通maven-webapp的maven项目
删除src目录
- 1.准备一个数据库
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT(20) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES (1,'**','123456'),(2,'##','zxcvbn'),(3,'##','123456')
- 2.导入maven依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
- 3.第一步配置在ReSource目录下 ==编写mybatis的核心配置文件 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">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${pwd}"/>
</dataSource>
</environment>
</environments>
configuration>
- 4.编写一个mybatis工具类操作数据库(注意包不要导错)
package com.yang.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import java.io.IOException;
import java.io.InputStream;
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 getSession(){
return sqlSessionFactory.openSession();
}
}
- 5.配置了一个实体类,引入了Lombok
package com.yang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
**Lombok的依赖**
```java
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
- 6.持久层mapper的操作接口
package com.yang.mapper;
import com.yang.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
}
-
7.在resource下创建与main目录对应的目录,创建UserMapper 对应的xml配置文件,并编写SQL语句
注意点:1、namespace 绑定对应的接口, 2、具体操作(CRUD)表情对应的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"> <!-- 原来我们都是编写具体的执行sql -->
<mapper namespace="com.yang.mapper.UserMapper">
<select id="getUserList" resultType="com.yang.pojo.User">
select * from user;
</select>
</mapper>
- 8.这个配置文件一定要注册到Mybatis的配置文件MyBatis-config.xml中
<mappers>
<mapper resource="com/yang/mapper/RMapper.xml"/>
<!--<mapper class="com.yang.mapper.UserMapper"/>-->
<!--<package name="com.yang.mapper"/>-->
</mappers>
<!--
resource 资源对应的路径,必须相同。
class 接口对应的路径,必须在同一个包中同一个路径下。
-->
- 9.测试
import com.yang.mapper.UserMapper;
import com.yang.pojo.User;
import com.yang.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class Test {
public static void main(String[] args) { // 如何获取接口
SqlSession session = MyBatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
}