• mybatis 开发自己的项目


    1、Mybatis优缺点

    优点:  Mybatis实现了对Dao层的封装,隔离了SQL语句,便于管理,避免了像JDBC那样操作数据集,便于扩展等等。

    缺点:  Mybatis属于?半自动“ORM”,比Hibernate的工作做得要多很多,对象参数传递比较麻烦,没有Hibernate对象操作的概念。

    2、Mybatis的实现方式  

    Mybatis提供两种应用实现:XML配置和注解。

    2.1配置主要依赖实体对象的xml文件,将对象以<resultMap>形式注入,并提供给<insert > <delete > <select> <update> 语句引用。

    2.2使用注解来的比配置XML文件要简单得多。只需要在接口上添加相应的注解并附上SQL语句就行了,如:   

      插入语句:@insert(" insert  into table_user ..")   

      修改语句: @update(" update table_user set ....")  

      删除语句:@delete(" delete from table_user .....")   

      查询语句:@select(" select * from table_user .....")

    3、下载mybatis的jar包:mybatis-3.1.1-bundle.zip?,网址:http://code.google.com/p/mybatis/

    4、构建自己的mybatis项目,如图所示:

    5、mybatis-config.xml:配置数据源和隐射文件

    <?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://localhost:3306/mybatis?characterEncoding =UTF-8"/>    
    
              <property name="username" value="root"/>       
    
              <property name="password" value="root"/>        
    
            </dataSource>    
    
          </environment>  
    
      </environments>  
    
    <!--  <mappers>      
    
       <mapper resource="mybatis/User.xml"/>  
    
       </mappers>  -->
    
    </configuration>

    注:mappers部分字体是使用实体配置的实现,如果使用注解则不需要添加。

    6、使用注解实现CRUD操作-此处以实现用户管理为例  

    6.1编写接口(interface):实际上就是官方文档所述的Mapper

    public interface UserService {
    
       @Insert(" insert into users (id,username,password) values (#{id},#{username},#{password})") void add(@Param("id")String id,@Param("username") String username,@Param("password")String password); 
    
       @Delete(" delete  from users where id=#{id}") void delete(String id); 
    
       @Update(" update users set username=#{username},password=#{password} where id=#{id}") int update(@Param("username") String username,@Param("password")String password,@Param("id")String id); 
    
       @Select(" select * from users where id=#{id}")  User getUser(String id); 
    
       @Select(" select * from users order by id asc ")  List<User> getUsers();   @Select(" select * from users order by id asc limit #{pageSize} offset #{offset} ")  List<User> getUsersByPage(@Param("offset")int offset,@Param("pageSize") int pageSize);//offset=pageSize*(page-1)}?

    注:使用注解时接口的方法不能重载,否者会产生Mapped Statements collection does not contain value for  异常,另外还应尽量避免配置和注解混合使用的情况。

    6.2获取数据源

    public class GetSqlSessionFactory {  
    
         private  static SqlSessionFactory sqlSessionFactory = null;   
    
         private static GetSqlSessionFactory getSqlSessionFactory = null; 
    
         private GetSqlSessionFactory(){         
    
              String rs = "mybatis-config.xml";   
    
              Reader reader = null;      
    
              try {              
    
                  reader = Resources.getResourceAsReader(rs);    
    
              } catch (IOException e) {     
    
                   e.printStackTrace();       
    
              }          
    
              sqlSessionFactory = new  SqlSessionFactoryBuilder().build(reader);   
    
              //注解方式查询时需要注册mapper  
    
               sqlSessionFactory.getConfiguration().addMapper(UserService.class); 
    
         }  
    
        public static GetSqlSessionFactory getInstance(){  
    
           if(getSqlSessionFactory == null)          getSqlSessionFactory = new GetSqlSessionFactory();        
    
           return getSqlSessionFactory;    
    
        }       
    
        public static SqlSessionFactory getSqlSessionFactory(){      
    
            return sqlSessionFactory;   
    
        }
    
    }

    6.3DAO层:数据访问

    public class UserDao { 
    
      @SuppressWarnings("static-access") 
    
       public void add(String id,String username,String password) {  
    
           SqlSession session =GetSqlSessionFactory.getInstance().getSqlSessionFactory().openSession(); 
    
           try {      
    
              session.getMapper(UserService.class).add(id,username,password);     
    
              session.commit();//提交事务
    
            } catch (Exception e) { 
    
               e.printStackTrace(); 
    
           }finally{  
    
              session.close(); 
    
          }
    
       }   
    
     
    
       @SuppressWarnings("static-access") 
    
       public void delete(String id) {   
    
           SqlSession session=GetSqlSessionFactory.getInstance().getSqlSessionFactory().openSession(); 
    
           try {      
    
                session.getMapper(UserService.class).delete(id);    
    
                session.commit();//提交事务  
    
          } catch (Exception e) {  
    
               e.printStackTrace();
    
           }finally{   
    
            session.close(); 
    
          } 
    
      }
    
      @SuppressWarnings("static-access") 
    
       public int update(String username,String password,String id) { 
    
           int count=0;  
    
          SqlSession session=GetSqlSessionFactory.getInstance().getSqlSessionFactory().openSession();  
    
          try {  
    
              // Map<String, Object> map=new HashMap<String, Object>();    
    
              // map.put("username", user.getUsername());  
    
              // map.put("password", user.getPassword()); 
    
              // session.update("updateUser", map);  
    
              count=session.getMapper(UserService.class).update(username,password,id); 
    
               session.commit();//提交事务
    
           } catch (Exception e) {
    
                count=0;  
    
              e.printStackTrace();  
    
         }finally{
    
                session.close();
    
           }  
    
        return count;
    
      }
    
     @SuppressWarnings("static-access")
    
       public List<User> getUsers(int offset, int pageSize) {  
    
           List<User> users=new ArrayList<User>(); 
    
           SqlSession session =GetSqlSessionFactory.getInstance().getSqlSessionFactory().openSession(); 
    
           try { 
    
                 //users=session.selectList("user_list_page", new User(),new  RowBounds(offset,pageSize));//未测试过 
    
                 //注解方式查询    
    
                 users=session.getMapper(UserService.class).getUsersByPage(offset, pageSize);
    
            } catch (Exception e) {
    
                 e.printStackTrace();
    
            }finally{  
    
               session.close(); 
    
           }    
    
        return users; 
    
      }
    
    }

    6.4servlet实现数据跳转访问

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    
         throws ServletException, IOException {
    
         response.setContentType("text/html;charset=utf-8");
    
         UserService userService=new UserServiceImpl(); 
    
        PrintWriter out = response.getWriter();
    
        String method=request.getParameter("method");
    
        String returnString=""; 
    
        //方法入口  
    
       if(method.equals("users")){  
    
         returnString=new  JsonUtil<User>().getJsonByListObj(userService.getUsers()); 
    
      }else if(method.equals("user")){ 
    
         String id=request.getParameter("id");
    
        returnString=new  JsonUtil<User>().getJsonByEntity(userService.getUser(id)); 
    
      }else if(method.equals("delete")){  
    
         String id=request.getParameter("id");   
    
         Map<String,Object> map=new HashMap<String, Object>();  
    
         map.put("event", method);   
    
         map.put("param", id); 
    
         returnString=new  JsonUtil<User>().getJsonByObject(map); 
    
      }else if(method.equals("update")){   
    
         String id=request.getParameter("id");  
    
         String username=request.getParameter("username");   
    
         String password=request.getParameter("password");  
    
         Map<String,Object> map=new HashMap<String, Object>();
    
         int count=userService.update(username, password,id);   
    
         map.put("status",count==0?false:true); 
    
         map.put("event", method);   
    
         map.put("param", id);  
    
         returnString=new  JsonUtil<User>().getJsonByObject(map); 
    
      }else if(method.equals("paginate")){ 
    
         int limit=Integer.valueOf(request.getParameter("pageSize"));  
    
         int offset=Integer.valueOf(request.getParameter("offset"));   
    
         returnString=new  JsonUtil<User>().getJsonByListObj(userService.getUsersByPage(offset, limit));
    
      }
    
    //输出JSON数据  
    
    out.print(returnString);  
    
    out.flush();
    
    out.close();
    
    }

    至此,注解方式的实现就全部完成了。

    7、XML实体对象配置

    <?xml version="1.0" encoding="UTF-8" ?> 
    
    <!DOCTYPE mapper   PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"   "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
    
      <mapper namespace="com.boonya.mybatis.entity.User">   
    
        <resultMap id="UserResultMap"  type="com.boonya.mybatis.entity.User">    
    
           <result property="id" column="id" jdbcType="VARCHAR"  javaType="string"/>      
    
           <result property="name" column="name" javaType = "string"  jdbcType="VARCHAR"/>    
    
          <result property="createtime" column="createtime" javaType =  "string" jdbcType="VARCHAR"/>
    
          <result property="tilepath" column="tilepath" javaType =  "string" jdbcType="VARCHAR"/>      
    
          <result property="lastlogintime" column="lastlogintime"  javaType = "string" jdbcType="VARCHAR"/>    
    
          <result property="nickname" column="nickname" javaType =  "string" jdbcType="VARCHAR"/>   
    
         <result property="password" column="password" javaType =  "string" jdbcType="VARCHAR"/> 
    
         <result property="picture" column="picture" javaType = "string"  jdbcType="VARCHAR"/> 
    
         <result property="sex" column="sex" javaType = "string"  jdbcType="VARCHAR"/> 
    
         <result property="username" column="username" javaType =  "string" jdbcType="VARCHAR"/> 
    
        </resultMap>
    
     <!-- 一般的SQL查询或操作语句配置-->
    
    <select id="user" parameterType="int" resultType="com.boonya.mybatis.entity.User" 
    
                               resultMap="UserResultMap">select * from users where id=#{id} </select>   
    
      <select id="users"   resultMap="UserResultMap">select * from users order by id asc </select>  
    
       <insert id="addUser" parameterType="com.boonya.mybatis.entity.User" parameterMap="UserResultMap">insert into users (id,name,createtime,email,tilepath,lastloginip,lastlogintime,nickname,password,picture,sessionid,sex,tel,username)   values (#{id},#{name},#{createtime},#{email},#{tilepath},#{lastloginip},#{lastlogintime},#{nickname},#{password},#{picture},#{sessionid},#{sex},#{tel},#{username})</insert>   
    
      <!-- Dynamic SQL 这里的方法未经测试 -->  
    
       <!-- IF -->  
    
       <select id="dynamic_user"   resultMap="UserResultMap">select * from users where username=#{username}    
    
         <if test="name!=null">and name=#{name}</if>  
    
       </select>      
    
        <!-- FOREACH -->    
    
      <select id="dynamic_users"   resultMap="UserResultMap">select * from users name=#{name} and createtime in     
    
        <foreach collection="list" item="classTimeList"  open="(" separator="," close=")">              #{classTimeList}           </foreach>   
    
        </select> 
    
    <!-- choose when  otherwise-->    
    
    <select id="dynamic_findNameLike"  resultMap="UserResultMap">         SELECT * FROM USERS WHERE NAME IS NOT NULL  
    
        <choose> 
    
            <when test="nickname!=null">AND nickname like #{nickname}</when>   
    
          <when test="picture!=null ">AND picture like #{picture}</when>   
    
          <otherwise>AND lastlogintime > #{lastlogintime}</otherwise>  
    
       </choose>
    
     </select>     
    
        <!-- WHERE -->   
    
    <select id="dynamic_findUSERSLike" resultMap="UserResultMap">select * from users  
    
       <where>   
    
        <if test="name!=null">WHERE name LIKE #{name}</if>  
    
       </where> 
    
      </select>   
    
    </mapper>

    注:如果使用配置需将mybatis-config.xml文件的mapper去掉注释:

    <!--  <mappers>      

         <mapper resource="mybatis/User.xml"/>   

        </mappers>  -->

    并且注释掉使用注解的mapper隐射:  

      //注解方式查询时需要注册mapper   

      sqlSessionFactory.getConfiguration().addMapper(UserService.class);

    配置方式是根据SQL语句的隐射去访问的 如UserDao 中的getUsers方法:  

      //xml方式查询 

      //users=session.selectList("users");

    注:刚开始学习mybatis的使用很多地方或细节不是很在意,如某些地方存在问题希望指点一二!

  • 相关阅读:
    JS高级
    函数作用域面试题
    11.14
    11.13
    Redux知识
    react-router-dom
    react 的三大属性
    vuex
    数组的扩展
    函数作用域和 class
  • 原文地址:https://www.cnblogs.com/boonya/p/2824183.html
Copyright © 2020-2023  润新知