mybatis框架快速入门
程序员文章站
2023-11-02 14:25:40
通过快速入门示例,我们发现使用mybatis 是非常容易的一件事情,因为只需要编写 Dao 接口并且按照 mybatis要求编写两个配置文件,就可以实现功能。远比我们之前的jdbc方便多了。(我们使用注解之后,将变得 更为简单,只需要编写一个mybatis配置文件就够了。) 1.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='女'}