mybatis框架快速入门

通过快速入门示例,我们发现使用mybatis 是非常容易的一件事情,因为只需要编写 Dao 接口并且按照
mybatis要求编写两个配置文件,就可以实现功能。远比我们之前的jdbc方便多了。(我们使用注解之后,将变得
更为简单,只需要编写一个mybatis配置文件就够了。)

       1.mybatis的环境搭建

第一步:创建maven工程并导入坐标

第二步:创建实体类和dao的接口

第三步:创建Mybatis的主配置文件 SqlMapConifg.xml

第四步:创建映射配置文件  IUserDao.xml

  • 创建maven工厂并导入坐标
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" 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>com.itheima</groupId>
 8   <artifactId>mybatis_eesy_day01_quickstart</artifactId>
 9   <version>1.0-SNAPSHOT</version>
10   <packaging>jar</packaging>
11 
12   <name>mybatis_eesy_day01_quickstart</name>
13   <!-- FIXME change it to the project's website -->
14   <url>http://www.example.com</url>
15 
16   <properties>
17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18     <maven.compiler.source>1.8</maven.compiler.source>
19     <maven.compiler.target>1.8</maven.compiler.target>
20   </properties>
21 
22   <dependencies>
23       <dependency>
24           <groupId>org.mybatis</groupId>
25           <artifactId>mybatis</artifactId>
26           <version>3.4.5</version>
27       </dependency>
28     <dependency>
29       <groupId>mysql</groupId>
30       <artifactId>mysql-connector-java</artifactId>
31       <version>5.1.6</version>
32     </dependency>
33     <dependency>
34       <groupId>log4j</groupId>
35       <artifactId>log4j</artifactId>
36       <version>1.2.17</version>
37     </dependency>
38     <dependency>
39       <groupId>junit</groupId>
40       <artifactId>junit</artifactId>
41       <version>4.11</version>
42       <scope>test</scope>
43     </dependency>
44   </dependencies>
45 
46   <build>
47     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
48       <plugins>
49         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
50         <plugin>
51           <artifactId>maven-clean-plugin</artifactId>
52           <version>3.1.0</version>
53         </plugin>
54         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
55         <plugin>
56           <artifactId>maven-resources-plugin</artifactId>
57           <version>3.0.2</version>
58         </plugin>
59         <plugin>
60           <artifactId>maven-compiler-plugin</artifactId>
61           <version>3.8.0</version>
62         </plugin>
63         <plugin>
64           <artifactId>maven-surefire-plugin</artifactId>
65           <version>2.22.1</version>
66         </plugin>
67         <plugin>
68           <artifactId>maven-jar-plugin</artifactId>
69           <version>3.0.2</version>
70         </plugin>
71         <plugin>
72           <artifactId>maven-install-plugin</artifactId>
73           <version>2.5.2</version>
74         </plugin>
75         <plugin>
76           <artifactId>maven-deploy-plugin</artifactId>
77           <version>2.8.2</version>
78         </plugin>
79         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
80         <plugin>
81           <artifactId>maven-site-plugin</artifactId>
82           <version>3.7.1</version>
83         </plugin>
84         <plugin>
85           <artifactId>maven-project-info-reports-plugin</artifactId>
86           <version>3.0.0</version>
87         </plugin>
88       </plugins>
89     </pluginManagement>
90   </build>
91 </project>
  • 创建实体类和dao接口
1 public class User implements Serializable {
2     private Integer id;
3     private String username;
4     private Date birthday;
5     private String address;
6     private String sex;
public interface IUserDao {
//    查询所有用户
    List<User> findAll();
}
  • 创建主配置文件
<?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>
    <!--配置mybatis的环境-->
<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/eesy_mybatis"/>
            <property name="username" value="root"/>
            <property name="password" value="123"/>
        </dataSource>
    </environment>
</environments>

    <!--告知mybatis映射文件的位置-->
    <mappers>
        <mapper resource="com/itheima/dao/IUserDao.xml"/>
    </mappers>
</configuration>
  • 创建映射文件
<?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.itheima.dao.IUserDao">
    <select id="findAll" resultType="com.itheima.domain.User">
        SELECT * FROM  user
    </select>
</mapper>

编写测试代码

package com.itheima;

import com.itheima.dao.IUserDao;
import com.itheima.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.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {
    public static void main(String[] args) throws IOException {
        //1.读取配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory的构建者对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.使用构建者创建工厂对象SqlSessionFactory
        SqlSessionFactory factory = builder.build(in);
        //4.使用SqlSessionFactory创建SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //5.使用SqlSession创建dao的代理对象
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        //6.使用代理对象执行查询方法
        List<User> userList = userDao.findAll();
        for (User user :userList){
            System.out.println(user.toString());
        }
        //7.释放资源
        sqlSession.close();
        in.close();
    }
}

测试结果

User{id=41, username='老王', birthday=Tue Feb 27 17:47:08 CST 2018, address='北京', sex='男'}
User{id=42, username='小二王', birthday=Fri Mar 02 15:09:37 CST 2018, address='北京金燕龙', sex='女'}
User{id=43, username='小二王', birthday=Sun Mar 04 11:34:34 CST 2018, address='北京金燕龙', sex='女'}
User{id=45, username='传智播客', birthday=Sun Mar 04 12:04:06 CST 2018, address='北京金燕龙', sex='男'}
User{id=46, username='老王', birthday=Wed Mar 07 17:37:26 CST 2018, address='北京', sex='男'}
User{id=48, username='小马宝莉', birthday=Thu Mar 08 11:44:00 CST 2018, address='北京修正', sex='女'}

猜你喜欢

转载自www.cnblogs.com/lsk-130602/p/12179234.html