美文网首页
mybatis 相关

mybatis 相关

作者: 一颗北上广的心 | 来源:发表于2018-11-14 10:45 被阅读0次

SpringMVC 注解事务

  1. 在需要事务的方法上,添加注解
 @Transactional
  1. 添加配置
    <!--事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 使用注解事务,需要添加Transactional注解属性 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

如果发现事务未生效,请确认@Transactional标注的方法及其子方法没有try/catch。

对于service中调用自身方法,自身方法声明了Transactional注解却事务未生效

@Service
public class MyServiceImpl implements MyService{

  public void test(){
    doTransaction();
  }

  @Transactional
  public void doTransaction(){}
}

外部controller调用 MyServiceImpl.test()方法,其中的doTransaction事务无效;
原因: Spring数据库事务约定的实现原理是AOP,而AOP原理是动态代理,在自调用过程中,是类自身的调用,而不是代理对象的调用,就不会产生AOP.

解决方案: 可以从容器中取出service来实现事务

@Service
public class MyServiceImpl implements MyService, ApplicationContextAware{
  
  private ApplicationContext applicationContext = null;
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }
  
  @Override
  public void test(){
    MyService service = applicationContext.getBean(MyService.class);
    service.doTransaction();
  }
  
  @Override
  @Transactional
  public void doTransaction(){}
}

Mybatis 级联查询相关

现有学生、老师两个类,关系是N个学生 - 1个老师

public class Student {
    private int id;
    private String name;
    private int teacherId;
    private Teacher teacher;

    /*get set */
}
public class Teacher {

    private int id;
    private String name;
    private List<Student> students;

    /*get set*/
}

数据库同实体类,所见即所得;

  1. 查询学生同时,查询学生的老师
1. 注解+JOIN+automapping
[mapper]:
        @Select("SELECT s.id,s.name, t.id AS `teacher.id`, t.name AS `teacher.name` FROM tbl_student s LEFT JOIN tbl_teacher t ON s.t_id = t.id ")
        List<Student> selectStudentsWithTeacherInfo();


2. XML + association
[StudentMapper.java]:
        List<Student> selectStudentsWithTeacherInfoInXml();
[StudentMapper.xml]
        <resultMap id="studentMap" type="Student">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="t_id" property="teacherId"/>
            <association property="teacher" column="t_id"  select="com.example.springbootdemo.mybatis.mapper.TeacherMapper.selectById" />
        </resultMap>
        <select id="selectStudentsWithTeacherInfoInXml" resultMap="studentMap">
            SELECT * FROM tbl_student
        </select>
[TeacherMapper.xml]:
        <select id="selectById" resultType="Teacher">
            SELECT id,name FROM tbl_teacher WHERE id = #{0}
        </select>


3. xml + JOIN + association
[StudentMapper.java]:
        List<Student> selectStudentsJoinTeacherInfoInXml();
[StudentMapper.xml]
        <resultMap id="studentJoinMap" type="Student">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="t_id" property="teacherId"/>
            <association property="teacher" javaType="Teacher">
                <id column="tid" property="id"/>
                <result column="tname" property="name"/>
            </association>
        </resultMap>
        <select id="selectStudentsJoinTeacherInfoInXml" resultMap="studentJoinMap">
            SELECT
              s.id AS id,
              s.name AS name,
              t.id AS tid,
              t.name AS tname
            FROM tbl_student s
            LEFT JOIN tbl_teacher t ON t.id = s.t_id
        </select>

2.查询老师的同时,查询学生

1. xml + collection
[TeacherMapper.java]:
List<Teacher> selectTeacherWithStudents();
[TeacherMapper.xml]
        <resultMap id="teacherMap" type="Teacher">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <collection property="students" column="id" select="com.example.springbootdemo.mybatis.mapper.StudentMapper.selectByTid"/>
        </resultMap>
        <select id="selectTeacherWithStudents" resultMap="teacherMap">
            SELECT * FROM tbl_teacher
        </select>
[StudentMapper.xml]
        <select id="selectByTid" resultType="Student" >
            SELECT * FROM tbl_student WHERE t_id = #{0}
        </select>


2.XML + JOIN + collection
[TeacherMapper.java]
        List<Teacher> selectTeacherJoinStudents();
[TeacherMapper.xml]
        <resultMap id="teacherJoinMap" type="Teacher">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <collection property="students" ofType="Student">
                <id column="sid" property="id"/>
                <result column="sname" property="name"/>
            </collection>
        </resultMap>
        <select id="selectTeacherJoinStudents" resultMap="teacherJoinMap">
            SELECT
              t.id,
              t.name,
              s.id AS sid,
              s.name AS sname
            FROM tbl_teacher t
            LEFT JOIN tbl_student s ON s.t_id = t.id
        </select>

相关文章

  • spring整合mybatis-XML形式

    Spring-MyBatis Spring整合MyBatis 导入包 spring相关包 mybatis相关包 m...

  • SSM(Spring+SpringMVC+MyBatis)整合记

    项目结构 依赖jar包 mybatis 相关 mybatis-3.4.1.jar mybatis-spring-1...

  • Spring之整合MyBatis

    十一、整合MyBatis 目录: 导入相关jar包、MyBatis代码编写、MyBatis-Spring 1.导入...

  • mybatis 相关

    SpringMVC 注解事务 在需要事务的方法上,添加注解 添加配置 如果发现事务未生效,请确认@Transact...

  • Mybatis 相关

    一、#{}和${}的区别是什么? 理解: 二、怎么处理实体类中的属性名和表中的字段名不一致? 三、 模糊查询lik...

  • mybatis相关

    不要试图挑战锻炼自己的自律,要想着把欲望彻底消除。 封装流程 mybatis在jdbc上进行封装 把数据库的链接 ...

  • 2020-04-20 Mybatis相关知识

    Mybatis相关知识 Mybatis之多参传递 注解方式传递参数 1.在mappper.xml中编写相关sql语...

  • Mybatis,hibernate相关

    Mybatis相关 1.Mybatis是什么? 2.为什么选择Mybatis? 3、#{}和${}的区别是什么? ...

  • Mybatis.hibernate相关

    Mybatis相关 1.Mybatis是什么? 2.为什么选择Mybatis? 3、#{}和${}的区别是什么? ...

  • Java面试——MyBatis 相关

    Java面试——MyBatis 相关 MyBatis原理 1,MyBatis 是一个被广泛应用的持久化框架。先创建...

网友评论

      本文标题:mybatis 相关

      本文链接:https://www.haomeiwen.com/subject/gludfqtx.html