1.首先建立一个maven项目。
删除src目录(方便后面创建子项目)。
在父项目的pom.xml文件中导入依赖
<!-- 导入依赖-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
2.然后再建立一个子maven项目,在src、main中创建这些类文件
dao包中:usermapper是一个接口,usermapper.xml相当与一个接口实现类。
//接口类,User是实体类名
public interface UserMapper {
List<User> getUserList();
}
//接口实现配置文件
<?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.feng.dao.UserMapper"> //命名空间:接口类文件位置
<select id="getUserList" resultType="com.feng.pojo.User" >
//select标签内,id是接口内的方法名。resulttype 是结果类型,写实体类名就好了
select * from user //这里是sql语句
</select>
</mapper>
pojo包中是一个实体类,映射数据库内容,set、get交互数据库。
//实体类,根据自己数据库表内容来写
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 + '\'' +
'}';
}
}
utils包中是一个工具类,用来创建SqlSessionFactory工厂,SqlSessionFactory可以build一个sqlsession,sqlsession中有各种SQL操作方法。
//工具类
private static SqlSessionFactory sqlSessionFactory;
static {
{
try {
// 三行固定代码,获取sqlSessionFactory对象
//使用mybatis第一步 固定
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.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>
<!-- 可以配置多个环境,default设置默认环境-->
<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=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 每一个mapper.xml都需要在核心配置文件中注册,不然会报错 error register什么的-->
<mappers>
<mapper resource="com/feng/dao/UserMapper.xml"/>
</mappers>
</configuration>
4.maven设置
在所有pom.xml文件中添加如下:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
避免配置文件无法生效或被导出使用。