SpringBoot整合Mybatis及分页插件pageHelper和事务spring事务分类:
1.声明事务
2.编程事务
spring事务原理:AOP技术 环绕通知进⾏拦截
使⽤spring事务的注意事项:不要Try。因为要将异常抛出给外层
Springboot默认集成事务,只要在⽅法上加上@Transactional注解
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="maven.apach  <modelVersion>4.0.0</modelVersion>
<groupId>cn.e3mall</groupId>
<artifactId>springboot-pageHelper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
springboot aop<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- MySQL依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jta+atomikos解决传统分布式事务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<!-- pagehelper⼀个mybtis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
在application.properties配置⽂件中配置数据源:
spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.sql.jdbc.Driver ample.demo.dao=DEBUG
pagehelper.helperDialect=mysql
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true
Dao层UserMapper.java:
//@Mapper
public interface UserMapper {
@Select("select * from user where name=#{name}")
User findByName(@Param("name")String name);
@Insert("insert into user(name,age) values(#{name},#{age})")
int insert(@Param("name")String name,@Param("age")Integer age); }
Service层UserService.java:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Transactional
//@Datasource(ref="datasource1")
public int insertUser(String name,Integer age) {
int insertUserResult=userMapper.insert(name, age);
int i=1/age;
System.out.println(insertUserResult);
//怎么验证事务开启成功
return insertUserResult;
}
}
web层UserController.java:
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/insertUser")
public Integer insertUser(String name,Integer age) {
return userService.insertUser(name, age);
}
}
启动类:
@SpringBootApplication
@MapperScan(basePackages= {"com.itmayiedu.mapper"})
public class MyBatisApp01 {
public static void main(String[] args) {
SpringApplication.run(MyBatisApp01.class,args);
}
}
实体类在这⾥就省略了。
下⾯是集成mybatis的pagehelper分页插件
IndexController.java:
@RestController
public class IndexController {
@Autowired
private UserService userService;
@RequestMapping("/index")
public PageInfo<User> findUserList(int page,int pageSize){
return userService.findUserList(page, pageSize);
}
UserService.java:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
//page当前页,pagesize表⽰当前展⽰多少个
public PageInfo<User> findUserList(int page,int pageSize){
//mysql查询 limit,oracle 伪列 sqlserver top
//pagehelper帮我们⽣成分页语句
PageHelper.startPage(page,pageSize);//底层实现原理是改写sql语句  List<User>listUser=userMapper.findUserList();
//返回个客户端封装⼀些page对象
PageInfo<User> pageInfoUserList=new PageInfo<User>(listUser);  return pageInfoUserList;
}
}
UserMapper.java:
public interface UserMapper {
@Select("select * from user")
List<User>findUserList();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。