SpringBoot+SQLite+MyBatis-Plus简单搭建案例
SpringBoot+SQLite+MyBatis-Plus简单搭建案例
工具:IDEA、Navicat
涉及: SpringBoot、SQLite、MyBatis-Plus、fastjson
1、使用IDEA创建SpringBoot项目。打开pom.xml文件。添加依赖支持。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <!--SQLite--> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.21.0.1</version> </dependency> <!--JSON--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency>
2、配置application.yml
server: port: 920 #数据库配置 spring: datasource: driver-class-name: org.sqlite.JDBC url: jdbc:sqlite:F:\\demo.db #username: #password: #控制台打印SQL语句 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3、创建User实体类
package com.example.sbmpsqlite.entity; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; @TableName("user") public class User { @TableField("id") private String id; @TableField("name") private String name; @TableField("age") private Integer age; @TableField("sex") private String sex; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
4、创建UserMapper,添加@Mapper注解,声明Mapper。
package com.example.sbmpsqlite.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.sbmpsqlite.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { }
5、创建UserService
package com.example.sbmpsqlite.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.sbmpsqlite.entity.User; public interface UserService extends IService<User> { }
6、创建UserServiceImpl
package com.example.sbmpsqlite.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.sbmpsqlite.entity.User; import com.example.sbmpsqlite.mapper.UserMapper; import com.example.sbmpsqlite.service.UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional(rollbackFor = Exception.class) public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
7、创建UserController
package com.example.sbmpsqlite.controller; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.sbmpsqlite.entity.User; import com.example.sbmpsqlite.service.UserService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; import java.util.UUID; @RestController @RequestMapping("/user") public class UserController { @Resource private UserService userService; /** * 查询全部 * @return */ @GetMapping("/getListUser") public List<User> getListUser() { List<User> list = userService.list(); return list; } /** * 新增 * * @return */ @GetMapping("/getSaveUser") public Object getSaveUser() { User user = user(); boolean save = userService.save(user); if (save) { return "新增成功!!!"; } return null; } public User user() { User user = new User(); user.setId(UUID.randomUUID().toString().substring(0, 10)); user.setName("李某某"); user.setAge(21); user.setSex("女"); return user; } /** * 单个查询 * 单个修改 * 单个删除 * @return */ @GetMapping("/getOneUser") public Object getOneUser() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "李某某"); //遍历打印至控制台,以JSON的格式 List<User> list = userService.list(queryWrapper); list.forEach(item -> { System.out.println(JSON.toJSONString(list.get(0))); String id = list.get(0).getId(); //修改 User user = user1(); user.setId(id); boolean b = userService.updateById(user); if (b) { System.out.println("修改成功,ID为:" + id); } else { System.out.println("修改失败,ID为:" + id); } //删除 boolean b1 = userService.removeById(id); if (b1) { System.out.println("删除成功,ID为:" + id); } else { System.out.println("删除失败,ID为:" + id); } }); return list; } public User user1() { User user = new User(); user.setName("李某某"); user.setAge(26); user.setSex("女"); return user; } }
8、修改启动类
package com.example.sbmpsqlite; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; @SpringBootApplication @MapperScan("com.example.sbmpsqlite.mapper") public class SbmpsqliteApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SbmpsqliteApplication.class, args); Environment bean = context.getBean(Environment.class); System.out.println("---启动完成,当前使用端口:[" + bean.getProperty("local.server.port") + "]---"); } }
9、结果展示
亲试可用。到这里,已经完成了对SqLite数据库的操作!