zoukankan      html  css  js  c++  java
  • JavaEE--Mybatis学习笔记(三)--单表的CURD

    1.插入后获取id

    • 在dao中定义方法
    • mapper中写sql语句
      <insert id="insertStudentCacheId" parameterType="Student">
            insert into student(name,age,score) value(#{name},#{age},#{score})
            <selectKey resultType="int" keyProperty="id" order="AFTER">
            <!-- order id的生成和insert语句的先后顺序关系 mysql 先执行insert 再写到id的那个字段里 所以生成id放在insert后面  不写 默认根据你的数据库来选择-->
                select @@identity
            </selectKey>
        </insert>
    • 实现类中覆写方法
     1 @Override
     2     public void insertStudentCacheId(Student student) {
     3         try {
     4             sqlSession = MyBatisUtils.getSqlSession();
     5  
     6             sqlSession.insert("insertStudentCacheId", student);
     7 
     8             sqlSession.commit();
     9         } finally{
    10             if(sqlSession != null){
    11                 //用了close() 就不需要事务的回滚
    12                 sqlSession.close();
    13             }
    14         }
    15         
    16     }
    • Test结果

    2.删除

    • 在dao中定义方法
    • mapper中写sql语句
      <delete id="deleteStudentById">
            delete from student where id = #{ryanxu}<!-- 这里的#{}仅仅是个占位符,里面放什么都可以 -->
            
        </delete>
    • 实现类中覆写方法
     1 @Override
     2     public void deleteStudentById(int id) {
     3         try {
     4             sqlSession = MyBatisUtils.getSqlSession();
     5  
     6             sqlSession.delete("deleteStudentById", id);
     7 
     8             sqlSession.commit();
     9         } finally{
    10             if(sqlSession != null){
    11                 //用了close() 就不需要事务的回滚
    12                 sqlSession.close();
    13             }
    14         }
    15         
    16     }
    • Test结果

    3.修改

    • 在dao中定义方法
    • mapper中写sql语句
        <update id="updateStudent">
            update student set name = #{name},age = #{age},score =#{score} where id = #{id}
        </update>
    • 实现类中覆写方法
     1 @Override
     2     public void updateStudent(Student student) {
     3         try {
     4             sqlSession = MyBatisUtils.getSqlSession();
     5  
     6             sqlSession.delete("updateStudent", student);
     7 
     8             sqlSession.commit();
     9         } finally{
    10             if(sqlSession != null){
    11                 //用了close() 就不需要事务的回滚
    12                 sqlSession.close();
    13             }
    14         }
    15         
    16     }
    • Test结果

    4.查询所有

    • 在dao中定义方法
    • mapper中写sql语句
      <select id="selectAllStudents" resultType="Student"> <!-- 需要定义resultType -->
            select id,name,age,score from student
        </select>
    • 实现类中覆写方法
     1     @Override
     2     public List<Student> selectAllStudents() {
     3         List<Student> students = new ArrayList<Student>();
     4         try {
     5             sqlSession = MyBatisUtils.getSqlSession();
     6  
     7             students = sqlSession.selectList("selectAllStudents");
     8         } finally{
     9             if(sqlSession != null){
    10                 //用了close() 就不需要事务的回滚
    11                 sqlSession.close();
    12             }
    13         }
    14         return students;
    15     }
    根据学习笔记(二) 知道了增删改底层都是调用的update方法 但是查询就不需要调用update方法了,所以可以把sqlSession.submit()去掉
    • Test结果

    5.查询返回Map

    • 在dao中定义方法
    • mapper中写sql语句
        <select id="selectAllStudents" resultType="Student"><!-- 需要定义resultType -->
            select id,name,age,score from student
        </select>
    • 实现类中覆写方法
     1 @Override
     2     public Map<String, Object> selectAllStudentsMap() {
     3         Map<String, Object> map = new HashMap<String, Object>();
     4         try {
     5             sqlSession = MyBatisUtils.getSqlSession();
     6             map = sqlSession.selectMap("selectAllStudents", "name"); /*mapKey写的是查询出来的对象的属性 name score age这样的*/
     7         } finally{
     8             if(sqlSession != null){
     9                 //用了close() 就不需要事务的回滚
    10                 sqlSession.close();
    11             }
    12         }
    13         return map;
    14     }
    • Test结果

    Test中 先写dao.selectAllStudentsMap() 再选中按 Alt + Shift + L 生成extract local variable

    6.根据id来查询

    • 在dao中定义方法
    • mapper中写sql语句
        <select id="selectById" resultType="Student">
            select id,name,age,score from student where id = #{ryanxu}
        </select>
    • 实现类中覆写方法
     1 @Override
     2     public Student selectStudentById(int id) {
     3         Student student = null;
     4         try {
     5             sqlSession = MyBatisUtils.getSqlSession();
     6             student = sqlSession.selectOne("selectById", id);
     7         } finally{
     8             if(sqlSession != null){
     9                 //用了close() 就不需要事务的回滚
    10                 sqlSession.close();
    11             }
    12         }
    13         return student;
    14     }
    • Test结果

    7.模糊查询

    • 在dao中定义方法
    • mapper中写sql语句
        <select id ="selectByName" resultType="Student">
            <!-- select id,name,age,score from student where name like concat('%',#{ryanxu},'%') -->
            <!-- select id,name,age,score from student where name like '%${value}%' -->
             select id,name,age,score from student where name like '%' #{ryanxu} '%'
        </select>
    • 实现类中覆写方法
     1 @Override
     2     public List<Student> selectStudentsByName(String name) {
     3         List<Student> students = null;
     4         try {
     5             sqlSession = MyBatisUtils.getSqlSession();
     6  
     7             students = sqlSession.selectList("selectByName",name);
     8         } finally{
     9             if(sqlSession != null){
    10                 //用了close() 就不需要事务的回滚
    11                 sqlSession.close();
    12             }
    13         }
    14         return students;
    15     }
    • Test结果

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    回顾学习笔记(一)(二)(三)

    •  三层架构

    •  工作原理
    • 源码分析
    1. 主要使用的是SqlSession<--SqlSessionFactory.openSession<--SqlSessionFactoryBuilder.build("主配置文件").
    2. openSession() 无参的autocommit 是false的
    3. 输入流是由build方法里有关闭
    4. 增删改底层实际上是update
    5. openSession的底层是对一些变量进行初始化 配置文件、执行器、自动提交、dirty
      1.   增删改的第一句话都是将dirty设置为true,同时也是对事物的提交,提交完就将dirty设为false
      2.   没有提交,中间发生异常,发生回滚,通过SqlSession 的 close来实现的
    • * >=0 ; +>=1 ; ?<=1 ; 无符号就是1有且只有1 选中标签按F2就可以查看标签了
    • 映射文件:
    1. 里的根标签<mapper> 里的<namespace> 就相当于人名里的姓 id 就相当于名
    2. 增删改 可以传参数过来 parameterType ="..."
    3. 查询 resultType 不能省 封装成什么对象 查询的是id,name,age,score 返回的是个对象
    • 主配置文件:
    1. properties jdbc连接四要素属性文件
    2. typeAliases 别名 <package name="com.ryanxu.beans"/>
    3. environments-->environment 包括jdbc属性文件
      1. 事务管理器
      2. 数据源 一般使用的是连接池技术
    4. 注册映射文件
  • 相关阅读:
    软件自动化测试工程师面试题集锦(1)
    IDEA Maven无法引入org.openjfx:javafx.base:11.0.0-SNAPSHOP
    Maven的dependency和dependencyManagement的区别
    在idea中创建多模块的SpringBoot项目
    MAVEN中三种packaging方式
    IntelliJ IDEA创建maven多模块项目
    Spring Boot 中使用 thrift 入门
    在IntelliJ IDEA使用.gitignore插件的方法和作用
    Git
    TortoiseGit安装、配置
  • 原文地址:https://www.cnblogs.com/windbag7/p/9361686.html
Copyright © 2011-2022 走看看