Java工程中使用Mybatis (工程结合Mybatis,数据结合Swing使用))
程序员文章站
2024-02-27 22:24:51
2011年6月ibatis 更名为 mybatis,从 ibatis 到 mybatis,不只是名称上的变化,mybatis 提供了更为强大的功能,同时并没有损失其易用性,...
2011年6月ibatis 更名为 mybatis,从 ibatis 到 mybatis,不只是名称上的变化,mybatis 提供了更为强大的功能,同时并没有损失其易用性,相反,在很多地方都借助于 jdk 的泛型和注解特性进行了简化。所以你懂的,开始学习就从mybatis吧.
例子使用mybatis+oracle实现一个用户表的查询,如下:
①数据库 在mysql中,test数据库下,简历user表,字段:iid,ifullname,ipassword,建表语句略。
②需导入的包 只有两个: mybatis-3.4.4.jar 和 ojdbc14.jar(jdbc包)
③目录结构 采用最简单的结构,com.mybatis包下有且仅有四个文件 configuration.xml user.xml user.java test.java
④mybatis配置文件configuration.xml,
[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> <typealiases> <typealias alias="user" type="com.mybatis.user"></typealias> </typealiases> <environments default="development"> <environment id="development"> <transactionmanager type="jdbc"></transactionmanager> <datasource type="pooled"> <property name="driver" value="oracle.jdbc.driver.oracledriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.0.87:1526:entegor" /> <property name="username" value="ideal" /> <property name="password" value="ideal" /> </datasource> </environment> </environments> <mappers> <mapper resource="user.xml"/> </mappers> </configuration>
user表sql文件user.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="user"> <select id="selectuser" parametertype="int" resulttype="user"> select * from user where id = #{id} </select> <select id="selectusers" resulttype="user"> select * from user </select> </mapper>
表结构文件user.java
package com.mybatis; public class user { private int iid; private string ifullname; private string iloginname; private string ipassword; public user(){} public user(int id, string name) { this.iid = id; this.ifullname = name; } public string getifullname() { return ifullname; } public int getiid() { return iid; } public void setiid(int iid) { this.iid = iid; } public void setifullname(string ifullname) { this.ifullname = ifullname; } public string getiloginname() { return iloginname; } public void setiloginname(string iloginname) { this.iloginname = iloginname; } public string getipassword() { return ipassword; } public void setipassword(string ipassword) { this.ipassword = ipassword; } }
⑦测试用例test.java
package com.controller; import java.io.ioexception; import java.io.reader; import java.util.list; 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 com.mybatis.user; public class test { public static void main(string[] args) throws ioexception { // todo auto-generated method stub string resource = "configuration.xml"; reader reader = resources.getresourceasreader(resource); sqlsessionfactory ssf = new sqlsessionfactorybuilder().build(reader); sqlsession session = ssf.opensession(); try{ user user = session.selectone("selectuser", 2); system.out.println(user.getifullname()); system.out.println(user); system.out.println("--------------分隔线---------------"); list<user> users = session.selectlist("selectusers"); for(int i=0; i<users.size(); i++) { system.out.println(users.get(i).getifullname()); } } catch (exception e) { e.printstacktrace(); } finally { session.close(); } } }
上一篇: java实现接口的典型案例