IDEA创建maven普通工程
1.创建本地数据库
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE USER(
id INT NOT NULL PRIMARY KEY,
NAME VARCHAR(32) NOT NULL,
pwd VARCHAR(32)NOT NULL
);
INSERT INTO USER VALUES(1,"fanlan","123456");
INSERT INTO USER VALUES(2,"zhangsan","123456");
INSERT INTO USER VALUES(3,"lisi","123456");
SELECT * FROM USER
2.导入mybatis相关jar包
<!--mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--jdbc依赖-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--junit依赖-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
编写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核心配置文件-->
<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/mybits?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--每一个mapper.xml都需要在mybatis核心配置文件中注册-->
<mappers>
<mapper resource="com/fanlan/Dao/UserMapper.xml"/>
</mappers>
</configuration>
4.编写mybatis工具类
package com.fanlan.Utils;
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 sqlSessionFactory;
//随着类的加载而加载
static {
try {
//加载配置
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取sqlSession对象
public static SqlSession getSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
5.创建实体类User
package com.fanlan.pojo;
public class User {
private int id;
private String name; //对应数据库
private String pwd;
public User(String s, String xiaofanlan, String pwd) {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
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 String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
6.编写Mapper接口类
package com.fanlan.Dao;
import com.fanlan.pojo.User;
import java.util.List;
public interface UserDAO {
//查询全部用户
List<User> selectUser();
}
7.编写mapper.xml文件
namespace
<?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">
<!--命名空间绑定一个对应的DAo与map-->
<mapper namespace="com.fanlan.Dao.UserDAO">
<!--查询全部用户-->
<select id="selectUser" resultType="com.fanlan.pojo.User">
<!--id指的就是写接口实现类中重写的类名( selectUser)-->
select * from mybits.user
</select>
</mapper>
8.编写junit测试类
public class UserMapperTest {
@Test
public void test(){
//获取SqlSession对象
SqlSession session = MybatisUtils.getSession();
//执行sql
UserDAO mapper = session.getMapper(UserDAO.class);
//全限定名调用映射语句
//List<User> users = mapper.selectUser("com.fanlan.Dao.UserDAO.selectUser()");
//
List<User> users = mapper.selectUser();
for (User user : users) {
System.out.println(user);
}
//关闭 session
session.close();
}
}
9.运行成功,查询出数据库中的数据就是ok👌
出现Maven资源导出问题?
org.apache.ibatis.binding.BindingException: Type interface com.fanlan.Dao.UserDAO is not known to the MapperRegistry.
解决方案: