SpringMVC 注解事务
- 在需要事务的方法上,添加注解
@Transactional
- 添加配置
<!--事务管理器 -->
<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. 注解+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>
网友评论