mybatis创建maven项目整体框架
创建Dao接口
? 1)存放Dao接口
? 2)存放接口的映射文件
里面除了头文件(固定的)就剩一个mapper标签
<mapper namespace=”命名空间“>:在标签里面填写sql语句
创建pojo文件
1)存放mysql对应的实体类
创建utils
存放工具类
创建mybatis-config.xml文件
1)配置数据库信息
2)加载接口的映射文件
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="mydev">
<environment id="mydev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="cgjcgj"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org\example\Dao\UserDao.xml"/>
</mappers>
</configuration>
在pom.xml中添加依赖
1)修改jdk信息
2)添加数据库和mybatis依赖
3)添加buil标签内容使其加载接口的映射文件
<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>
|