目录
1、新建数据库
2、新建项目
2.1导入依赖
2.2创建子工程(新建模块)
2.3添加配置文件mybatis-config.xml
2.4添加数据源
2.5编写mybatis核心配置文件
2.6编写MybatisUtils工具类
3、编写代码
3.1实体类
3.2 Mapper(UserDao)接口
3.3 接口实现类(mapper.xml)
4、测试
1、新建数据库
2、新建项目
2.1导入依赖
创建maven工程并导入坐标(创建一个普通maven项目,删除src文件夹,作为父工程)
此处特别注意<build>中代码
<!--引入依赖-->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<!--在build中配置resources来防止资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.2创建子工程(新建模块)
2.3添加配置文件mybatis-config.xml
2.4添加数据源
2.5编写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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?
useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每个Mapper.XML都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper resource="com/dymic/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>
</configuration>
2.6编写MybatisUtils工具类
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();
}
}
//SqlSession 包含了面向数据库执行SQL所需的方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
3、编写代码
3.1实体类
public class User {
private int id;
private String username;
private String password;
public User(){}
public User(int id,String username,String password){
this.id=id;
this.username=username;
this.password=password;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3.2 Mapper(UserDao)接口
public interface UserDao {
List<User>getUserList();
}
3.3 接口实现类(mapper.xml)
(之前的接口实现类)
<?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">
<!--namespace绑定一个对应的Dao/Mapper的接口-->
<mapper namespace="com.dymic.dao.UserDao">
<!-- id 是接口要实现的方法 ,resultType是实体类全类名-->
<select id="getUserList" resultType="com.dymic.pojo.User" >
select * from mybatis.user
</select>
</mapper>
这里用一张图说下Dao层接口和Mpper.xml的关系
4、测试
public class UserDaoTest {
@Test
public void test() {
//获得SqlSession
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭SqlSessstion
sqlSession.close();
}
}