码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis

时间:2018-08-16 17:33:57      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:标签   new   encoding   conf   oca   pen   rom   doctype   注解   

一、MyBatis的介绍

MyBatis是一个支持普通SQL查询存储过程高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。

二、mybatis的使用

1.创建项目,加入jar包

  比如:mybatis-3.1.1.jar

        mysql-connector-java-5.1.7-bin.jar

2.创建数据库以及表内容

3、添加Mybatis的配置文件conf.xml

  在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>
      <environments default="development">
          <environment id="development">
              <transactionManager type="JDBC" />
              <!-- 配置数据库连接信息 -->
              <dataSource type="POOLED">
                 <property name="driver" value="com.mysql.jdbc.Driver" />
                 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                 <property name="username" value="root" />
                 <property name="password" value="nmdnbb123" />
             </dataSource>
         </environment>
     </environments>
 </configuration>

也可以采用将数据库的连接配置信息写在一个properties文件中,然后在conf.xml文件中引用properties文件的方式

  <?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>
  
      <!-- 引用db.properties配置文件 -->
      <properties resource="db.properties"/>
         <environments default="development">
         <environment id="development">
             <transactionManager type="JDBC" />
             <!-- 配置数据库连接信息 -->
             <dataSource type="POOLED">
                 <!-- value属性值引用db.properties配置文件中配置的值 -->
                 <property name="driver" value="${driver}" />
                 <property name="url" value="${url}" />
                 <property name="username" value="${name}" />
                 <property name="password" value="${password}" />
             </dataSource>
         </environment>
     </environments>
     
 </configuration>

4.创建实体类以及操作users表的sql映射文件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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
 例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
   -->
  <mapper namespace="me.gacl.mapping.userMapper">
      <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
      使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
     resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
     User类就是users表所对应的实体类
     -->
     <!-- 
         根据id查询得到一个user对象
      -->
     <select id="getUser" parameterType="int" 
         resultType="me.gacl.domain.User">
         select * from users where id=#{id}
     </select>
     <insert id="addUser" parameterType="me.gacl.domain.User">
         insert into users(name,age) values(#{name},#{age})
     </insert>
     
     <!-- 删除用户(Remove) -->
     <delete id="deleteUser" parameterType="int">
         delete from users where id=#{id}
     </delete>
     
     <!-- 修改用户(Update) -->
     <update id="updateUser" parameterType="me.gacl.domain.User">
         update users set name=#{name},age=#{age} where id=#{id}
     </update>
     
     <!-- 查询全部用户-->
     <select id="getAllUsers" resultType="me.gacl.domain.User">
         select * from users
     </select>
     
 </mapper>

5.在conf.xml文件中注册userMapper.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>
      <environments default="development">
          <environment id="development">
              <transactionManager type="JDBC" />
              <!-- 配置数据库连接信息 -->
              <dataSource type="POOLED">
                 <property name="driver" value="com.mysql.jdbc.Driver" />
                 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                 <property name="username" value="root" />
                 <property name="password" value="nmdnbb123" />
             </dataSource>
         </environment>
     </environments>
     <mappers>
     <mapper resource="me/gacl/mapping/userMapper.xml"/>
     </mappers>
 </configuration>

6.编写测试类进行测试,与下面的测试类大致相同

三、使用MyBatis对表执行CRUD操作——基于注解的实现

1、定义sql映射的接口,并在conf.xml中注册

package me.gacl.mapping;                                                                                   
                                                                                                           
import java.util.List;                                                                                     
import me.gacl.domain.User;                                                                                
import org.apache.ibatis.annotations.Delete;                                                               
import org.apache.ibatis.annotations.Insert;                                                               
import org.apache.ibatis.annotations.Select;                                                               
import org.apache.ibatis.annotations.Update;                                                               
                                                                                                           
/**                                                                                                        
 * @author gacl                                                                                            
 * 定义sql映射的接口,使用注解指明方法要执行的SQL                                                                              
 */                                                                                                        
public interface UserMapperI {                                                                             
                                                                                                           
    //使用@Insert注解指明add方法要执行的SQL                                                                            
    @Insert("insert into users(name, age) values(#{name}, #{age})")                                        
    public int add(User user);                                                                             
                                                                                                           
    //使用@Delete注解指明deleteById方法要执行的SQL                                                                     
    @Delete("delete from users where id=#{id}")                                                            
    public int deleteById(int id);                                                                         
                                                                                                           
    //使用@Update注解指明update方法要执行的SQL                                                                         
    @Update("update users set name=#{name},age=#{age} where id=#{id}")                                     
    public int update(User user);                                                                          
                                                                                                           
    //使用@Select注解指明getById方法要执行的SQL                                                                        
    @Select("select * from users where id=#{id}")                                                          
    public User getById(int id);                                                                           
                                                                                                           
    //使用@Select注解指明getAll方法要执行的SQL                                                                         
    @Select("select * from users")                                                                         
    public List<User> getAll();                                                                            
}                                                                                                          

注册代码

 <mapper class="me.gacl.mapping.UserMapperI"/>

测试类

package me.gacl.test;                                                                                                    
                                                                                                                        
import java.util.List;                                                                                                  
import me.gacl.domain.User;                                                                                             
import me.gacl.mapping.UserMapperI;                                                                                     
import me.gacl.util.MyBatisUtil;                                                                                        
import org.apache.ibatis.session.SqlSession;                                                                            
import org.junit.Test;                                                                                                  
                                                                                                                        
public class Test1 {                                                                               
                                                                                                                        
    @Test                                                                                                               
    public void testAdd(){                                                                                              
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);                                                        
        //得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来                       出来
        UserMapperI mapper = sqlSession.getMapper(UserMapperI.class);                                                   
        User user = new User();                                                                                         
        user.setName("用户xdp");                                                                                          
        user.setAge(20);                                                                                                
        int add = mapper.add(user);                                                                                     
        //使用SqlSession执行完SQL之后需要关闭SqlSession                                                                            
        sqlSession.close();                                                                                             
        System.out.println(add);                                                                                        
    }                                                                                                                   
                                                                                                                        
    @Test                                                                                                               
    public void testUpdate(){                                                                                           
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);                                                        
        //得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来                       出来
        UserMapperI mapper = sqlSession.getMapper(UserMapperI.class);                                                   
        User user = new User();                                                                                         
        user.setId(3);                                                                                                  
        user.setName("孤傲苍狼_xdp");                                                                                       
        user.setAge(26);                                                                                                
        //执行修改操作                                                                                                        
        int retResult = mapper.update(user);                                                                            
        //使用SqlSession执行完SQL之后需要关闭SqlSession                                                                            
        sqlSession.close();                                                                                             
        System.out.println(retResult);                                                                                  
    }                                                                                                                   
                                                                                                                        
    @Test                                                                                                               
    public void testDelete(){                                                                                           
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);                                                        
        //得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来                       出来
        UserMapperI mapper = sqlSession.getMapper(UserMapperI.class);                                                   
        //执行删除操作                                                                                                        
        int retResult = mapper.deleteById(7);                                                                           
        //使用SqlSession执行完SQL之后需要关闭SqlSession                                                                            
        sqlSession.close();                                                                                             
        System.out.println(retResult);                                                                                  
    }                                                                                                                   
                                                                                                                        
    @Test                                                                                                               
    public void testGetUser(){                                                                                          
        SqlSession sqlSession = MyBatisUtil.getSqlSession();                                                            
        //得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来                       出来
        UserMapperI mapper = sqlSession.getMapper(UserMapperI.class);                                                   
        //执行查询操作,将查询结果自动封装成User返回                                                                                       
        User user = mapper.getById(8);                                                                                  
        //使用SqlSession执行完SQL之后需要关闭SqlSession                                                                            
        sqlSession.close();                                                                                             
        System.out.println(user);                                                                                       
    }                                                                                                                   
                                                                                                                        
    @Test                                                                                                               
    public void testGetAll(){                                                                                           
        SqlSession sqlSession = MyBatisUtil.getSqlSession();                                                            
        //得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来                       出来
        UserMapperI mapper = sqlSession.getMapper(UserMapperI.class);                                                   
        //执行查询操作,将查询结果自动封装成List<User>返回                                                                                 
        List<User> lstUsers = mapper.getAll();                                                                          
        //使用SqlSession执行完SQL之后需要关闭SqlSession                                                                            
        sqlSession.close();                                                                                             
        System.out.println(lstUsers);                                                                                   
    }                                                                                                                   
}                                                                                                                       

其中的MyBatisUtil是封装了构建sqlSession的工具类

package me.gacl.util;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {

    /**
     * 获取SqlSessionFactory
     * 
     * @return SqlSessionFactory
     */
    public static SqlSessionFactory getSqlSessionFactory() {
        String resource = "conf.xml";
        InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        return factory;
    }

    /**
     * 获取SqlSession
     * 
     * @return SqlSession
     */
    public static SqlSession getSqlSession() {
        return getSqlSessionFactory().openSession();
    }

    /**
     * 获取SqlSession
     * 
     * @param isAutoCommit true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务 false
     *                     表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务
     * @return SqlSession
     */
    public static SqlSession getSqlSession(boolean isAutoCommit) {
        return getSqlSessionFactory().openSession(isAutoCommit);
    }
}

四、别名的使用

1.parameterType一般都是挺长的一串,我们可以通过在conf.xml文件中<configuration></configuration>标签中添加配置为其添加一个别名

<typeAliases>
    <typeAlias type="me.gacl.domain.User" alias="_User"/>
</typeAliases>

2.通过<package name="me.gacl.domain"/>为这个包下面的所有实体类设置别名,MyBatis默认的设置别名的方式就是去除类所在的包后的简单的类名,比如me.gacl.domain.User这个实体类的别名就会被设置成User。

 五、关联表查询,只需要创建新的sql映射文件Mapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的                                   一的                       
 例如namespace="me.gacl.mapping.classMapper"就是me.gacl.mapping(包名)+classMapper(classMapper.xml文件去除后缀)                                             
  -->                                                                                                                                          
 <mapper namespace="me.gacl.mapping.classMapper">                                                                                              
                                                                                                                                               
     <!--                                                                                                                                      
         根据班级id查询班级信息(带老师的信息)                                                                                                                  
         ##1. 联表查询                                                                                                                             
         SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=1;                                                               
                                                                                                                                               
         ##2. 执行两次查询                                                                                                                           
         SELECT * FROM class WHERE c_id=1;  //teacher_id=1                                                                                     
         SELECT * FROM teacher WHERE t_id=1;//使用上面得到的teacher_id                                                                                
      -->                                                                                                                                      
                                                                                                                                               
     <!--                                                                                                                                      
     方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集                                                                                                            
              封装联表查询的数据(去除重复的数据)                                                                                                               
         select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1                                                               
     -->                                                                                                                                       
     <select id="getClass" parameterType="int" resultMap="ClassResultMap">                                                                     
         select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id}                                                           
     </select>                                                                                                                                 
     <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->                                                                                                     
     <resultMap type="me.gacl.domain.Classes" id="ClassResultMap">                                                                             
         <id property="id" column="c_id"/>                                                                                                     
         <result property="name" column="c_name"/>                                                                                             
         <association property="teacher" javaType="me.gacl.domain.Teacher">                                                                    
             <id property="id" column="t_id"/>                                                                                                 
             <result property="name" column="t_name"/>                                                                                         
         </association>                                                                                                                        
     </resultMap>                                                                                                                              
                                                                                                                                               
     <!--                                                                                                                                      
     方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型                                                                                                        
         SELECT * FROM class WHERE c_id=1;                                                                                                     
         SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值                                                                        
     -->                                                                                                                                       
      <select id="getClass2" parameterType="int" resultMap="ClassResultMap2">                                                                  
         select * from class where c_id=#{id}                                                                                                  
      </select>                                                                                                                                
      <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->                                                                                                    
      <resultMap type="me.gacl.domain.Classes" id="ClassResultMap2">                                                                           
         <id property="id" column="c_id"/>                                                                                                     
         <result property="name" column="c_name"/>                                                                                             
         <association property="teacher" column="teacher_id" select="getTeacher"/>                                                             
      </resultMap>                                                                                                                             
                                                                                                                                               
      <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher">                                                         
         SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}                                                                             
      </select>                                                                                                                                
                                                                                                                                               
 </mapper>                                                                                                                                     

 

 MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:

 

roperty:对象属性的名称

javaType:对象属性的类型

column:所对应的外键字段名称

select:使用另一个查询封装的结果

 

如果是一对多的话

 <!--                                                                                                                             
         根据classId查询对应的班级信息,包括学生,老师                                                                                               
      -->                                                                                                                         
     <!--                                                                                                                         
     方式一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集                                                                                             
     SELECT * FROM class c, teacher t,student s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND  c.c_id=1                     
      -->                                                                                                                         
     <select id="getClass3" parameterType="int" resultMap="ClassResultMap3">                                                      
         select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.C_id=s.class_id and  c.c_id=#{id}             
     </select>                                                                                                                    
     <resultMap type="me.gacl.domain.Classes" id="ClassResultMap3">                                                               
         <id property="id" column="c_id"/>                                                                                        
         <result property="name" column="c_name"/>                                                                                
         <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher">                                   
             <id property="id" column="t_id"/>                                                                                    
             <result property="name" column="t_name"/>                                                                            
         </association>                                                                                                           
         <!-- ofType指定students集合中的对象类型 -->                                                                                        
         <collection property="students" ofType="me.gacl.domain.Student">                                                         
             <id property="id" column="s_id"/>                                                                                    
             <result property="name" column="s_name"/>                                                                            
         </collection>                                                                                                            
     </resultMap>                                                                                                                 
                                                                                                                                  
     <!--                                                                                                                         
         方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型                                                                                       
             SELECT * FROM class WHERE c_id=1;                                                                                    
             SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值                                                       
             SELECT * FROM student WHERE class_id=1  //1是第一个查询得到的c_id字段的值                                                         
      -->                                                                                                                         
      <select id="getClass4" parameterType="int" resultMap="ClassResultMap4">                                                     
         select * from class where c_id=#{id}                                                                                     
      </select>                                                                                                                   
      <resultMap type="me.gacl.domain.Classes" id="ClassResultMap4">                                                              
         <id property="id" column="c_id"/>                                                                                        
         <result property="name" column="c_name"/>                                                                                
         <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher" select="getTeacher2"></association>
         <collection property="students" ofType="me.gacl.domain.Student" column="c_id" select="getStudent"></collection>          
      </resultMap>                                                                                                                
                                                                                                                                  
      <select id="getTeacher2" parameterType="int" resultType="me.gacl.domain.Teacher">                                           
         SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}                                                                
      </select>                                                                                                                   
                                                                                                                                  
      <select id="getStudent" parameterType="int" resultType="me.gacl.domain.Student">                                            
         SELECT s_id id, s_name name FROM student WHERE class_id=#{id}                                                            
      </select>                                                                                                                   

MyBatis中使用collection标签来解决一对多的关联查询,ofType属性指定集合中元素的对象类型。

mybatis

标签:标签   new   encoding   conf   oca   pen   rom   doctype   注解   

原文地址:https://www.cnblogs.com/14cn/p/9487225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!