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

MyBaits 入门之MyBatis环境搭建(第一篇)

程序员文章站 2024-03-11 14:03:31
mybatis 简介 mybatis的前身叫ibatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移...

mybatis 简介

mybatis的前身叫ibatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为mybatis。

mybatis是支持普通sql查询,存储过程和高级映射的优秀持久层框架。mybatis消除了几乎所有的jdbc代码和参数的手工设置以及结果集的检索。mybatis使用简单的xml或注解用于配置和原始映射,将接口和java的pojos(plan old java objects,普通的java对象)映射成数据库中的记录。

下面分步骤给搭建介绍mybatis 环境搭建。

1)在工程下引入mybatis-3.4.1.jar包,再引入数据库(mysql,mssql..)包.

2)在src下新建一个配置文件conf.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>
<!-- 引入外部配置文件 -->
 <properties resource="jdbc.properties" />
 <!-- 配置mybatis运行环境 -->
 <environments default="development">
 <environment id="development">
  <!-- type="jdbc" 代表使用jdbc的提交和回滚来管理事务 -->
  <transactionmanager type="jdbc" />  
  <!-- mybatis提供了3种数据源类型,分别是:pooled,unpooled,jndi -->
  <!-- pooled 表示支持jdbc数据源连接池 -->
  <!-- unpooled 表示不支持数据源连接池 -->
  <!-- jndi 表示支持外部数据源连接池 -->
  <datasource type="pooled">
  <property name="driver" value="${driver}" />
  <property name="url" value="${url}" />
  <property name="username" value="${username}" />
  <property name="password" value="${password}" />
  </datasource>
 </environment>
 </environments> 
 <mappers>
 <mapper resource="usermapper.xml"/>
 </mappers> 
</configuration>

这里是通过外部配置文件来存储数据库存信息的,所以加入一个jdbc.properties的数据库存信息

driver=com.microsoft.sqlserver.jdbc.sqlserverdriver
url=jdbc:sqlserver://127.0.0.1;databasename=test
username=sa
password=123456

3)建立一个映射文件 usermapper.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="hw.com.ser.iuser">
 <!-- 查询全部用户 -->
 <select id="queryusers" resulttype="hw.com.pojo.user">
  select * from ms_user
 </select>
 <selectid="queryuserbyid" resulttype="hw.com.pojo.user" parametertype="int">
  select * from ms_user where id=#{id}
 </select> 
</mapper>

(这里要注意的是,namespace属性,因为,此实例是通过接口映射的方式,所以,namespace属性一点要写成接口的路径)附图:

MyBaits 入门之MyBatis环境搭建(第一篇)

4)建一个映射接口类

package hw.com.ser;
import java.util.list;
import hw.com.pojo.user;
public interface iuser {
 public list<user> queryusers();
 public user queryuserbyid(int id);
}

5)建立一个sqlsessionfactory

package hw.com.util;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.reader;
import java.util.properties;
import org.apache.ibatis.io.resources;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
import org.apache.ibatis.session.sqlsessionfactorybuilder;
public class sqlsessionfactoryutil {
 private static sqlsessionfactory sqlsessionfactory = null;
 private static final class class_lock = sqlsessionfactoryutil.class;
 private sqlsessionfactoryutil() {
 }
 public static sqlsessionfactory initsqlsessionfactory() {
 string resource = "conf.xml";
 inputstream inputstream = null;
 try {
  inputstream = resources.getresourceasstream(resource);  
 } catch (ioexception e) {
  e.printstacktrace();
 }
 synchronized (class_lock) {
  if (sqlsessionfactory == null) {
  sqlsessionfactory = new sqlsessionfactorybuilder().build(inputstream);
  }
 }
 return sqlsessionfactory;
 }
 public static sqlsession opensqlsession(){
 if(sqlsessionfactory==null){
  initsqlsessionfactory();
 }
 return sqlsessionfactory.opensession();
 }
}

6)建一个pojo

package hw.com.pojo;
import java.util.date;
public class user {
 private string id;
 private string username;
 private string userpwd;
 private int deptmentid;
 private string usertruename;
 private string email;
 private int learncenterid;
 private date createdate;
 private date lastmodifydate;
 private int userstatus;
 public user() {
 super();
 // todo auto-generated constructor stub
 }
 public string getid() {
 return id;
 }
 public void setid(string id) {
 id = id;
 }
 public string getusername() {
 return username;
 }
 public void setusername(string username) {
 username = username;
 }
 public string getuserpwd() {
 return userpwd;
 }
 public void setuserpwd(string userpwd) {
 userpwd = userpwd;
 }
 public int getdeptmentid() {
 return deptmentid;
 }
 public void setdeptmentid(int deptmentid) {
 deptmentid = deptmentid;
 }
 public string getusertruename() {
 return usertruename;
 }
 public void setusertruename(string usertruename) {
 usertruename = usertruename;
 }
 public string getemail() {
 return email;
 }
 public void setemail(string email) {
 email = email;
 }
 public int getlearncenterid() {
 return learncenterid;
 }
 public void setlearncenterid(int learncenterid) {
 learncenterid = learncenterid;
 }
 public date getcreatedate() {
 return createdate;
 }
 public void setcreatedate(date createdate) {
 createdate = createdate;
 }
 public date getlastmodifydate() {
 return lastmodifydate;
 }
 public void setlastmodifydate(date lastmodifydate) {
 lastmodifydate = lastmodifydate;
 }
 public int getuserstatus() {
 return userstatus;
 }
 public void setuserstatus(int userstatus) {
 userstatus = userstatus;
 }
 @override
 public string tostring() {
 return "user [id=" + id + ", username=" + username + ", userpwd=" + userpwd + ", deptmentid=" + deptmentid
  + ", usertruename=" + usertruename + ", email=" + email + ", learncenterid=" + learncenterid
  + ", createdate=" + createdate + ", lastmodifydate=" + lastmodifydate + ", userstatus=" + userstatus
  + "]";
 }
}

7) 在main方法下做测试

package hw.com.day1.main;
import java.util.list;
import org.apache.ibatis.session.sqlsession;
import hw.com.pojo.user;
import hw.com.ser.iuser;
import hw.com.util.sqlsessionfactoryutil;
public class usertest {
 public static void main(string[] args) { 
 sqlsession sqlsession=null;
 try {
  sqlsession=sqlsessionfactoryutil.opensqlsession();
  iuser iuser=sqlsession.getmapper(iuser.class);
  list<user> users=iuser.queryusers();
  if(users.size()>0){
  for (user user : users) {
   system.out.println(user.tostring());
  }
  }
 } catch (exception e) {
  e.printstacktrace();
 }
 }
}