MyBatis基础

2020-03-10  本文已影响0人  煗NUAN

一.MyBatis简介

a.简介

b.框架特点

c.总体流程

d.MyBatis功能架构分层

e.MyBatis和数据交互的方式

f.配置文件mybatis.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>
    <!--引入外部配置文件db.properties-->
    <properties resource="db.properties" />

    <!--给当前mybatis项目添加日志功能,该STDOUT_LOGGING值的好处是不用添加第三方jar包就可以有日志输出-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--配置mybatis环境变量-->
    <environments default="development">
        <environment id="development">
            <!--配置JDBC事务控制,由mybatis进行管理-->
            <transactionManager type="JDBC"/>
            <!--配置数据源,采用mybatis连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${user}"/>
                <property name="password" value="${pass}"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载映射文件-->
    <mappers>
        <!--使用资源的路径-->
        <mapper resource="mapper/AddressMapper.xml" />
        <mapper resource="mapper/DetailsMapper.xml" />
        <!-- <mapper resource="com/mybrtis/mapper/UserMapper.xml"/> -->
        <!-- 使用资源的绝对路径<mapper url=""/> -->
        <!--
            Mapper接口的全类名
            要求:Mapper接口的名称与映射文件名称一致
                </configuration>必须在同一个目录下
                <mapper class="com.qf.mapper.UserMapper"/>
           -->
        <!-- 加载某个包下的映射文件 (推荐)
            要求:Mapper接口的名称与映射文件名称一致
            必须在同一个目录下
        -->
    </mappers>
</configuration>

g.映射文件xxxMapper.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">
    <!--
    namespace:配置名称空间,对配置的statement进行分类管理
    此时名称可以任意
    当使用Mapper代理时,namespace具有特殊的含义与功能
    -->
<mapper namespace="pojo.UserMapper">

    <!--
        根据id查询用户,User findById(int id)
        select:配置查询语句
        id:可以通过id找到执行的statement,statement唯一标识
        parameterType:输入参数类型
        resultType:输出结果类型
        #{}:相当于占位符
        #{id}:其中的id可以表示输入参数的名称,如果是简单类型名称可以任意
    -->

    <!--查询所有的用户信息-->
    <select id="selectAllUser" resultType="com.mybatis.pojo.UserInfo">
        select * from userinfo;
    </select>

    <!--根据用户信息数量-->
    <select id="selectUserNum" resultType="int">
        select count(1) from userinfo;
    </select>

    <!--根据id查询用户信息-->
    <select id="selectOneUser" resultType="com.mybatis.pojo.UserInfo">
        select * from userinfo where id=#{uid};
    </select>

    <!--limit查询几条数据-->
    <select id="selectUserPage" resultType="com.mybatis.pojo.UserInfo">
        select * from userinfo where sex=#{sex} limit #{startPage}, #{pagesize};
    </select>

    <!--新增用户信息-->
    <!-- 添加用户
        #{user}:名称与类中的属性名一致,可以使用对象接收,也可使用map接收
    -->
    <insert id="insertUser">
        insert into userinfo values (default ,#{userId},#{realName},#{sex},#{email},#{phone},default ,#{address});
    </insert>

    <!--根据用户id更新用户信息-->
    <update id="updataUserById">
        update userinfo SET realName=#{realName} WHERE id=#{id};
    </update>

    <!--根据id删除用户-->
    <delete id="deleteUserById">
        delete from userinfo where id=#{uid};
    </delete>
</mapper>

二.MyBatis的简单使用

a.获取SqlSession对象

//1、读取配置文件
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2、根据配置文件创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3、SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();

//或者
private SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml");
private SqlSession ss=ssf.openSession(true);    //设置自动提交

b.利用SqlSession实现CRUD操作

<!-- 添加用户 -->
<!-- selectKey:查询主键,在标签内需要输入查询主键的sql -->
<!-- order:指定查询主键的sql和insert语句的执行顺序,相当于insert语句来说 -->
<!-- LAST_INSERT_ID:该函数是mysql的函数,获取自增主键的ID,它必须配合insert语句一起使用 -->

<insert id="addUser" parameterType="com.meng.po.User”>
<selectKey keyProperty="id" resultType="int" order="AFTER">
    SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO USER(username,birthday,sex,address)
    VALUES(#{username},#{birthday},#{sex},#{address})
</insert>
                                                          
或者
<insert id="insertUser">
        insert into userinfo values (default ,#{userId},#{realName},#{sex},#{email},#{phone},default ,#{address});
    </insert>                                                      
<delete id="deleteUserById">
        delete from userinfo where id=#{uid};
</delete>
<update id="updataUserById">
        update userinfo SET realName=#{realName} WHERE id=#{id};
</update>
<!--
    根据id查询用户,User findById(int id)
    select:配置查询语句
    id:可以通过id找到执行的statement,statement唯一标识
    parameterType:输入参数类型
    resultType:输出结果类型
    #{}:相当于占位符
    #{id}:其中的id可以表示输入参数的名称,如果是简单类型名称可以任意
-->
<select id="findById" parameterType="int" resultType="com.meng.domain.User" >
    select * from user where id=#{id}
</select>
<!--
    根据用户名称来模糊查询用户信息列表;
    ${}:表示拼接sql语句
    ${value}:表示输入参数的名称,如果参数是简单类型,参数名称必须是value
-->
<select id="findByUsername" parameterType="java.lang.String"
resultType="com.meng.domain.User">
    select * from user where username like '%${value}%'
</select>

c.利用MyBatis实现分页查询

    <select id="selectUserPage" resultType="com.mybatis.pojo.UserInfo">
        select * from userinfo where sex=#{sex} limit #{startPage}, #{pagesize};
    </select>
//测试
    @Test
    public void queryUserPage(){
        Map<String,Integer> map=new HashMap<String, Integer>();
        map.put("sex",2);
        map.put("startPage",1);
        map.put("pagesize",2);
        List<UserInfo> users=ss.selectList("pojo.UserMapper.selectUserPage",map);
        for (UserInfo user : users) {
            System.out.println(user.toString());
        }
    }
<!--注解传参-->
<select id="selectByPage2" resultType="com.meng.domain.User">
    SELECT * FROM user LIMIT #{offset}, #{pagesize}
</select>

接口

/**
* 根据分页参数查询
* @param offset 偏移量
* @param pagesize 每页条数
* @return 分页后的用户列表
*/
public List<User> selectByPage2(@Param(value="offset")int offset,@Param(value="pagesize")int pagesize);

测试

@Test
public void testSelectAuthorByPage2(){
    SqlSession sqlSession = sqlSessionFactory.openSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List<User> users = mapper.selectByPage2(0,2);
    for (User user : users) {
        System.out.println(user.toString());
    }
}
<!-- 分页:序号传参 MyBatis3.4.4版后不能直接使用#{0}要使用 #{arg0} -->
    <select id="selectByPage3" resultType="com.qf.domain.User">
        SELECT * FROM user LIMIT #{arg0}, #{arg1}
    </select>

接口

/**
* 根据分页参数查询
* @param offset 偏移量
* @param pagesize 每页条数
* @return 分页后的用户列表
*/
public List<User> selectByPage3(int offset, int pagesize);

测试

@Test
public void testSelectAuthorByPage3(){
    SqlSession sqlSession = sqlSessionFactory.openSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List<User> users = mapper.selectByPage3(0,2);
    for (User user : users) {
        System.out.println(user.toString());
    }
}

d.返回Map类型查询结果

<select id="getAllUsers" resultType="java.util.Map">
    SELECT * FROM user
</select>
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<Map<String, Object>> allUsers = mapper.getAllUsers();
for (Map<String, Object> allUser : allUsers) {
    Set<String> strings = allUser.keySet();
    for (String key : strings) {
        System.out.print("key="+key+"\t");
        Object o = allUser.get(key);
        System.out.println(o);
    }
}

三.Mapper映射与Mapper接口注解

a.使用Mapper映射器

b.ResultMap映射定义

<!-- 定义resultMap -->
<!--
    [id]:定义resultMap的唯一标识
    [type]:定义该resultMap最终映射的pojo对象
    [id标签]:映射结果集的唯一标识列,如果是多个字段联合唯一,则定义多个id标签
    [result标签]:映射结果集的普通列
    [column]:SQL查询的列名,如果列有别名,则该处填写别名
    [property]:pojo对象的属性名
-->
<resultMap type="user" id="userResultMap">
    <id column="id_" property="id"/>
    <result column="username_" property="username"/>
    <result column="sex_" property="sex"/>
</resultMap>
<!-- 根据ID查询用户信息(resultMap)-->
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
    SELECT id id_,username username_,sex sex_ FROM USER WHERE id = #{id}
</select>
//根据ID查询用户信息(resultMap)
public User findUserByIdResultMap(int id);
//定义Statement使用resultMap映射结果集时,Mapper接口定义方法的返回值类型为mapper映射文件中resultMap的type类型
@Test
public void findUserByIdResultMapTest() {
    // 创建SqlSession
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 通过SqlSession,获取mapper接口的动态代理对象
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    // 调用mapper对象的方法
    User user = userMapper.findUserByIdResultMap(1);
    System.out.println(user);
    // 关闭SqlSession
    sqlSession.close();
}

c.Mapper接口注解

/**
* 插入记录,手动分配主键
*/
@Insert("INSERT INTO t_user (id, username, passwd) VALUES (#{id}, #{username}, #{passwd})")
int addUserAssignKey(User user);

​ 表自增主键
​ 自增主键对应着XML配置中的主键回填

/**
* 插入记录,数据库生成主键
* 使用Option来对应着XML设置的select标签的属性,userGeneratordKeys表示要使用自增主键,keyProperty用来指定主键字段的字段名。
* @param user
* @return
*/
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("INSERT INTO t_user (username, passwd) VALUES (#{username}, #{passwd})")
int addUserGeneratedKey(User user);
@Delete("DELETE FROM t_user WHERE id=#{id}")
int delete(Long id);
@Update("UPDATE t_user SET username=#{username}, passwd=#{passwd} WHERE id=#{id}")
int update(User user);
上一篇 下一篇

猜你喜欢

热点阅读