mybatis高级(入门回顾)

Posted cnsdhzzl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis高级(入门回顾)相关的知识,希望对你有一定的参考价值。

首先入门案例(并且拿到新增记录当前id)

1.创建mybatis-config.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>
    <!-- 使用别名 -->
    <typeAliases>
        <!-- 为包下所有类使用别名,默认是简单类名 -->
        <package name="cn.cnsdhzzl.entity" />
        <!-- 对单个类,按类型名使用别名 -->
        <!-- <typeAlias type="cn.cnsdhzzl.entity.Student" alias="student" /> -->
    </typeAliases>

    <!-- 开发模式 -->
    <environments default="development">
        <environment id="development">
            <!-- 使用jdbc的事务 -->
            <transactionManager type="JDBC" />
            <!-- 使用自带的连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
                <property name="username" value="**" />
                <property name="password" value="**" />
            </dataSource>
        </environment>
    </environments>

    <!-- 映射小配置 -->
    <mappers>
        <mapper resource="cn/cnsdhzzl/dao/StudentDAO.xml" />
    </mappers>
</configuration>

 

 

2.创建分层搭建架构

 

3.创建StudentDAO.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="cn.cnsdhzzl.dao">
    <!-- 添加操作 -->
    <insert id="insertStudent">
        insert into student(id,name,address,sex)
        values(seq_ssm.nextval,#{name},#{address},#{sex})
        <selectKey keyProperty="id" resultType="int">
            select
            seq_ssm.currval from dual
        </selectKey>
    </insert>

    <!-- 删除的id名称随便写 -->
    <delete id="deleteStudentById">
        delete student where id=#{id}
    </delete>

    <!-- 修改操作 -->
    <update id="updateStudent">
        update student set
        name=#{name},address=#{address},sex=#{sex} where id=#{id}
    </update>

    <!-- 返回集合要加resultType -->
    <select id="findAll" resultType="student">
        select * from student
    </select>

    <!-- 方法一 -->
    <select id="likeSelectByStr" resultType="student">
        select * from student
        where name like \'%${value}%\'
    </select>
    <!-- 方法二 -->
    <!-- <select id="likeSelectByStr" resultType="student"> select * from student 
        where name like concat(\'%\',#{stuname},\'%\') </select> -->

    <!-- 方法一 -->
    <select id="likeSelectByEntity" resultType="student">
        select * from student
        where name like \'%${name}%\'
    </select>
    <!-- 方法二 -->
    <!-- <select id="likeSelectByEntity" resultType="student"> select * from 
        student where name like concat(\'%\',#{stuname},\'%\') </select> -->

</mapper>

 

4.实现接口方法

@Override
    public Integer addStudent(Student stu) {
        SqlSession sqlSession = SessionUtil.getSqlSession();
        int result = sqlSession.insert("insertStudent", stu);

        sqlSession.commit();
        sqlSession.close();

        System.out.println("保存结果" + result);
        return result;
    }

 

5.测试

@Test
    public void frist() {
        Student stu = new Student("BBB", "北京", "男");
        StudentDaoImpl dao = new StudentDaoImpl();
        dao.addStudent(stu);
        System.out.println("成功");
    }

 结果图

 

注意事项:

语句后面不能加\';\'(分号),会报sql异常

 

以上是关于mybatis高级(入门回顾)的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis入门---一对多多对多

MyBatis高级特性

mybatis总结回顾

MyBatis入门详解——附代码

MyBatis 入门

myBatis简单入门