IDEA版本:2021.2.3
jdk:11
mysql:8.0.16版本
首先先创建一个父Maven工程名为Mybatis-Study,然后将他的src文件夹删除,将他作为父工程,在其中创建子模块用于我们编写程序(方便导包,直接在父工程中使用maven来导包,而子模块中不需要一一去导入包)
父工程的pom.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.example</groupId> 8 <artifactId>Mybatis-Study</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 <modules> 12 <module>Mybatis-01</module> 13 </modules> 14 15 <properties> 16 <maven.compiler.source>11</maven.compiler.source> 17 <maven.compiler.target>11</maven.compiler.target> 18 </properties> 19 <!-- 该工程为父工程,所以我删除了src文件夹--> 20 21 <!-- 这里要导入三个依赖1、mysql驱动 2、mybatis 3、junit--> 22 <dependencies> 23 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 24 <dependency> 25 <groupId>mysql</groupId> 26 <artifactId>mysql-connector-java</artifactId> 27 <version>8.0.16</version> 28 </dependency> 29 30 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 31 <dependency> 32 <groupId>org.mybatis</groupId> 33 <artifactId>mybatis</artifactId> 34 <version>3.4.5</version> 35 </dependency> 36 <!-- https://mvnrepository.com/artifact/junit/junit --> 37 <dependency> 38 <groupId>junit</groupId> 39 <artifactId>junit</artifactId> 40 <version>4.12</version> 41 <scope>test</scope> 42 </dependency> 43 44 45 </dependencies> 46 47 <!--在build中配置resources,来防止我们资源导出失败的问题--> 48 <build> 49 <resources> 50 <resource> 51 <directory>src/main/resources</directory> 52 <includes> 53 <include>**/*.properties</include> 54 <include>**/*.xml</include> 55 </includes> 56 <filtering>false</filtering> 57 </resource> 58 <resource> 59 <directory>src/main/java</directory> 60 <includes> 61 <include>**/*.properties</include> 62 <include>**/*.xml</include> 63 </includes> 64 <filtering>false</filtering> 65 </resource> 66 </resources> 67 </build> 68 69 </project>
然后我们在父工程中创建子模块Mybatis-01用于编写程序.
在子模块的resource文件夹中创建Mybatis的核心配置文件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> <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=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/kuang/dao/UserMapper.xml"/> </mappers> </configuration>
创建一个实体类User,属性与mysql中你要操作的表一模一样,并设置get和set还有toString、构造方法.
package com.kuang.pojo; public class User { private int id; private String name; private String pwd; public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public User() { } 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 + '\'' + '}'; } }
然后再java文件夹中创建包com.kuang.pojo中创建接口UserDao:
package com.kuang.dao; import com.kuang.pojo.User; import java.util.List; public interface UserDao { public List<User> getUserList();//用于获取用户列表的方法 }
再继续在com.kuang.pojo包中创建xml文件实现该接口(该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.kuang.dao.UserDao"> <select id="getUserList" resultType="com.kuang.pojo.User"> select * from mybatis.user </select> </mapper>
要注意每一个mapper都要在mybatis的核心配置文件中注册,使用在<mappers>标签中使用<mapper>注册
然后再com.kuang.utils包下创建一个Mybatis的工具类,名为MybatisUtils,编写该类的方法,用于获取SqlSession对象.
package com.kuang.utils; 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.IOException; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch(IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){//用于获取SqlSession对象 return sqlSessionFactory.openSession(); } }
然后在test文件中编写一个测试类测试是否能够成功(快捷键ctrl+shift+t):
package com.kuang.dao; import com.kuang.pojo.User; import com.kuang.utils.MybatisUtils; import junit.framework.TestCase; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest extends TestCase { @Test public void testGetUserList() { //1、获得了SqlSession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao userDao = sqlSession.getMapper(UserDao.class);//使用class属性来获取类的class对象 List<User> userList = userDao.getUserList();//执行语句 for(User user :userList){ //循环输出语句 System.out.println(user); } sqlSession.close();//关闭sqlSession流 } }