关闭

MyBatis入门

标签: mybatis实例
93人阅读 评论(0) 收藏 举报

MyBatis工作流程图


实例代码:

1:创建一个用户类

  1. package com.org.mybatis.entity;
  2. import java.io.Serializable;
  3. import javax.xml.crypto.Data;
  4. public class User implements Serializable {
  5.  private static final long serialVersionUID = 1L;
  6.  private String id;
  7.  private String name;
  8.  private String password;
  9.  private Data createdate;
  10.  .....

2:创建一个用户接口

  1. package com.org.mybatis.entity;
  2. import com.org.mybatis.entity.User;
  3. public interface UserInterface {
  4.  User getUser(int id);
  5.  }

3:创建xml映射文件

  1. <?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="com.org.mybatis.service.UserInterface">
     <select id="getUser" parameterType="int" resultType="com.org.mybatis.entity.User">
      select * from ls_user where id=#{id}
     </select>
    </mapper>

4:创建xml配置文件(数据库的链接和导入xml映射文件)

  1. <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
     "
    http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
    <configuration>
     <environments default="environment">
      <environment id="environment">
       <transactionManager type="JDBC" />
       <dataSource type="POOLED">
        <property name="driver" value="com.mysql.jdbc.Driver" />
        <property name="url"
         value="jdbc:mysql://127.0.01:3306/dome" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
       </dataSource>
      </environment>
     </environments>
     <mappers>
      <mapper resource="com/org/mybatis/mapper/user.xml" />
     </mappers>
    </configuration>

5:测试

  1. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import com.org.mybatis.entity.User;
    import com.org.mybatis.service.UserInterface;
    import com.org.mybatis.url.UrlDome;
  2. public class UserDome {
     private static  SqlSessionFactoryBuilder sessionFactoryBuilder;
     private static  SqlSessionFactory sessionFactory;
     private static  SqlSession session;
     private static InputStream is;
  3.  static{
      sessionFactoryBuilder=new SqlSessionFactoryBuilder();
      is = UserDome.class.getClassLoader().getResourceAsStream(UrlDome.SQL_CONFIG);
      sessionFactory=sessionFactoryBuilder.build(is);
      session = sessionFactory.openSession();
     }
  4.  /**第一种写法:
      * 该方法的使用需要在user.xml配置文件的mapper 设置为user.xml所在的路径
      * 比如:<mapper namespace="com.org.mybatis.mapper.user">
      * **/

     public static void test1(){
      try {
       String statement = "com.org.mybatis.mapper.user.getUser";
       User user = session.selectOne(statement, 1);
       System.out.println(user.getName());
      } finally {
       session.close();
      }
     }
     
     /**第二种写法:
      * 该方法的使用需要在user.xml配置文件的mapper 设置为UserInterface就所在的包
      * 比如:<mapper namespace="com.org.mybatis.service.UserInterface">
      * **/

     public static void test2(){
      try {
       UserInterface userMapper=session.getMapper(UserInterface.class);
       User user=userMapper.getUser(1);
       System.out.println(user.getName());
      } finally {
       session.close();
      }
     }
  5.  public static void main(String[] args) {
      test2();
     }
    }

 

 

 

 

 

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:589次
    • 积分:32
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档
    阅读排行
    评论排行