MyBaits 入門之MyBatis環境搭建(第一篇)。本站提示廣大學習愛好者:(MyBaits 入門之MyBatis環境搭建(第一篇))文章只能為提供參考,不一定能成為您想要的結果。以下是MyBaits 入門之MyBatis環境搭建(第一篇)正文
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屬性一點要寫成接口的途徑)附圖:
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(); } } }