1. Maven工程结构
2. mybatis环境搭建
2.1 新建数据库和表
2.2 新建一个maven工程
2.3 pom.xml导入坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?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.ssm</groupId> <artifactId>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>8.0.17</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.4 创建实体类和DAO接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.ssm.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 + '\'' + '}'; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.ssm.dao;
import com.ssm.domain.User;
import java.util.List;
public interface IUserDao {
List<User> findAll(); }
|
2.5 创建Mybatis的主配置文件——SqlMapConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?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"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments>
<mappers> <mapper resource="com/ssm/dao/IUserDao.xml"/> </mappers> </configuration>
|
2.6 创建映射配置文件——IUserDao.xml
1 2 3 4 5 6 7 8 9 10
| <?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.ssm.dao.IUserDao"> <select id="findAll" resultType="com.ssm.domain.User"> select * from user </select> </mapper>
|
环境搭建的注意事项:
①在Mybatis中持久层的操作接口名称和映射文件也叫做: Mapper
所以: IUserdao和IUserMapper是一样的
②在idea中创建目录的时候,它和包是不一样的
包在创建时:com. i theima.dao它是三级结构
目录在创建时:com. i theima.dao是一级目录
③myatis的映射配置文件位置必须和dao接口的包结构相同
④映射配置文件的mapper标签namespace属性的取值必须是dao接口的全限定类名
⑤映射配置文件的操作配置(select),id属性的取值必须是dao接口的方法名
3. mybatis入门案例
3.1 在Test中创建MybatisTest类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.ssm.test;
import com.ssm.dao.IUserDao; import com.ssm.domain.User; 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.InputStream; import java.util.List;
public class MybatisTest { public static void main(String[] args)throws Exception { InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); SqlSession session = factory.openSession(); IUserDao userDao = session.getMapper(IUserDao.class); List<User> users = userDao.findAll(); for (User user : users) { System.out.println(user); } session.close(); in.close(); } }
|
3.2 执行结果