#Mybatis官网入门地址:https://mybatis.org/mybatis-3/zh/getting-started.html
#创建空项目,创建Maven模块
#配置pom.xml <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"> <parent> <artifactId>mybatisDemo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>test</artifactId> <packaging>jar</packaging> <name>test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- mybatis 依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <!--mysql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> </dependencies> <build> <resources> <resource> <!--所在的目录--> <directory>src/main/java</directory> <includes> <!--包括目录下的.properties .xml文件都会扫描到--> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
#创建resources文件夹
#创建一个User类(根据数据库表) public class User { private int p_id; private String p_name; private String address; private String birth; private String gender; get+set方法(自行脑补--) }
#创建User接口
#创建sql映射文件UserDao.xml <?xml version="1.0" encoding="UTF-8" ?> <!--约束文件 mybatis-3-mapper.dtd约束文件的名称,扩展名btd(用来限制和检测当前文件中出现的标签,属性必须符合mybatis的要求)--> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace名称空间,唯一,可自定义,要求使用dao接口的全限定名称--> <!--mapper是当前文件的根标签 dao的全限定名称 resultType返回结果类型 id唯一标识--> <mapper namespace="org.example.dao.UserDao"> <select id="selectUsers" resultType="org.example.domain.User"> SELECT * FROM TEST1; </select> <!-- <update id=""></update> <insert id=""></insert> --> </mapper> <!--sql映射文件:写sql语句的 ,mybatis会执行这些sql-->
#尝试编译
#创建mybatis主配置文件mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- default必须和environment值一样--> <environments default="myuser"> <!--environment可以有多个 但id唯一--> <environment id="myuser"> <!--transactionManager,mybatis的事务类型 commit rollback--> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://IP:3306/mysql?"/> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> <!-- mapper sql映射文件的位置 target/classes类路径--> <mappers> <mapper resource="org/example/dao/UserDao.xml"/> </mappers> </configuration> <!--mybatis主配置文件:主要定义了数据库的配置信息,sql映射文件的位置-->
#创建一个测试类 public class Sqltest { public static void main(String[] args) throws IOException { //mybatis读取User数据 //1.定义mybatis主配置文件名称,从类路径开始(target/clasess) String resource = "mybatis-config.xml"; //读取mybatis-config.xml文件 InputStream inputStream = Resources.getResourceAsStream(resource); //创建SqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //(关键)获取sqlSessionFactory对象,从SqlSessionFactory中获取sqlSession SqlSession sqlSession =sqlSessionFactory.openSession(); //(关键)指定要执行的sql语句的标识。(sql映射文件的namesapce+id标识) String Sqlmapper ="org.example.dao.UserDao.selectUsers"; //执行sql语句 List<User> userList = sqlSession.selectList(Sqlmapper); //输出结果 //userList.forEach(users -> System.out.println(users)); for (User u:userList) {System.out.println(u);} //释放资源 sqlSession.close(); } }
#重新编译一遍,执行