第一个mybatis程序
- 创建数据库
2.连接数据库
打开idea添加数据库
添加成功
3.创建项目
新建一个maven项目
配置xml文件,引入mysql,mybatis,junit依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
在项目src的resource下创建mybatis.xml用以配置mybatis
打开mybatis官方文档
复制以上代码到新建的mybatis.xml文件下
修改以上代码,连接自己的数据库
环境配置完成!
3.编写代码
新建一个实体类User
package pojo;
public class User {
private int Id;
private String username;
private String password;
public User() {
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(int id, String username, String password) {
Id = id;
this.username = username;
this.password = password;
}
}
实体类创建好过后定义操作实体类的接口UserMapper
以往实现接口的方法操作数据库,现在通过配置.xml文件来代替,查看官方文档,复制代码
注意这里的mapper namespace为作用空间,修改为UserMapper接口的路径,select标间中id绑定为对应的函数getUserList,resultType为返回的结果形式,为pojo包下的User实体类
定义一个工具类MybatisUtil获取SqlSession
参考官方文档代码
其中resource路径修改为原先配置的mybatis.xml
工具类代码
package util;
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 {
//使用mybatis获取sessionfactory对象
try {
String resource = "mybatis-conf.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
开始测试
测试代码:
import dao.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import utils.MybatisUtil;
import java.util.List;
public class MybatisTest {
public static void main(String[] args) {
SqlSession sqlSession= MybatisUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users = userMapper.getUserList();
for(User user:users)
{
System.out.println("Id:"+user.getId()+"用户名:"+user.getUsername()+"密码:"+user.getPassword());
}
sqlSession.close();
}
}
报错情况处理:
在mybatis.xml中需改变resource,为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>
修改后如果仍出现错误可以 清楚缓存后再重新启动!