zoukankan      html  css  js  c++  java
  • MyBatis Plus使用学习

    1、配置数据库:

    1 server:
    2   port: 8020
    3   
    4 spring:
    5   datasource:
    6     driver-class-name: com.mysql.jdbc.Driver
    7     url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
    8     username: root
    9     password: root

    2、建立实体类:

    1 public class User extends Model<User> {
    2     private static final long serialVersionUID = 1L;
    3     
    4     private Long id;
    5     private String name;
    6     private Integer age;
    7     private String email;
    8     .............
    9 }

    3、建立mapper文件:

    1 public interface UserMapper extends BaseMapper<User> {
    2     .............
    3 }

    4、定义service

     1 public interface IUserService extends IService<User> {
     2     User getUserById(Long id);
     3 }
     4 
     5 @Service(value = "userService")
     6 public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
     7 
     8     @Resource
     9     private UserMapper userMapper;
    10 
    11     @Override
    12     public User getUserById(Long id) {
    13         return userMapper.selectById(id);
    14     }
    15 }

    5、实现controller

     1 @Controller
     2 @RequestMapping("/user")
     3 public class UserController {
     4 
     5     @Autowired
     6     private IUserService userService;
     7 
     8     @RequestMapping("/{id}")
     9     @ResponseBody
    10     public User getItem(@PathVariable(value = "id") Long id) {
    11         User user = userService.getUserById(id);
    12         return user;
    13     }
    14 }

    6、相关配置:

    (1) Spring Boot配置:

    配置@MapperScan注解,

    1 @SpringBootApplication
    2 @MapperScan("com.qingxuan.mybatis.plus.study.demo1")
    3 public class MybatisPlusStudyDemo1Application {
    4 
    5     public static void main(String[] args) {
    6        SpringApplication.run(MybatisPlusStudyDemo1Application.class, args);
    7     }
    8 }

    (2) Spring MVC配置:

    配置MapperScan:

    1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    2     <!-- 自动扫描 com.imooc.dao下的interface,并加入IOC容器 -->
    3     <property name="basePackage" value="com.imooc.dao" />
    4     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    5 </bean>

    调整sqlSessionFactory为MyBatis Plus的sqlSessionFactory:

     1 <bean id="sqlSessionFactory"
     2     class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
     3     <property name="dataSource" ref="dataSource" />
     4     <!-- 配置MyBatis的配置的文件 -->
     5     <property name="configLocation" value="classpath:mybatis.xml"></property>
     6     <!-- 注入MP的全局策略配置  -->
     7     <property name="globalConfig"  ref="globalConfiguration"></property>
     8     <!-- 插件注册 -->
     9     <property name="plugins">
    10        <list>
    11            <!-- 注册分页插件 -->
    12            <bean  class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"></bean>
    13            <!-- 注册性能分析插件 -->
    14            <bean  class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
    15                <property name="format" value="true"></property>
    16            </bean>
    17        </list>
    18     </property>
    19     
    20 </bean>

    参考资料:https://mp.baomidou.com/

  • 相关阅读:
    Python 安装Twisted 提示python version 2.7 required,which was not found in the registry
    Openfire Strophe开发中文乱码问题
    css div 垂直居中
    How to create custom methods for use in spring security expression language annotations
    How to check “hasRole” in Java Code with Spring Security?
    Android 显示/隐藏 应用图标
    Android 当媒体变更后,通知其他应用重新扫描
    文件上传那些事儿
    专题:点滴Javascript
    主流动画实现方式总结
  • 原文地址:https://www.cnblogs.com/laoxia/p/11424786.html
Copyright © 2011-2022 走看看