Java工程中使用Mybatis (工程结合Mybatis,数据结合Swing使用))

时间:2021-05-20

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(); } }}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章