欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis的详细使用教程

程序员文章站 2024-03-09 13:01:35
mybatis所需要的jar包: 需要引用两个jar包,一个是mybatis,另一个是mysql-connector-java,如果是maven工程的话,pom里如下添加...

mybatis所需要的jar包:

需要引用两个jar包,一个是mybatis,另一个是mysql-connector-java,如果是maven工程的话,pom里如下添加依赖就可以了。

<dependency>
 <groupid>org.mybatis</groupid>
 <artifactid>mybatis</artifactid>
 <version>3.2.3</version>
</dependency>
<dependency>
 <groupid>mysql</groupid>
 <artifactid>mysql-connector-java</artifactid>
 <version>5.1.26</version>
</dependency>

数据准备:

在mysql中造点数据进行测试用:

create database mybatis_test;
create table user
(
 age integer not null,
 name varchar(64) not null default ''
);
insert user values(18,'zhanjindong');
insert user values(20,'zhangsan');

配置文件:

需要两类配置文件,一类是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> 
 <settings> 
  <!-- changes from the defaults for testing --> 
  <setting name="cacheenabled" value="false" /> 
  <setting name="usegeneratedkeys" value="true" /> 
  <setting name="defaultexecutortype" value="reuse" /> 
 </settings> 
 <typealiases> 
  <typealias alias="user" type="test.mybatis.user"/> 
 </typealiases> 
 <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://192.168.71.38:3306/mybatis_test"/> 
    <property name="username" value="root"/> 
    <property name="password" value="123456"/> 
   </datasource> 
  </environment> 
 </environments> 
 <mappers> 
  <mapper resource="mappers/usermapper.xml" /> 
 </mappers> 
</configuration>

另一类是数据访问接口映射文件:示例中是usermapper.xml。这个文件一般放在src/main/resource下面或子目录下mybatis就可以找到了,在mybatis-config.xml中由mappers/mapper节点的resource指定。

<?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="test.mybatis.usermapper">
 <!-- 这里namespace必须是usermapper接口的路径” -->
 <insert id="insertuser" parametertype="user">
  insert into user(name,age) values(#{name},#{age})
  <!-- 这里sql结尾不能加分号,否则报“ora-00911”的错误 -->
 </insert>
 <!-- 这里的id必须和usermapper接口中的接口方法名相同 -->
 <select id="getuser" resulttype="user" parametertype="java.lang.string">
  select * from user where name=#{name}
 </select>
</mapper>

对应这个映射文件就是test.mybatis这个命名空间下的usermapper这个接口,只是定义了访问数据表的接口:

package test.mybatis;
public interface usermapper {
 public void insertuser(user user);
 public user getuser(string name);
}

需要一个pojo:user.java

package test.mybatis;
public class user {
 private string name;
 private integer age;
 public string getname() {
  return name;
 }
 public void setname(string name) {
  this.name = name;
 }
 public integer getage() {
  return age;
 }
 public void setage(integer age) {
  this.age = age;
 }
 public user(string name, integer age) {
  super();
  this.name = name;
  this.age = age;
 }
 public user() {
  super();
 }
}

测试:

通过mybatis操作数据库用到的是一个叫sqlsession的类,这个类是通过sqlsessionfactory产生的,一般建议在全局维护一个sqlsessionfactory就可以了。

testmybatis.java
package test.mybatis;
import java.io.ioexception;
import java.io.reader;
import org.apache.ibatis.io.resources;
import org.apache.ibatis.session.sqlsessionfactory;
import org.apache.ibatis.session.sqlsessionfactorybuilder;
public class mybatisutil {
 private final static sqlsessionfactory sqlsessionfactory;
 static {
  string resource = "mybatis-config.xml";
  reader reader = null;
  try {
   reader = resources.getresourceasreader(resource);
  } catch (ioexception e) {
   system.out.println(e.getmessage());
  }
  sqlsessionfactory = new sqlsessionfactorybuilder().build(reader);
 }
 public static sqlsessionfactory getsqlsessionfactory() {
  return sqlsessionfactory;
 }
}

测试代码如下:

testmybatis.java
package test.mybatis;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
public class testmybatis {
 static sqlsessionfactory sqlsessionfactory = null;
 static {
  sqlsessionfactory = mybatisutil.getsqlsessionfactory();
 }
 public static void main(string[] args) {
  testadd();
  getuser();
 }
 public static void testadd() {
  sqlsession sqlsession = sqlsessionfactory.opensession();
  try {
   usermapper usermapper = sqlsession.getmapper(usermapper.class);
   user user = new user("lisi", new integer(25));
   usermapper.insertuser(user);
   sqlsession.commit();// 这里一定要提交,不然数据进不去数据库中
  } finally {
   sqlsession.close();
  }
 }
 public static void getuser() {
  sqlsession sqlsession = sqlsessionfactory.opensession();
  try {
   usermapper usermapper = sqlsession.getmapper(usermapper.class);
   user user = usermapper.getuser("zhangsan");
   system.out.println("name: " + user.getname() + "|age: "
     + user.getage());
  } finally {
   sqlsession.close();
  }
 }
}

1、mybatis会用log4j记录日志,但是开启debug模式貌似对性能影响非常厉害。

2、mybatis的查询缓存对性能的影响非常大,启用和不启用差距非常大

以上所述是小编给大家介绍的mybatis的详细使用教程,希望对大家有所帮助