持久层框架的基本使用
Spring Data Jpa
pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
pojo
@Data
@Entity
@Table(name = "users")
public class User implements Serializable {
private static final long serialVersionUID = -2811134687378990361L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String password;
private Date birthday;
}
dao
@Repository
public interface UserDao extends JpaRepository<User,Integer> {
}
service
public interface UserService {
Page<User> findAllByPage(Integer page, Integer size);
}
serviceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public Page<User> findAllByPage(Integer page, Integer size) {
Sort sort = Sort.by(Sort.Order.desc("id"));
Pageable pageable = PageRequest.of(page, size, sort);
return userDao.findAll(pageable);
}
}
controller
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/user/findAll")
public ResultVo<Page<User>> findAllByPage(@RequestParam(defaultValue = "0")Integer page,@RequestParam(defaultValue = "5")Integer size){
return ResultVo.of(userService.findAllByPage(page, size));
}
}
vo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultVo<T> {
private Integer code;
private String msg;
private T obj;
public static <T> ResultVo<T> of(Integer code, String msg, T obj) {
return new ResultVo<>(code, msg, obj);
}
public static <T> ResultVo<T> of(T obj) {
return new ResultVo<>(null, null, obj);
}
public static <T> ResultVo<T> of(Integer code, String msg) {
return new ResultVo<>(code, msg, null);
}
}
tkmapper
pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
启动类
@SpringBootApplication
@MapperScan({"com.tmin.dao"})
public class TxDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TxDemoApplication.class, args);
}
}
pojo
@Data
@Table(name = "users")
public class User implements Serializable {
private static final long serialVersionUID = -8813457192818141421L;
private Integer id;
private String name;
private String password;
private Date birthday;
}
dao
@Repository
public interface UserDao extends Mapper<User> {
}
service
public interface UserService {
PageInfo<User> findAllByPage(Integer page, Integer size);
}
serviceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public PageInfo<User> findAllByPage(Integer page, Integer size) {
PageHelper.startPage(page, size, "id desc");
List<User> users = userDao.selectAll();
return PageInfo.of(users);
}
}
controller
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/user/findAll")
public ResultVo<PageInfo<User>> findAllByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer size) {
return ResultVo.of(userService.findAllByPage(page,size));
}
}
vo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultVo<T> {
private Integer code;
private String msg;
private T obj;
public static <T> ResultVo<T> of(Integer code, String msg, T obj) {
return new ResultVo<>(code, msg, obj);
}
public static <T> ResultVo<T> of(T obj) {
return new ResultVo<>(null, null, obj);
}
public static <T> ResultVo<T> of(Integer code, String msg) {
return new ResultVo<>(code, msg, null);
}
}
mypatis-plus
pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
启动类
@SpringBootApplication
@MapperScan({"com.tmin.dao"})
public class MpDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MpDemoApplication.class, args);
}
}
mp配置文件,配置常用插件
@Configuration
public class MybatisPlusConfig {
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
public ISqlInjector iSqlInjector(){
return new LogicSqlInjector();
}
@Bean
@Profile({"dev","test"})
public PerformanceInterceptor performanceInterceptor(){
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
performanceInterceptor.setMaxTime(100);
performanceInterceptor.setFormat(true);
return performanceInterceptor;
}
}
拦截器,对数据进行填充
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("执行插入时填充...");
this.setFieldValByName("birthday",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("执行更新时填充...");
}
}
pojo
@Data
@TableName("users")
public class User implements Serializable {
private static final long serialVersionUID = -8813457192818141421L;
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private String password;
@TableField(fill = FieldFill.INSERT)
private Date birthday;
@Version
private Integer version;
@TableLogic
private Integer deleted;
}
逻辑删除对应 yml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
dao
@Repository
public interface UserDao extends BaseMapper<User> {
}
service
public interface UserService {
Page<User> findAllByPage(Integer page,Integer size);
}
serviceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public Page<User> findAllByPage(Integer page, Integer size) {
Page<User> userPage = new Page<>(page,size);
userPage.setDesc("id");
userDao.selectPage(userPage, null);
return userPage;
}
}
controller
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/user/findAll")
public ResultVo<Page<User>> findAllByPage(@RequestParam(defaultValue = "0")Integer page,@RequestParam(defaultValue = "5")Integer size){
return ResultVo.of(userService.findAllByPage(page,size));
}
}
vo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultVo<T> {
private Integer code;
private String msg;
private T obj;
public static <T> ResultVo<T> of(Integer code, String msg, T obj) {
return new ResultVo<>(code, msg, obj);
}
public static <T> ResultVo<T> of(Integer code, String msg) {
return new ResultVo<>(code, msg, null);
}
public static <T> ResultVo<T> of(T obj) {
return new ResultVo<>(0, "成功", obj);
}
}