eclipse中配置myBatis


eclipse string user sqlserver 数据库 encoding

首先去myBatis的官网下载jar包,地址:点击打开链接,该地址中还有个MyBatis-3-User-Guide-Simplified-Chinese.pdf的说明文档,建议一起下载。

下载完成后打开eclipse新建项目MyBatis,添加myBatis的jar包和mySQL的jar包,如果你使用的是其他数据库添加其他jar包。

新建User.java

[java]  view plain copy
  1. package com.myBatis.bean;  
  2.   
  3. import java.io.Serializable;  
  4. import java.sql.Date;  
  5. import java.util.Set;  
  6.   
  7. public class User implements Serializable{  
  8.       
  9.     /** 
  10.      *  
  11.      */  
  12.     private static final long serialVersionUID = 1L;  
  13.     private String id;  
  14.     private String name;  
  15.     private String pasw;  
  16.     private String sex;  
  17.     private String email;  
  18.     private String degree;  
  19.     private String hobby;  
  20.     private Date birthDate;  
  21.       
  22.       
  23.     public String getDegree() {  
  24.         return degree;  
  25.     }  
  26.     public void setDegree(String degree) {  
  27.         this.degree = degree;  
  28.     }  
  29.     public void setHobby(String hobby) {  
  30.         this.hobby = hobby;  
  31.     }  
  32.     public String getSex() {  
  33.         return sex;  
  34.     }  
  35.     public void setSex(String sex) {  
  36.         this.sex = sex;  
  37.     }  
  38.     public String getEmail() {  
  39.         return email;  
  40.     }  
  41.     public void setEmail(String email) {  
  42.         this.email = email;  
  43.     }  
  44.     public String getHobby() {  
  45.         return hobby;  
  46.     }  
  47.     public Date getBirthDate() {  
  48.         return birthDate;  
  49.     }  
  50.     public void setBirthDate(Date birthDate) {  
  51.         this.birthDate = birthDate;  
  52.     }  
  53.     public String getId() {  
  54.         return id;  
  55.     }  
  56.     public void setId(String id) {  
  57.         this.id = id;  
  58.     }  
  59.     public String getName() {  
  60.         return name;  
  61.     }  
  62.     public void setName(String name) {  
  63.         this.name = name;  
  64.     }  
  65.     public String getPasw() {  
  66.         return pasw;  
  67.     }  
  68.     public void setPasw(String pasw) {  
  69.         this.pasw = pasw;  
  70.     }  
  71. }  


新建configuration.xml

[html]  view plain copy
  1. xml version="1.0" encoding="UTF-8" ?>   
  2. "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  3. <configuration>  
  4.       
  5.     <properties resource="properties/dataBase.properties">properties>  
  6.     <typeAliases>  
  7.       
  8.         <typeAlias type="com.myBatis.bean.User" alias="User" />  
  9.     typeAliases>  
  10.     <environments default="development">  
  11.         <environment id="development">  
  12.             <transactionManager type="JDBC" />  
  13.             <dataSource type="POOLED">  
  14.               
  15.                 <property name="driver" value="${driver}" />  
  16.                 <property name="url" value="${url}" />  
  17.                 <property name="username" value="${username}" />  
  18.                 <property name="password" value="${password}" />  
  19.             dataSource>  
  20.         environment>  
  21.     environments>  
  22.       
  23.     <mappers>  
  24.           
  25.         <mapper resource="conf/User.xml" />  
  26.     mappers>  
  27. configuration>  

新建dataBase.properties文件

在这个文件里,你可以配置oracle、sqlServer等等的配置信息,但是要通过名称区分开来,例如mysql.driver、orcl.driver等等,同时,如果跟换数据库时,configuration.xml中的${ }中的名字也要改变。

[html]  view plain copy
  1. driver = org.gjt.mm.mysql.Driver  
  2. url = jdbc:mysql://localhost:3306/bbs  
  3. username = root  
  4. password = root  

新建User.xml

[html]  view plain copy
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"   
  3. "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
  4.     <mapper namespace="User">  
  5.         <resultMap id="userResultMap" type="User">  
  6.               
  7.             <id property="id" column="id" />  
  8.             <result property="name" column="name" />  
  9.             <result property="pasw" column="pasw" />  
  10.             <result property="email" column="email"/>  
  11.             <result property="birthDate" column="birthDate" />  
  12.             <result property="sex" column="sex" />  
  13.             <result property="degree" column="degree"/>  
  14.             <result property="hobby" column="hobby"/>  
  15.               
  16.         resultMap>  
  17.           
  18.           
  19.         <select id="selectUser" resultType="User" resultMap="userResultMap">  
  20.             select * from user order by id;  
  21.         select>  
  22.   
  23.     mapper>   

新建UserDaoTest.java来测试

[java]  view plain copy
  1. package com.myBatis.dao;  
  2.   
  3. import java.io.Reader;  
  4. import java.sql.SQLException;  
  5. import java.util.ArrayList;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8.   
  9. import org.apache.ibatis.io.Resources;  
  10. import org.apache.ibatis.session.SqlSession;  
  11. import org.apache.ibatis.session.SqlSessionFactory;  
  12. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  13.   
  14. import com.myBatis.bean.User;  
  15.   
  16. public class UserDaoTest {  
  17.   
  18.     public static List getAllUsers() throws SQLException {  
  19.   
  20.         List users = new ArrayList();  
  21.         try {  
  22.             /* 
  23.              * 下面的内容在MyBatis-3-User-Guide-Simplified-Chinese.pdf中有详细的解释 
  24.              */  
  25.             String resource = "com/myBatis/data/configuration.xml";//加载配置文件  
  26.             Reader reader = Resources.getResourceAsReader(resource);  
  27.             SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();  
  28.             SqlSessionFactory factory = builder.build(reader);  
  29.             SqlSession session = factory.openSession();  
  30.             users = session.selectList("User.selectUser");//配置文件中的namespace.select id  
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         return users;  
  35.     }  
  36.   
  37.     public static void main(String[] args) {  
  38.         List users = new ArrayList();  
  39.         try {  
  40.             users = UserDaoTest.getAllUsers();  
  41.             Iterator iterator = users.iterator();  
  42.             while (iterator.hasNext()) {  
  43.                 User user = (User) iterator.next();  
  44.                 System.out.println(user.getName());  
  45.             }  
  46.         } catch (SQLException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.     }  
  50. }  

总结:

    一共3个配置文件,要注意xml文件中双引号是否是英文状态下输入的,如果对配置文件中的标签有不理解的话可以看pdf文档,相当详细还是中文的。

 

你可能感兴趣的:(eclipse中配置myBatis)