Mybatis的环境搭建
1.创建maven工程导入mybatis坐标(其他坐标根据需要搜索repository)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mystudy</groupId>
<artifactId>day01_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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.6</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>
</project>
2.创建实体类和DAO接口
- 创建User实体类
package com.itheima.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
- 创建DAO接口
package com.itheima.dao;
import com.itheima.domain.User;
import java.util.List;
/**
* 用户持久层接口
*/
public interface UserDao {
/**
* 查询所有操作
* @return
*/
List<User> findAll();
}
3.创建Mybatis主配置文件(SqlMapConfig.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>
<!-- 配置环境 (主配置文件)-->
<environments default="mysql">
<!--配置mysql的环境-->
<environment id="mysql">
<!--配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据源(连接池)-->
<dataSource type="POOLED">
<!--配置数据库的连接信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/studydb"/>
<property name="username" value="root"/>
<property name="password" value="961020"/>
</dataSource>
</environment>
</environments>
<!--指定映射配置文件的位置,映射配置文件指的是每一个dao独立的配置文件路径-->
<mappers>
<mapper resource="com/itheima/dao/UserDao.xml"></mapper>
</mappers>
</configuration>
4.创建映射配置文件(UserDao.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">
<mapper namespace="com.dao.UserDao">
<!--配置查询所有-->
<select id="findAll">
select * from User;
</select>
</mapper>
- 注意事项
1.创建UserDao.java和UserDao.xml的命名是为了和小白所学知识保持一致,在Mybatis持久层中操作接口名称和映射文件也叫做:Mapper
所以:UserDao和UserMapper是一样的
2.在idea闯进目录的时候,和包是不一样的
包在创建时:com.dao是二级结构
目录在创建时:com.dao是一级目录
3.mybatis的映射配置文件必须要和dao接口的包结构相同
4.映射配置文件的mapper标签namespace属性的取值必须是dao接口的全限定类名
5.映射配置文件的select的id必须是dao接口的方法名
遵从了3、4、5则不需要再写dao的实现类
网友评论