2、第一个mybatis程序
思路:搭建环境--->导入mybatis--->编写代码--->测试!
2.1、搭建环境
搭建数据库
CREATE DATABASE `mybatis`;
USE `mybatis`;
INSERT INTO `user`(`id`,`name`,`pwd`)VALUES
(1,'张三','123456'),
(2,'李四','123456'),
(3,'王五','123545');
新建项目
- 新建一个普通的maven项目
- 删除src目录(成为父工程)
2.2、导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
2.3、创建一个模块
- 编写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>
<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?useSSL=true&useUnicode=true&CharacterEncoding=UTF-8}"/>
<property name="username" value="${root}"/>
<property name="password" value="${root}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
- 编写mybatis工具类
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//获取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();
}
}
2.4、编写代码
- 实体类
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
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 + '\'' +
'}';
}
}
- Dao接口
public interface UserDao {
List<User> getUserList();
}
- 接口实现类由原来的UserDaoImpl变为现在的UserMapper
<?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.kuang.dao.UserDao">
<select id="getUserList" resultType="com.kuang.pojo.User">
select * from mybatis.user
</select>
</mapper>
2.5、测试
- Junit测试
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
这里的测试问题真的是百出!!!把我折腾了有半天
- 一定要记住在核心配置中注册UserMapper,并且以这种格式,不然会报错,显示没有注册
<mappers>
<mapper resource="com/kuang/dao/UserMapper.xml"/>
</mappers>
- 由于maven的约定大于限制,我写的UserMapper.xml文件一定要在子父工程的pom.xml中添加这个,
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
- 再就是遇到了一个奇葩中的奇葩问题,我一运行代码就报错,检查了所有代码没有任何问题,该更新的也都更新了,结果还是报错,结果最后在右边栏maven中点了个generate sources问题就解决了,我现在还不明白,整的我一头雾水。