使用idea搭建一个简单的maven,实现mybatis

本文介绍了mybatis的基本概念,以及如何在idea中搭建一个简单的maven项目,实现mybatis的使用。包括创建maven工程,引入mybatis和数据库驱动依赖,创建实体类、持久层接口,编写接口映射文件和数据库配置文件,同时强调了mybatis映射文件和接口的一致性规则,以及测试实例中的设计模式应用。
摘要由CSDN通过智能技术生成

1.mybatis简介

        mybatis就是一个持久层的框架,使开发人员只需关注SQL语句本身,而无需过多关注数据库的加载驱动、创建连接等过程。采用ORM(object relational mapper)对象关系映射原理,即把数据库表和实体类以及实体类的属性对应起来,使开发人员通过操作实体类就能实现操作数据库表。

2.创建maven工程

3.在pom.xml文件中,引入mybatis依赖,由于使用的是mysql数据库,故还需导入数据库驱动依赖

<dependencies>
        <!--导入依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先,在idea创建一个新的maven项目,选择Spring Initializr模板,添加所需要的依赖: - Spring Boot DevTools - Spring Web - MyBatis Plus - Redis 2. 在pom.xml中添加如下配置: ```xml <properties> <java.version>1.8</java.version> <mybatis-plus.version>3.4.2</mybatis-plus.version> <redisson.version>3.16.1</redisson.version> </properties> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!-- Redis --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>${redisson.version}</version> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> ``` 3. 在application.yml中添加如下配置: ```yaml server: port: 8080 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 username: root password: 123456 redis: host: localhost port: 6379 database: 0 ``` 4. 创建一个UserController,实现用户的增删改查功能: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/add") public boolean addUser(@RequestBody User user) { return userService.addUser(user); } @DeleteMapping("/delete/{id}") public boolean deleteUser(@PathVariable("id") Long id) { return userService.deleteUser(id); } @PutMapping("/update") public boolean updateUser(@RequestBody User user) { return userService.updateUser(user); } @GetMapping("/get/{id}") public User getUserById(@PathVariable("id") Long id) { return userService.getUserById(id); } @GetMapping("/list") public List<User> getUserList() { return userService.getUserList(); } } ``` 5. 创建一个User实体类: ```java @Data @AllArgsConstructor @NoArgsConstructor public class User { private Long id; private String name; private Integer age; private String email; } ``` 6. 创建一个UserService,定义用户的增删改查接口: ```java public interface UserService { boolean addUser(User user); boolean deleteUser(Long id); boolean updateUser(User user); User getUserById(Long id); List<User> getUserList(); } ``` 7. 实现UserService接口,在其中使用MyBatis Plus操作数据库: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public boolean addUser(User user) { return userMapper.insert(user) > 0; } @Override public boolean deleteUser(Long id) { return userMapper.deleteById(id) > 0; } @Override public boolean updateUser(User user) { return userMapper.updateById(user) > 0; } @Override public User getUserById(Long id) { return userMapper.selectById(id); } @Override public List<User> getUserList() { return userMapper.selectList(null); } } ``` 8. 创建一个UserMapper接口,继承BaseMapper<User>,使用MyBatis Plus操作数据库: ```java @Mapper @Repository public interface UserMapper extends BaseMapper<User> { } ``` 9. 在启动类中添加@EnableCaching注解,开启缓存: ```java @SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 10. 在UserService中使用Redis缓存,优化数据库操作: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Autowired private RedissonClient redissonClient; @Override @Cacheable(value = "userCache", key = "#user.id") public boolean addUser(User user) { return userMapper.insert(user) > 0; } @Override @CacheEvict(value = "userCache", key = "#id") public boolean deleteUser(Long id) { return userMapper.deleteById(id) > 0; } @Override @CachePut(value = "userCache", key = "#user.id") public boolean updateUser(User user) { return userMapper.updateById(user) > 0; } @Override @Cacheable(value = "userCache", key = "#id") public User getUserById(Long id) { RBucket<User> userBucket = redissonClient.getBucket("user:" + id); User user = userBucket.get(); if (user == null) { user = userMapper.selectById(id); userBucket.set(user); } return user; } @Override @Cacheable(value = "userCache") public List<User> getUserList() { return userMapper.selectList(null); } } ``` 以上就是使用idea搭建一个Git项目,技术使用maven、springboot、mybatis plus、redis等的详细步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值