分页插件直接作为SqlSessionFactoryBean里面的一个属性直接注入即可:
<!--整合分页插件-->
<property name="plugins">
<!--从源码我们知道要一个Interceptor数组-->
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
同时我们还需要事务管理,所以需要把事务管理作为一个Bean交给Spring。注意头部也要加入tx命名空间。
<!--开启事务管理,事务管理需要datasource,我们就给他我们创建好的-->
<bean id="txmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txmanager"/>
这个时候我们就可以去我们的Service接口上面加上事务的注解来进行管理了。记住是接口而不是实现类,因为接口的应用更加广泛。
@Transactional(readOnly = true)
public interface UserService {
@Transactional(readOnly = false)
public boolean save(User user);
@Transactional(readOnly = false)
public boolean update(User user);
@Transactional(readOnly = false)
public boolean delete(Integer uuid);
public User get(Integer uuid);
public PageInfo<User> getAll(int page, int size);
//业务层与Dao层不同,所以这里实现的是一个登录的操作,而不是与数据库交互获得User,所以修改了方法名
public User login(String userName,String password);
}
通过类上加@Transactional(readOnly = true)而方法上@Transactional(readOnly = false)来实现查的不加锁,而增删改的时候加锁,以此来提高效率。